whi
whi

Reputation: 2750

sha1 collision of less than 512 bit

In Wikipedia SHA-1 pseudocode, it's said:

Pre-processing: append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits. append 0 ≤ k < 512 bits '0', such that the resulting message length in bits

So is it same with \x01 and x00\x01?

Python example:

import hashlib
for s in ('01', '0001'):
    m=hashlib.sha1()
    m.update(s.decode('hex'))
    print m.hexdigest()

>>>bf8b4530d8d246dd74ac53a13471bba17941dff7
>>>0e356ba505631fbf715758bed27d503f8b260e3a

It turns out not to be the same, why?

Upvotes: 0

Views: 396

Answers (1)

chenzhongpu
chenzhongpu

Reputation: 6871

\x01 and \x0001 cannot be the same after pre-processing.

I think that you misunderstand the pseudocode in Wiki. Here I take \x01 and \x0001 as examples, which their length are less than 512 bits.

(Now suppose that the original message length is less than 448 bits)

  • first step: fill in to let its length =448 (mod 512). We append the 1 bit first, then append 0.

Thus, for \x01, it becomes \x01800000...000, and its length is 448 bits, and the ellipsis represents 0 in hex.

  • second step: fill in the length. We should add the reminder (512 - 448) = 64 bits to represent its original length.

Thus, for \x01, its original length is 8, it shall append \x00000000 00000008.

From mentioned above, after pre-processing, \x01 becomes \x01800000...08, and whole length is 512, and the ellipsis represents 0 in hex.

Upvotes: 1

Related Questions