This repository has been archived on 2023-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
MagicHashes/magic_hashes_random.py

58 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""
This program intents to "break" _magic_ hashes for the SHA-2 algorithms family.
It will generate random strings, only containing printable ASCII characters.
If a magic hash is found, it will create a text file and will write down the match.
"""
import hashlib
import random
import re
import string
if __name__ == '__main__':
# Generate "plausible" password strings, between 4 and 32-character long.
BOUNDS = (4 + 1, 32 + 1)
POOL = string.ascii_letters + string.digits + string.punctuation
# _______________________________________________________________________
# "Magic Hashes" format.
PATTERN = re.compile(r'^0+e\d+$')
# ______________________
# Hash methods to search against.
HASH_METHODS = [
hashlib.sha224,
hashlib.sha256,
hashlib.sha384,
hashlib.sha512
]
# _______________________________
while True:
# Let's generate a random string !
# pylint: disable=C0103
random_string = ''.join(
random.choice(POOL) for _ in range(random.randint(*BOUNDS))
).encode()
# ________________________________
# Compute hashes over here.
for hash_method in HASH_METHODS:
digest = hash_method(random_string).hexdigest()
if re.search(PATTERN, digest):
with open('magic_hashes.txt', 'a') as f_magic:
f_magic.write(
'[{0}]\t{1}\t{2}\n'.format(
hash_method.__name__,
random_string.decode(),
digest
)
)
# _________________________