Finding phrases with some trailing zeros on md5 hash

Hash with trailing zeros

Among English words, the words "current" and "undeniable" have an md5 hash with three trailing zeros:

echo -n current | md5sum
43b5c9175984c071f30b873fdce0a000  -

echo -n undeniable | md5sum
26e74ad4d09b9eee72aa8530e6e2c000  -

We cannot expect too much from English words because they are limited.

But, what if we create random phrases which have six trailing zeros? There is no straight algorithm for it but brute-forcing is possible:

import hashlib
import random
import string



def find_phrase_with_md5_suffix_zero(suffix_length=6):
    while True:
        phrase = generate_random_string(10)  # Adjust length as needed
        if md5_ends_with_zero(phrase, suffix_length):
            return phrase

def generate_random_string(length):
    return ''.join(random.choices(string.ascii_letters + string.digits, k=length))


def md5_ends_with_zero(phrase, suffix_length):
    md5_hash = hashlib.md5(phrase.encode()).hexdigest()
    return md5_hash[-suffix_length:] == '0' * suffix_length


result = find_phrase_with_md5_suffix_zero()
print(f'phrase found: {result}')
print(f'md5sum:', hashlib.md5(result.encode()).hexdigest())

Output

An output example of the above code is as follows

phrase found: DDbI3WW2l8
md5sum: 54e276b72fda23453b8c11d6e6000000

Here is the way to verify:

echo -n DDbI3WW2l8 | md5sum
54e276b72fda23453b8c11d6e6000000  -

The -n after echo is to prevent new line being added by echo at the end of the output because it will impact the md5 hashing.

python
md5
Software and digital electronics / Coding
Posted by admin
2024-07-27 16:35
×

Login

No account?
Terms of use
Forgot password?