Implement `hashlib`

Merged
SA
2mo ago
+3794-81

Summary by cubic

Implements the full hashlib module: every guaranteed algorithm (md5, sha1, sha2, sha3, shake_128/256, blake2b/blake2s), plus new(), pbkdf2_hmac(), and the algorithms_* sets. Hash objects are CPython-compatible in types, attributes, reprs, and error messages, and a hash mid-stream survives a session dump.

  • SHA-1 and SHA-2 are backed by the RustCrypto sha1/sha2 crates (0.11) for hardware-accelerated compression; the other cores are hand-written, and blake2b/blake2s expose the full BLAKE2 parameter block and size constants.
  • SHA-1/SHA-2 state is persisted through the crate's SerializableState, so a crate upgrade that changes that layout is a DUMP_VERSION bump; a test snapshots the serialized state so the change fails visibly. The fixed digest lives on the stack so an HMAC round allocates nothing, and update() lifts the state out of the heap instead of cloning it.
  • SHAKE output lengths and PBKDF2 iteration counts are bounded by the resource tracker; hexdigest() also counts the live digest it keeps while encoding, and PBKDF2 absorbs the salt into the keyed HMAC state once. SHAKE digest()/hexdigest() length errors follow _sha3's messages, through a FromValue for u64 with unsigned_long semantics.
  • Hashing polls the deadline once per 64 KiB of input, so a large update() can be interrupted.
  • Adds type stubs with new() overloaded by name, plus a limitations docs page; file_digest() and scrypt() remain unimplemented.
  • Allows style = def with varargs in FromArgs, needed by hashlib.new(name, *args, **kwargs), and fixes typed-argument fallback errors to name 'NoneType' instead of 'None'.
  • Error-path tests accept either wording where CPython's messages depend on the C long width or the OpenSSL build, including the BLAKE2 leaf_size and PBKDF2 dklen bounds; the limitations page records the difference.