vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

sha2_main.c (2266B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <errno.h>
      4 
      5 #include "sha2.h"
      6 
      7 
      8 #define BLOCKSIZE 4096
      9 /* Ensure that BLOCKSIZE is a multiple of 64.  */
     10 #if BLOCKSIZE % 64 != 0
     11 /* FIXME-someday (soon?): use #error instead of this kludge.  */
     12 "invalid BLOCKSIZE"
     13 #endif
     14 
     15 
     16 /* Compute SHA512 message digest for bytes read from STREAM.  The
     17    resulting message digest number will be written into the 16 bytes
     18    beginning at RESBLOCK.  */
     19 int
     20 sha512_stream (FILE *stream, void *resblock)
     21 {
     22   SHA512_CTX ctx;
     23   char buffer[BLOCKSIZE + 72];
     24   size_t sum;
     25 
     26   /* Initialize the computation context.  */
     27   SHA512_Init (&ctx);
     28 
     29   /* Iterate over full file contents.  */
     30   while (1)
     31     {
     32       /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
     33 	 computation function processes the whole buffer so that with the
     34 	 next round of the loop another block can be read.  */
     35       size_t n;
     36       sum = 0;
     37 
     38       /* Read block.  Take care for partial reads.  */
     39       while (1)
     40 	{
     41 	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
     42 
     43 	  sum += n;
     44 
     45 	  if (sum == BLOCKSIZE)
     46 	    break;
     47 
     48 	  if (n == 0)
     49 	    {
     50 	      /* Check for the error flag IFF N == 0, so that we don't
     51 		 exit the loop after a partial read due to e.g., EAGAIN
     52 		 or EWOULDBLOCK.  */
     53 	      if (ferror (stream))
     54 		return 1;
     55 	      goto process_partial_block;
     56 	    }
     57 
     58 	  /* We've read at least one byte, so ignore errors.  But always
     59 	     check for EOF, since feof may be true even though N > 0.
     60 	     Otherwise, we could end up calling fread after EOF.  */
     61 	  if (feof (stream))
     62 	    goto process_partial_block;
     63 	}
     64 
     65       /* Process buffer with BLOCKSIZE bytes.  Note that
     66 			BLOCKSIZE % 64 == 0
     67        */
     68       SHA512_Update (&ctx, buffer, BLOCKSIZE);
     69     }
     70 
     71  process_partial_block:;
     72 
     73   /* Process any remaining bytes.  */
     74   if (sum > 0)
     75     SHA512_Update (&ctx, buffer, sum);
     76 
     77   /* Construct result in desired memory.  */
     78   SHA512_Final (resblock, &ctx);
     79   return 0;
     80 }
     81 
     82 int
     83 main( int   argc,
     84       char *argv[] )
     85 {
     86   unsigned char output[SHA512_DIGEST_LENGTH];
     87   int i;
     88 
     89   if ( sha512_stream( stdin, output ) != 0 ) {
     90     fprintf( stderr, "error reading stdin: errno=%i", errno );
     91     return 1;
     92   }
     93 
     94   for ( i = 0; i < sizeof( output ); i++ )
     95     printf("%02x", output[i]);
     96 
     97   return 0;
     98 }
     99