codely
codely

Reputation: 3

Quickly generating the new SHA-1 hash for a file with data appended to it

Here's my scenario:

So my question is if it's possible to take advantage of the fact that I already have the hash for part of the file, and just continue the new hash calculation from that point in order to save time? If so, how would I go about that?

Note that I'm currently using OpenSSL to generate my hashes, but I'm open to other options (as long as they're cross-platform).

Upvotes: 0

Views: 585

Answers (1)

Omri Barel
Omri Barel

Reputation: 9490

The input to SHA-1 contains the length of the message and some padding. From Wikipedia:

Pre-processing:
append the bit '1' to the message
append 0 ≤ k < 512 bits '0', so that the resulting message length (in bits)
   is congruent to 448 ≡ −64 (mod 512)
append length of message (before pre-processing), in bits, as 64-bit big-endian integer

This means that you cannot simply continue from a SHA-1 digest, because it already contains in itself the size of the data and padding for the block it was calculated for.

The internal state of SHA-1 is 160 bits, without them you cannot calculate a new SHA-1 digest for the longer block. That's the reason the OpenSSL functions have a "final" call - it has to be called once at the end. Until then you have to keep the internal state somehow (with the SHA-1 context or in other ways).

Upvotes: 1

Related Questions