Cryptography Reference
In-Depth Information
hash and hash_len are fairly straightforward; this is the hash code as it has
been computed with the data that's been given so far. input_len is the number
of bytes that have been passed in; remember input_len needs to be tracked in
order to append the length in bits to the virtual buffer before computing the
fi nal hash code. Go ahead and stick the block_operate and block_finalize
function pointers in here so that you don't have to further pollute your function
call signatures. Finally, there's a block of temporary storage; if a call to update
is made with less than a full block (or with a non-aligned bit left over), store it
here, along with its length, and pass it on to block_operate when there's enough
data to make a full block. This is shown in Listing 4-22.
Listing 4-22: “digest.c” update digest function
void update_digest( digest_ctx *context, const unsigned char *input, int input_
len )
{
context->input_len += input_len;
// Process any left over from the last call to “update_digest”
if ( context->block_len > 0 )
{
// How much we need to make a full block
int borrow_amt = DIGEST_BLOCK_SIZE - context->block_len;
if ( input_len < borrow_amt )
{
memcpy( context->block + context->block_len, input, input_len );
context->block_len += input_len;
input_len = 0;
}
else
{
memcpy( context->block + context->block_len, input, borrow_amt );
context->block_operate( context->block, context->hash );
context->block_len = 0;
input += borrow_amt;
input_len -= borrow_amt;
}
}
while ( input_len >= DIGEST_BLOCK_SIZE )
{
context->block_operate( input, context->hash );
input += DIGEST_BLOCK_SIZE;
input_len -= DIGEST_BLOCK_SIZE;
}
// Have some non-aligned data left over; save it for next call, or
// “finalize” call.
 
Search WWH ::




Custom Search