Search code examples
pythoncryptographysha1

Is it possible for the output of SHA-1 to end in zero bytes?


Is it possible for sha1 hashes to end with zero bytes?

Python Code

hash = haslib.sha1();
hash.update(STRING THAT RESULTS IN ZERO BYTES);
if hash.digest().endswith('\x00\x00\x00'):
....

Solution

  • Yes, since hash function outputs are supposed to be uniformly distributed you have a non-zero chance of getting any supported output. The chance for seeing the last 24 bits (3 byte) as zero is 1.0/(2**24) or 5.9 * 10-8. On average you will have to try 223 random strings to find one with this property. Since hashing is pretty fast, this shouldn't take longer than a couple of minutes (although, Python might be a bit slow in general).