Cryptography Reference
In-Depth Information
if ( input_len > 0 )
{
memcpy( context->block, input, input_len );
context->block_len = input_len;
}
}
This is probably the most complex function presented so far, so it's worth
going through carefully:
1. Update the input length. You have to append this to the very last block,
whenever that may come.
context->input_len = input_len;
// Process any left over from the last call to “update_digest”
if ( context->block_len > 0 )
{
...
}
2. Check to see if you have any data left over from a previous call. If you
don't, go ahead and process the data, one block at a time, until you run
out of blocks:
while ( input_len >= DIGEST_BLOCK_SIZE )
{
context->block_operate( input, context->hash );
input = DIGEST_BLOCK_SIZE;
input_len -= DIGEST_BLOCK_SIZE;
}
This ought to look familiar; it's the main loop of the original digest func-
tion from Listing 4-13.
3. Process one entire block, update the hash, increment the input pointer
and decrement the length counter. At the end, input_len is either 0 or
some integer less than the block size, so just store the remaining data in
the context pointer until the next time update_digest is called:
if ( input_len > 0 )
{
memcpy( context->block, input, input_len );
context->block_len = input_len;
}
At this point, you know that input_len is less than one block size, so
there's no danger of overrunning the temporary buffer context->block .
4. Next time update_digest is called, check to see if there's any data left over
from the previous call. If so, concatenate data from the input buffer onto
Search WWH ::




Custom Search