How to "reduce" a hash?

Posted by Julien Lebosquain on Stack Overflow See other posts from Stack Overflow or by Julien Lebosquain
Published on 2010-06-13T15:56:05Z Indexed on 2010/06/13 16:02 UTC
Read the original article Hit count: 283

Filed under:
|

Suppose I have any "long" hash, like a 16 bytes MD5 or a 20 bytes SHA1. I want to reduce this hash to fit on 4 bytes, for GetHashCode() purposes.

First, I'm perfectly aware that I'll get more collisions. That's totally fine in my case, but I'd still prefer to get the less possible collisions.

There are several solutions to my problem:

  • I could take the 4 first bytes of the hash.
  • I could take the 4 last bytes of the hash.
  • I could take 4 random bytes of the hash.
  • I could generate a hash of the hash, involving classic prime numbers multiplications.

Are there other solutons I didn't think about? And more importantly, what method will give me the most unique hash code? I'm currently supposing they're almost equivalent.

Microsoft choose that the public key token of an assembly is the last 8 bytes of the SHA1 hash of its public key, so I'll probably go for this solution but I'd like to know why.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about hashcode