bcrypt.ctf/bcrypter/debug/hasheq.py

40 lines
1.3 KiB
Python
Raw Normal View History

2025-06-21 19:05:47 +10:00
'''
This file implements debug methods to check whether
hashrev.bcrypt() operates identically to hash.bcrypt().
TLDR: test if I made a dumb typo.
'''
from random import randint, randbytes
import bcrypt.hash
import bcrypt.hashrev
def is_bcrypt_eq(x: bytes, R_table: Optional[dict[int, int]] = None) -> bool:
return hashrev.bcrypt(x, R_table=R_table) == hash.bcrypt(x)
'''
Display internals of testing bcrypt implementations
from bcrypt/hash.py and bcrypt/hashrev.py
NOTE: By default this runs 10000 trials for
NOTE: random sequences of length 0-16 bytes.
'''
def disint_bcrypt_eq(trials: int = 10000,
max_bytes: int = 16) -> bytes | None:
R_table = hashrev.calc_R_array(max = 255)
for i in range(trials):
# generate random bytes
num_bytes = randint(0, max_bytes)
x = randbytes(num_bytes)
# test the modified bcrypt with the original
hash_rev = hashrev.bcrypt(x, R_table=R_table)
hash_expected = hash.bcrypt(x)
if hash_rev != hash_expected:
print(f'Your hashfn sucks, big mistake bucko!! (failed iter: {i})')
print(f' Got: {hash_rev}')
print(f'Expected: {hash_bcrypt}')
print(f'Input Bytes: {[str(b) for b in x]}')
return x
print('Impeccable hashfn holy moly!!')
return None