Cryptography Reference
In-Depth Information
/**
* Generic digest hash computation. The hash should be set to its initial
* value *before* calling this function.
*/
int digest_hash( unsigned char *input,
int len,
unsigned int *hash,
void (*block_operate)(const unsigned char *input,
unsigned int hash[] ),
void (*block_finalize)(unsigned char *block, int length ) )
{
unsigned char padded_block[ DIGEST_BLOCK_SIZE ];
int length_in_bits = len * 8;
while ( len >= INPUT_BLOCK_SIZE )
{
// Special handling for blocks between 56 and 64 bytes
// (not enough room for the 8 bytes of length, but also
// not enough to fill up a block)
if ( len < DIGEST_BLOCK_SIZE )
{
memset( padded_block, 0, sizeof( padded_block ) );
memcpy( padded_block, input, len );
padded_block[ len ] = 0x80;
block_operate( padded_block, hash );
input += len;
len = -1;
}
else
{
block_operate( input, hash );
input += DIGEST_BLOCK_SIZE;
len -= DIGEST_BLOCK_SIZE;
}
}
memset( padded_block, 0, sizeof( padded_block ) );
if ( len >= 0 )
{
memcpy( padded_block, input, len );
padded_block[ len ] = 0x80;
}
block_finalize( padded_block, length_in_bits );
block_operate( padded_block, hash );
return 0;
}
Search WWH ::




Custom Search