Cryptography Reference
In-Depth Information
a = hash[ 0 ];
b = hash[ 1 ];
c = hash[ 2 ];
d = hash[ 3 ];
...
hash[ 3 ] += d;
hash[ 4 ] += e;
hash[ 0 ] = htonl( hash[ 0 ] );
hash[ 1 ] = htonl( hash[ 1 ] );
hash[ 2 ] = htonl( hash[ 2 ] );
hash[ 3 ] = htonl( hash[ 3 ] );
hash[ 4 ] = htonl( hash[ 4 ] );
}
Notice that all this does is reverse the hash values prior to each sha1_block_
operate call so that you can use the native arithmetic operators to work on
the block. It then re-reverses them on the way out. Of course, you also have to
reverse sha1_initial_hash .
Now you can call digest and treat the hash results uniformly, whether the
hash algorithm is MD5 or SHA-1. Go ahead and build a test main routine and
see some results as shown in Listing 4-15.
Listing 4-15: “digest.c” main routine
#ifdef TEST_DIGEST
int main( int argc, char *argv[ ] )
{
unsigned int *hash;
int hash_len;
int i;
unsigned char *decoded_input;
int decoded_len;
if ( argc < 3 )
{
fprintf( stderr, “Usage: %s [-md5|-sha] [0x]<input>\n”, argv[ 0 ] );
exit( 0 );
}
decoded_len = hex_decode( argv[ 2 ], &decoded_input );
if ( !( strcmp( argv[ 1 ], “-md5” ) ) )
{
hash = malloc( sizeof( int ) * MD5_RESULT_SIZE );
memcpy( hash, md5_initial_hash, sizeof( int ) * MD5_RESULT_SIZE );
hash_len = MD5_RESULT_SIZE;
digest_hash( decoded_input, decoded_len, hash,
md5_block_operate, md5_finalize );
}
(Continued)
 
Search WWH ::




Custom Search