1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
/* -*- Mode: C; c-file-style: "stroustrup" -*- */
#include <stdio.h>
#include <string.h>
#include "sha1.h"
#define BUFFER_SIZE 1024
int SHA1_file( char* filename, byte md[ SHA1_DIGEST_BYTES ] )
{
FILE* file = NULL ;
byte buffer[ BUFFER_SIZE ] = {0};
size_t bytes_read = 0 ;
int opened = 0;
SHA1_ctx ctx;
if ( strcmp( filename, "-" ) == 0 ) {
file = stdin;
} else {
file = fopen( filename, "rb" );
if ( file == 0 ) { return -1; }
opened = 1;
}
SHA1_Init( &ctx );
while ( !feof( file ) ) {
bytes_read = fread( buffer, 1, BUFFER_SIZE, file );
if ( bytes_read < BUFFER_SIZE && ferror( file ) ) {
return -1;
}
SHA1_Update( &ctx, buffer, bytes_read );
}
SHA1_Final( &ctx, md );
if ( opened ) {
fclose( file );
}
return 0;
}
const char* hex_digest( byte md[ SHA1_DIGEST_BYTES ] )
{
int i = 0 ;
static char hex[ SHA1_DIGEST_BYTES * 2 + 1 ] = {0};
for ( i = 0; i < SHA1_DIGEST_BYTES; i++ ) {
sprintf( hex + 2 * i, "%02x", md[ i ] );
}
hex[ sizeof( hex ) - 1 ] = '\0';
return hex;
}
int main( int argc, char* argv[] )
{
int i = 0 ;
byte digest[ SHA1_DIGEST_BYTES ] = {0};
int status = 0 ;
if ( argc == 1 ) {
status = SHA1_file( "-", digest );
if ( status < 0 ) {
perror( "(stdin)" );
} else {
printf( "%s\n", hex_digest( digest ) );
}
} else {
for ( i = 1; i < argc; i++ ) {
status = SHA1_file( argv[ i ], digest );
if ( status < 0 ) {
perror( argv[ i ] );
return 1;
} else {
printf( "%s %s\n", hex_digest( digest ), argv[ i ] );
}
}
}
return 0;
}
|