#include <stdio.h>
#include "sha1.h"

#define BUFFER_SIZE 1024

int SHA1_file( char* filename, byte md[ SHA1_DIGEST_BYTES ] )
{
    FILE* file;
    byte buffer[ BUFFER_SIZE ];
    int bytes_read;
    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 );
    memcpy( md, SHA1_digest( &ctx ), SHA1_DIGEST_BYTES );
    if ( opened )
    {
	fclose( file );
    }
    return 0;
}

const char* hex_digest( byte md[ SHA1_DIGEST_BYTES ] )
{
    int i;
    static char hex[ SHA1_DIGEST_BYTES * 2 + 1 ];

    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;
    byte digest[ SHA1_DIGEST_BYTES ];
    int status;
    
    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;
}
