# ------------------------------------------------------------
# PSUT IoT Engineering Studio | CR66
# Experiment: password hashing salt and cost
# Purpose: expose algorithm states and verify the experiment invariant.
# ------------------------------------------------------------
# CR66 | Password hashing: salt and cost
# Execute the named operation; expose states and assert the laboratory invariant.
import hashlib
import hmac
import json



DEFAULT={'iterations': 10000}
ALTERNATE={'iterations': 20000}

def run(overrides=None):
    # Merge only explicitly supplied inputs with the reference baseline.
    params={**DEFAULT, **(overrides or {})}
    iterations=params['iterations']
    if type(iterations) is not int or not 1000<=iterations<=200000: raise ValueError("Choose 1000–200000 classroom iterations")
    import time
    password=b'PSUT-synthetic-only';s1=b'lab-salt-0000001';s2=b'lab-salt-0000002';steps=[];values=[]
    for salt in (s1,s2):
        t=time.perf_counter();dk=hashlib.pbkdf2_hmac('sha256',password,salt,iterations);dt=(time.perf_counter()-t)*1000;values.append(dk);steps.append({'salt':salt.hex(),'iterations':iterations,'derived':dk.hex(),'elapsed_ms':round(dt,4)})
    result={'different_salts_different':values[0]!=values[1],'valid_password':hmac.compare_digest(values[0],hashlib.pbkdf2_hmac('sha256',password,s1,iterations))}
    # Independently check the stated mathematical or protocol invariant.
    assert result["different_salts_different"] and result["valid_password"], "verification failed"
    return {"steps":steps,"result":result}

if __name__ == "__main__":
    import argparse
    parser=argparse.ArgumentParser();parser.add_argument("--inputs",default="{}");parser.add_argument("--test",action="store_true");args=parser.parse_args()
    if args.test:
        for case in [{},ALTERNATE]:run(case)
        print("PASS")
    else:print(json.dumps(run(json.loads(args.inputs)),indent=2))
