Something went wrong. Try again.
passlib's password hash schemes, ported to Zig 0.16
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
"""Asks passlib 1.7.4 to verify the hashes tools/crosscheck.zig made.
Four Cisco formats passlib does not have are checked against an independentimplementation here instead; see REFERENCE.
Each line is `scheme, hex(password), user, realm, hex(hash)`, tab-separated.Every hash must verify with passlib under the same scheme, and a passwordthat differs from it must not. The schemes that verify nothing(unix_disabled, unix_fallback, django_disabled) must verify nothing."""
import base64import hashlibimport hmacimport osimport sysimport warnings
warnings.simplefilter("ignore")# passlib 1.7.4 cannot drive the bcrypt package nixpkgs has; see flake.nix.os.environ["PASSLIB_BUILTIN_BCRYPT"] = "enabled"
import passlibfrom passlib.registry import get_crypt_handler
assert passlib.__version__ == "1.7.4", passlib.__version__
DISABLED = {"unix_disabled", "unix_fallback", "django_disabled"}
# The Cisco formats passlib 1.7.4 does not have, checked instead with Python's# hashlib the way ciscoPWDhasher computes them: the salt of types 8 and 9 as# its ASCII, and standard base64 moved to crypt's alphabet, unpadded._STD64 = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"_CISCO64 = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def _cisco64(raw): return base64.b64encode(raw).rstrip(b"=").translate(bytes.maketrans(_STD64, _CISCO64)).decode()
def _type4(password, hash): return hmac.compare_digest(_cisco64(hashlib.sha256(password).digest()), hash)
def _type8(password, hash): _, _, salt, digest = hash.split("$") return hmac.compare_digest(_cisco64(hashlib.pbkdf2_hmac("sha256", password, salt.encode(), 20000, 32)), digest)
def _type9(password, hash): _, _, salt, digest = hash.split("$") raw = hashlib.scrypt(password, salt=salt.encode(), n=16384, r=1, p=1, dklen=32) return hmac.compare_digest(_cisco64(raw), digest)
def _asa_pbkdf2(password, hash): _, _, rounds, salt, digest = hash.split("$") raw = hashlib.pbkdf2_hmac("sha512", password, base64.b64decode(salt), int(rounds), 16) return hmac.compare_digest(base64.b64encode(raw).decode(), digest)
class _Reference: context_kwds = ()
def __init__(self, check): self.check = check
def verify(self, password, hash): return self.check(password, hash)
REFERENCE = { "cisco_type4": _Reference(_type4), "cisco_type8": _Reference(_type8), "cisco_type9": _Reference(_type9), "cisco_asa_pbkdf2": _Reference(_asa_pbkdf2),}
def main(path): failures = 0 checked = 0 with open(path) as f: for line in f: scheme, password, user, realm, hash = line.rstrip("\n").split("\t") password = bytes.fromhex(password) hash = bytes.fromhex(hash).decode("utf-8") handler = REFERENCE.get(scheme) or get_crypt_handler(scheme) context = set(getattr(handler, "context_kwds", ())) kwds = {} if "user" in context: kwds["user"] = user if "realm" in context: kwds["realm"] = realm try: ok = handler.verify(password, hash, **kwds) except Exception as e: ok = "%s: %s" % (type(e).__name__, e) want = scheme not in DISABLED checked += 1 if ok is not want: failures += 1 print("%s: passlib says %r for %r, password %r" % (scheme, ok, hash, password)) continue # A leading byte every scheme sees: it shifts the eight bytes DES # crypt reads and the 72 bcrypt reads, lmhash's upper-casing # leaves it alone, and it is printable, which SASLprep insists # on. wrong = b"Q" + password try: bad = handler.verify(wrong, hash, **kwds) except Exception as e: bad = "%s: %s" % (type(e).__name__, e) if bad is not False: failures += 1 print("%s: passlib says %r for %r, wrong password %r" % (scheme, bad, hash, wrong)) print("%d hashes checked, %d failures" % (checked, failures)) return 1 if failures else 0
if __name__ == "__main__": sys.exit(main(sys.argv[1]))