"""Compare SHA-256 and SHA-512 using independent library checks."""
import hashlib, hmac
from lab11 import find_suffix, truncated_collision, manual_hmac, keyed_digits
for alg in ('sha256','sha512'):
    a=hashlib.new(alg,b'I like cats').digest(); b=hashlib.new(alg,b'I like cars').digest()
    print(alg, len(a)*8, 'bits:', a.hex(), 'changed bits:',sum((x^y).bit_count() for x,y in zip(a,b)))
    attempts,m,d=find_suffix('00',algorithm=alg); assert d.endswith('00') and d==hashlib.new(alg,m).hexdigest()
    x,y,suffix,n=truncated_collision(algorithm=alg); assert x!=y and hashlib.new(alg,x).hexdigest()[-2:]==hashlib.new(alg,y).hexdigest()[-2:]==suffix
    key=bytes(range(16)); msg=b'Temperature=25'; assert manual_hmac(msg,key,alg)==hmac.digest(key,msg,alg)
    assert len(keyed_digits(key,100,algorithm=alg))==100
    print('Suffix attempts:',attempts,'collision attempts:',n,'HMAC:',manual_hmac(msg,key,alg).hex())
