continued reorganisation

This commit is contained in:
Emile Clark-Boman 2025-06-21 21:29:00 +10:00
parent 6f8a7322f2
commit 0a2d9a5694
22 changed files with 190 additions and 61 deletions

View file

View file

@ -0,0 +1,13 @@
'''
This file provides various methods for determining
properties, connections, etc for bcrypt's constants.
'''
from math import gcd
from bcrypt.hash.rev import H, K, M
def disint_gcds() -> None:
print(f'gcd(H,K): {gcd(H,K)}')
print(f'gcd(H,M): {gcd(H,M)}')
print(f'gcd(K,M): {gcd(K,M)}')

View file

@ -0,0 +1,18 @@
'''
This file provides various "disint" (display internal) methods
regarding the "constituent functions" that exist in "bcrypt".
'''
from bcrypt.lib.format import lpad, lpadbin
'''
Display internal calculations of Rb(b: int) for bcrypt/hashrev.py
'''
def disint_Rb(b: int):
Rb = 0
for j in range(8):
j_sq = j**2
Rjb = b << j_sq
Rb ^= Rjb
print(f'{lpad(j_sq, 2)}: {lpadbin(Rjb, 64)} {Rjb}')
print(f'Rb: {lpadbin(Rb, 64)} {Rb}')

39
bcrypter/debug/hasheq.py Normal file
View file

@ -0,0 +1,39 @@
'''
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