warcsum/warcsum.c
/*
 * Finalize hash and produce digest in hex
 */
Finalize hash and produce digest in hex
Finalize hash and produce digest in hex
int
hash_final (void* hash_ctx, int hash, char* computed_digest,
            struct cli_args args)
{
  time_t now_hash;
  time (&now_hash);
  int j = 0;
  int i;
  int ret;
  unsigned char result[DIGEST_LENGTH];
  switch (hash)
    {
    case 1:
      ret = MD5_Final (result, (MD5_CTX*) hash_ctx);
      for (i = 0; i < MD5_DIGEST_LENGTH; i++, j += 2)
        {
          char temp[3];
          snprintf (temp, sizeof (temp), "%02x", result[i]);
          computed_digest[j] = temp[0];
          computed_digest[j + 1] = temp[1];
        }
      computed_digest[j] = '\0';
      if (args.verbose == 2)
        {
          fprintf (stderr, "Hash: MD5 \n");
        }
      break;
    case 2:
      ret = SHA1_Final (result, (SHA_CTX*) hash_ctx);
      for (i = 0; i < SHA_DIGEST_LENGTH; i++, j += 2)
        {
          char temp[3];
          snprintf (temp, sizeof (temp), "%02x", result[i]);
          computed_digest[j] = temp[0];
          computed_digest[j + 1] = temp[1];
        }
      computed_digest[j] = '\0';
      if (args.verbose == 2)
        {
          fprintf (stderr, "Hash: SHA1 \n");
        }
      break;
    case 3:
      ret = SHA256_Final (result, (SHA256_CTX*) hash_ctx);
      for (i = 0; i < SHA256_DIGEST_LENGTH; i++, j += 2)
        {
          char temp[3];
          snprintf (temp, sizeof (temp), "%02x", result[i]);
          computed_digest[j] = temp[0];
          computed_digest[j + 1] = temp[1];
        }
      if (args.verbose == 2)
        {
          fprintf (stderr, "Hash: SHA256 \n");
        }
      computed_digest[j] = '\0';
      break;
    case 4:
      ret = SHA512_Final (result, (SHA512_CTX*) hash_ctx);
      for (i = 0; i < SHA512_DIGEST_LENGTH; i++, j += 2)
        {
          char temp[3];
          snprintf (temp, sizeof (temp), "%02x", result[i]);
          computed_digest[j] = temp[0];
          computed_digest[j + 1] = temp[1];
        }
      if (args.verbose == 2)
        {
          fprintf (stderr, "Hash: SHA512 \n");
        }
      computed_digest[j] = '\0';
      break;
    default:
      fprintf (stderr, "Unknown hash algorithm: %d!!\n"
               "How did you get here?!\n\n", hash);
      exit (EXIT_FAILURE);
    }
  free (hash_ctx);
  time_t then_hash;
  time (&then_hash);
  time_hash += difftime (then_hash, now_hash);
  return ret;
}
int hash_final ( void * hash_ctx , int hash , char * computed_digest , struct cli_args args ) { time_t now_hash ; time ( & now_hash ) ; int j = 0 ; int i ; int ret ; unsigned char result [ DIGEST_LENGTH ] ; switch ( hash ) { case 1 : ret = MD5_Final ( result , ( MD5_CTX * ) hash_ctx ) ; for ( i = 0 ; i < MD5_DIGEST_LENGTH ; i ++ , j += 2 ) { char temp [ 3 ] ; snprintf ( temp , sizeof ( temp ) , " " , result [ i ] ) ; computed_digest [ j ] = temp [ 0 ] ; computed_digest [ j + 1 ] = temp [ 1 ] ; } computed_digest [ j ] = ' \0 ' ; if ( args . verbose == 2 ) { fprintf ( stderr , " \n " ) ; } break ; case 2 : ret = SHA1_Final ( result , ( SHA_CTX * ) hash_ctx ) ; for ( i = 0 ; i < SHA_DIGEST_LENGTH ; i ++ , j += 2 ) { char temp [ 3 ] ; snprintf ( temp , sizeof ( temp ) , " " , result [ i ] ) ; computed_digest [ j ] = temp [ 0 ] ; computed_digest [ j + 1 ] = temp [ 1 ] ; } computed_digest [ j ] = ' \0 ' ; if ( args . verbose == 2 ) { fprintf ( stderr , " \n " ) ; } break ; case 3 : ret = SHA256_Final ( result , ( SHA256_CTX * ) hash_ctx ) ; for ( i = 0 ; i < SHA256_DIGEST_LENGTH ; i ++ , j += 2 ) { char temp [ 3 ] ; snprintf ( temp , sizeof ( temp ) , " " , result [ i ] ) ; computed_digest [ j ] = temp [ 0 ] ; computed_digest [ j + 1 ] = temp [ 1 ] ; } if ( args . verbose == 2 ) { fprintf ( stderr , " \n " ) ; } computed_digest [ j ] = ' \0 ' ; break ; case 4 : ret = SHA512_Final ( result , ( SHA512_CTX * ) hash_ctx ) ; for ( i = 0 ; i < SHA512_DIGEST_LENGTH ; i ++ , j += 2 ) { char temp [ 3 ] ; snprintf ( temp , sizeof ( temp ) , " " , result [ i ] ) ; computed_digest [ j ] = temp [ 0 ] ; computed_digest [ j + 1 ] = temp [ 1 ] ; } if ( args . verbose == 2 ) { fprintf ( stderr , " \n " ) ; } computed_digest [ j ] = ' \0 ' ; break ; default : fprintf ( stderr , " \n " " \n \n " , hash ) ; exit ( EXIT_FAILURE ) ; } free ( hash_ctx ) ; time_t then_hash ; time ( & then_hash ) ; time_hash += difftime ( then_hash , now_hash ) ; return ret ; }
Finalize hash and produce digest in hex
Finalize hash and produce digest in hex
/* TIME */ /* END OF TIME */ /* TIME */ /* END OF TIME */