Cryptography Reference
In-Depth Information
memset( context->block, '\0', DIGEST_BLOCK_SIZE );
context->block_operate = sha1_block_operate;
context->block_finalize = sha1_finalize;
}
And fi nally, Listing 4-26 shows how you initialize the SHA-256 context.
Listing 4-26: “sha.c” SHA-256 digest initialization
void new_sha256_digest( digest_ctx *context )
{
context->hash_len = 8;
context->input_len = 0;
context->block_len = 0;
context->hash = ( unsigned int * ) malloc( context->hash_len *
sizeof( unsigned int ) );
memcpy( context->hash, sha256_initial_hash, context->hash_len *
sizeof( unsigned int ) );
memset( context->block, '\0', DIGEST_BLOCK_SIZE );
context->block_operate = sha256_block_operate;
context->block_finalize = sha1_finalize;
}
Of course if you want to support more hash contexts, just add more of them
here.
After the contexts have been initialized, they're just passed to update_digest
as new data becomes available, and passed to finalize_digest after all the data
to be hashed has been accumulated.
Computing the MD5 Hash of an Entire File
An example might help to clarify how these updateable digest functions work.
Consider a real-world example — computing the MD5 hash of an entire fi le:
// hash a file; buffer input
digest_ctx context;
const char *filename = “somefile.tmp”;
char buf[ 400 ]; // purposely non-aligned to exercise updating logic
int bytes_read;
int f = open( filename, O_RDONLY );
if ( !f )
{
fprintf( stderr, “Unable to open '%s' for reading: “, filename );
perror( “” );
exit( 0 );
}
new_md5_digest( &context );
 
Search WWH ::




Custom Search