Created
November 24, 2018 19:59
-
-
Save pqlx/3e8d0c9fd6c5e3a1b9f50f2b05919313 to your computer and use it in GitHub Desktop.
Small hash cracker (not to be taken seriously, use hashcat or JTR)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import * | |
from typing import BinaryIO | |
import hashlib | |
class HashCracker(object): | |
supported_algos = hashlib.algorithms_available | |
@classmethod | |
def crack_hash(cls: type, algo: str, ciphertext: AnyStr, plaintext_pool: Iterable[bytes]) -> bytes: | |
if algo not in cls.supported_algos: | |
raise NotImplementedError(f"Cracking '{algo}' is not supported yet") | |
hash_func = getattr(hashlib, algo) | |
if isinstance(ciphertext, str): | |
ciphertext = ciphertext.lower() # The hashlib hexdigest is in lowercase | |
for p in plaintext_pool: | |
h = hash_func(p) | |
if isinstance(ciphertext, str): | |
digest = h.hexdigest() | |
elif isinstance(ciphertext, bytes): | |
digest = h.digest() | |
if digest == ciphertext: | |
return p | |
@classmethod | |
def crack_hash_file(cls: type, algo: str, ciphertext: AnyStr, file: Union[str, BinaryIO]) -> bytes: | |
def line_generator(stream: BinaryIO) -> Iterable[bytes]: | |
while True: | |
line = stream.readline() | |
if line == '': | |
return | |
yield line[:-1] # remove newline but retain other whitespace | |
f: BinaryIO | |
if isinstance(file, str): | |
f = open(file, 'rb') | |
else: | |
f = file | |
with f as stream: | |
cls.crack_hash(algo, ciphertext, line_generator(stream)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment