Implement `hashlib`
Merged
SA
2mo ago+3794+3794+0123456789012345678901234567890123456789-81-81-01234567890123456789
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/sha2crates (0.11) for hardware-accelerated compression; the other cores are hand-written, andblake2b/blake2sexpose 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 aDUMP_VERSIONbump; a test snapshots the serialized state so the change fails visibly. The fixed digest lives on the stack so an HMAC round allocates nothing, andupdate()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. SHAKEdigest()/hexdigest()length errors follow_sha3's messages, through aFromValueforu64withunsigned_longsemantics. - 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()andscrypt()remain unimplemented. - Allows
style = defwithvarargsinFromArgs, needed byhashlib.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
longwidth or the OpenSSL build, including the BLAKE2leaf_sizeand PBKDF2dklenbounds; the limitations page records the difference.