FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

_hashlib.HMAC and Update hmac from v3.14.3 by youknowone · Pull Request #7159 · RustPython/RustPython · GitHub

_hashlib.HMAC and Update hmac from v3.14.3 - #7159

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:hashlib
Feb 15, 2026
Merged

_hashlib.HMAC and Update hmac from v3.14.3#7159
youknowone merged 2 commits into
RustPython:mainfrom
youknowone:hashlib

Conversation

youknowone commented Feb 15, 2026
edited by coderabbitai Bot
Loading

Copy link
Copy Markdown
Member

close #7122

Summary by CodeRabbit

Release Notes

  • New Features

    • Added comprehensive HMAC (Hash-based Message Authentication Code) support for all supported hash algorithms.
    • HMAC operations now include update, digest, hexdigest, and copy functionality.
  • Improvements

    • Enhanced error handling for invalid digest algorithms and missing configuration during HMAC creation.

coderabbitai Bot commented Feb 15, 2026
edited
Loading

Copy link
Copy Markdown
Contributor
📝 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

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 ⚠️ Warning 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 using DynClone for consistency with ThreadSafeDynDigest.

The file already imports dyn_clone::{DynClone, clone_trait_object} (line 24) and uses them for ThreadSafeDynDigest (line 876). DynHmac manually re-implements the same pattern with dyn_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), replace self.ctx.read().dyn_clone() with self.ctx.read().clone() (which clone_trait_object! provides for Box<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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

youknowone marked this pull request as ready for review February 15, 2026 13:09

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/hmac.py
[ ] test: cpython/Lib/test/test_hmac.py (TODO: 14)

dependencies:

  • hmac

dependent tests: (4 tests)

  • hmac: test_hmac test_smtplib
    • secrets: test_secrets
    • smtplib: test_smtpnet

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

youknowone merged commit b4edd54 into RustPython:main Feb 15, 2026
14 checks passed
youknowone deleted the hashlib branch February 15, 2026 14:38
youknowone added a commit to youknowone/RustPython that referenced this pull request Mar 22, 2026
_hashlib.HMAC and Update hmac from v3.14.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL