_hashlib.HMAC and Update hmac from v3.14.3 by youknowone · Pull Request #7159 · RustPython/RustPython
📝 Walkthrough
Walkthrough
RustPython's hashlib module now supports HMAC operations through a type-erased dispatch pattern. The changes introduce a new PyHmac class wrapping algorithm metadata and a thread-safe context implementing standard HMAC methods (update, digest, hexdigest, copy). The implementation uses a DynHmac trait with TypedHmac<D> concrete wrappers to handle multiple hash algorithms uniformly.
Changes
| Cohort / File(s) | Summary |
|---|---|
HMAC Core Implementation crates/stdlib/src/hashlib.rs |
Introduces PyHmac struct with algorithm metadata and type-erased HMAC context; adds DynHmac trait for object-safe dispatch and TypedHmac<D> wrapper for concrete implementations. Refactors hmac_new to return PyHmac instead of PyObjectRef, adds resolve_digestmod function for algorithm resolution, updates NewHMACHashArgs.msg field signature, and expands error handling for missing/invalid digestmod and unknown algorithms. |
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
- Update hashlib from v3.14.3 and align _hashlib to CPython #7120: Modifies the same
crates/stdlib/src/hashlib.rsfile to add/implement HMAC support with overlapping changes onhmac_new,resolve_digestmod, and HMAC creation/dispatch logic.
Poem
🐰 Hops through HMAC traits with glee,
Type erasure set our messages free,
DynHmac bounces, TypedHmac springs,
Secure digests on cryptographic wings! ✨
🚥 Pre-merge checks | ✅ 5 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title clearly describes the main changes: introducing _hashlib.HMAC and updating hmac to v3.14.3, which directly matches the PR's core objectives. |
| Linked Issues check | ✅ Passed | The code implements _hashlib.HMAC with proper algorithm support and error handling as required by #7122's upgrade to CPython v3.14.3, enabling proper HMAC functionality. |
| Out of Scope Changes check | ✅ Passed | All changes are scoped to hashlib.rs HMAC implementation, directly supporting the CPython v3.14.3 upgrade objective without unrelated modifications. |
| Merge Conflict Detection | ✅ Passed | ✅ No merge conflicts detected when merging into main |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
No actionable comments were generated in the recent review. 🎉
🧹 Recent nitpick comments
crates/stdlib/src/hashlib.rs (1)
261-285: Consider usingDynClonefor consistency withThreadSafeDynDigest.The file already imports
dyn_clone::{DynClone, clone_trait_object}(line 24) and uses them forThreadSafeDynDigest(line 876).DynHmacmanually re-implements the same pattern withdyn_clone(). Unifying on the crate-provided mechanism removes boilerplate and keeps the two trait-object hierarchies consistent.♻️ Suggested refactor
- trait DynHmac: Send + Sync { + trait DynHmac: DynClone + Send + Sync { fn dyn_update(&mut self, data: &[u8]); fn dyn_finalize(&self) -> Vec<u8>; - fn dyn_clone(&self) -> Box<dyn DynHmac>; } + clone_trait_object!(DynHmac); struct TypedHmac<D>(D); impl<D> DynHmac for TypedHmac<D> where D: Mac + Clone + Send + Sync + 'static, { fn dyn_update(&mut self, data: &[u8]) { Mac::update(&mut self.0, data); } fn dyn_finalize(&self) -> Vec<u8> { self.0.clone().finalize().into_bytes().to_vec() } - - fn dyn_clone(&self) -> Box<dyn DynHmac> { - Box::new(TypedHmac(self.0.clone())) - } }Then in
PyHmac::copy(line 346), replaceself.ctx.read().dyn_clone()withself.ctx.read().clone()(whichclone_trait_object!provides forBox<dyn DynHmac>).
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.