Cryptography Reference
In-Depth Information
hash[ 2 ] = sha1_initial_hash[ 2 ];
hash[ 3 ] = sha1_initial_hash[ 3 ];
hash[ 4 ] = sha1_initial_hash[ 4 ];
while ( len >= SHA1_INPUT_BLOCK_SIZE )
{
if ( len < SHA1_BLOCK_SIZE )
{
memset( padded_block, 0, sizeof( padded_block ) );
memcpy( padded_block, input, len );
padded_block[ len ] = 0x80;
sha1_block_operate( padded_block, hash );
input += len;
len = -1;
}
else
{
sha1_block_operate( input, hash );
input += SHA1_BLOCK_SIZE;
len -= SHA1_BLOCK_SIZE;
}
}
memset( padded_block, 0, sizeof( padded_block ) );
if ( len >= 0 )
{
memcpy( padded_block, input, len );
padded_block[ len ] = 0x80;
}
padded_block[ SHA1_BLOCK_SIZE - 4 ] = ( length_in_bits & 0xFF000000 ) >> 24;
padded_block[ SHA1_BLOCK_SIZE - 3 ] = ( length_in_bits & 0x00FF0000 ) >> 16;
padded_block[ SHA1_BLOCK_SIZE - 2 ] = ( length_in_bits & 0x0000FF00 ) >> 8;
padded_block[ SHA1_BLOCK_SIZE - 1 ] = ( length_in_bits & 0x000000FF );
sha1_block_operate( padded_block, hash );
return 0;
}
In fact, sha1_hash and md5_hash are so similar it's almost painful not
to just go ahead and consolidate them into a common function. Go ahead
and do so.
Because md5_block_operate and sha1_block_operate have identical method
signatures (what a coincidence!), you can just pass the block_operate function
in as a function pointer as in Listing 4-10.
Search WWH ::




Custom Search