Alex Reynolds
Alex Reynolds

Reputation: 96927

Are SHA1 hashes distributed uniformly?

I have a string in Python. I calculate the SHA1 hash of that string with hashlib. I convert it to its hexadecimal representation and take the last 16 characters to use as an identifier:

hash_str = "foobarbazάλφαβήταγάμμα..."
hash_obj = hashlib.sha1(hash_str, encode('utf-8'))
hash_id  = hash_obj.hexdigest()[:16]

My goal is an identifier that provides reasonable length and is unlikely to yield the same hash_id value for a different hash_str input.

If the probability of a SHA1 collision is 1/(2^160), or 1/(16^40), then if I take the last sixteen characters of the hex representation, is the probability of a collision only 1/(16^16)? Or are the bytes (or their hex equivalent) not distributed evenly?

Upvotes: 10

Views: 4646

Answers (1)

abligh
abligh

Reputation: 25119

Yes. Any hash function which exhibits the property of uniformity has equal chance of any value in its output range being generated by a randomly chosen input value. Therefore, each value of the truncated hash is equally likely too. SHA-1 is is hash function that demonstrates uniformity, therefore your conjecture is true.

Upvotes: 7

Related Questions