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

byte b;

#define BUF_SIZE 1024

int main( int argc, char* argv[] )
{
   SHA1_ctx ctx;
   byte buf[ BUF_SIZE ];
   int len;
   int i;
   long secondCount;
   const byte* digest;

/* test 1 data */
   const byte* test1 = "abc";

   word32 result1[ 5 ] = {
      0xA9993E36, 0x4706816A, 0xBA3E2571, 0x7850C26C, 0x9CD0D89D };

/* test 2 date */
   const byte* test2 = 
      "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";

   word32 result2[ 5 ] = {
      0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, 0xF95129E5, 0xE54670F1 };

/* test 3 data */
   /* test 3 is 1,000,000 letter "a"s. */
   /* fill test 3 with "a"s and hash this 5000 times */
   byte test3[ 200 ];
   word32 result3[ 5 ] = {
      0x34AA973C, 0xD4C4DAA4, 0xF61EEB2B, 0xDBAD2731, 0x6534016F };

/* test 1 */

   printf( "test 1\n\n" );
   SHA1_init( &ctx );
   SHA1_update( &ctx, test1, strlen( test1 ) );
   SHA1_final( &ctx );
   printf( "SHA1(\"%s\") = \n\t", test1 );

/* illustrating printing using output of SHA1_digest function */

   digest = SHA1_digest( &ctx );

   for ( i = 0; i < 20 ; i++ )
   {
      printf( "%02x", digest[ i ] );
   }

   if ( memcmp( ctx.H, result1, 20 ) == 0 )
   {
      printf( " test ok\n" );
   }
   else
   {
      printf( " test failed\n" );
   }
   printf( "\n" );

/* test 2 */

   printf( "test 2\n\n" );
   SHA1_init( &ctx );
   SHA1_update( &ctx, test2, strlen( test2 ) );
   SHA1_final( &ctx );

/* illustrating printing using ctx.H values directly */

   printf( "SHA1(\"%s\") = \n\t", test2 );

   for ( i = 0; i < 5; i++ )
   {
      printf( "%08x", ctx.H[ i ] );
   }

   if ( memcmp( ctx.H, result2, 20 ) == 0 )
   {
      printf( " test ok\n" );
   }
   else
   {
      printf( " test failed\n" );
   }
   printf( "\n" );

/* test 3 */

   printf( "test 3\n\n" );

/* we'll time test3 to see how many Mb/s we can do */

   timer_start();

   SHA1_init( &ctx );
   memset( test3 , 'a', 200 );	/* fill test3 with "a"s */
   for( i = 0; i < 5000; i++ )	/* digest 5000 times */
   {
      SHA1_update( &ctx, test3, 200 );
   }
   SHA1_final( &ctx );

   timer_stop();

   printf( "SHA1(\"a\" x 1,000,000) = \n\t" );

   for ( i = 0; i < 5; i++ )
   {
      printf( "%08x", ctx.H[ i ] );
   }

   if ( memcmp( ctx.H, result3, 20 ) == 0 )
   {
      printf( " test ok\n" );
   }
   else
   {
      printf( " test failed\n" );
   }
   printf( "\n" );

/* report timing information */

   printf( "speed: %.2lf Mb/s\n", 1 / ( timer_time() / 1000000.0 ) );

   return 0;
}
