Skip to content

Commit c012868

Browse files
authored
Impl core::hash::Hash for RsaPrivateKey (#308)
Adds an impl which hashes only the public key components, along with a domain separator string (`RsaPrivateKey`). Closes #165
1 parent faabaa7 commit c012868

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/key.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use alloc::vec::Vec;
2-
use core::ops::Deref;
2+
use core::{
3+
hash::{Hash, Hasher},
4+
ops::Deref,
5+
};
36
use num_bigint::traits::ModInverse;
47
use num_bigint::Sign::Plus;
58
use num_bigint::{BigInt, BigUint};
@@ -40,6 +43,7 @@ pub struct RsaPrivateKey {
4043
pub(crate) precomputed: Option<PrecomputedValues>,
4144
}
4245

46+
impl Eq for RsaPrivateKey {}
4347
impl PartialEq for RsaPrivateKey {
4448
#[inline]
4549
fn eq(&self, other: &RsaPrivateKey) -> bool {
@@ -49,7 +53,13 @@ impl PartialEq for RsaPrivateKey {
4953
}
5054
}
5155

52-
impl Eq for RsaPrivateKey {}
56+
impl Hash for RsaPrivateKey {
57+
fn hash<H: Hasher>(&self, state: &mut H) -> () {
58+
// Domain separator for RSA private keys
59+
state.write(b"RsaPrivateKey");
60+
Hash::hash(&self.pubkey_components, state);
61+
}
62+
}
5363

5464
impl Zeroize for RsaPrivateKey {
5565
fn zeroize(&mut self) {

0 commit comments

Comments
 (0)