From 6649ccd0e524959296654533e357a7621ab951de Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 24 Apr 2023 21:54:44 -0600 Subject: [PATCH] Impl `core::hash::Hash` for `RsaPrivateKey` Adds an impl which hashes only the public key components, along with a domain separator string (`RsaPrivateKey`). Closes #165 --- src/key.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/key.rs b/src/key.rs index a1fa6c5d..fe2bfcaf 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1,5 +1,8 @@ use alloc::vec::Vec; -use core::ops::Deref; +use core::{ + hash::{Hash, Hasher}, + ops::Deref, +}; use num_bigint::traits::ModInverse; use num_bigint::Sign::Plus; use num_bigint::{BigInt, BigUint}; @@ -39,6 +42,7 @@ pub struct RsaPrivateKey { pub(crate) precomputed: Option, } +impl Eq for RsaPrivateKey {} impl PartialEq for RsaPrivateKey { #[inline] fn eq(&self, other: &RsaPrivateKey) -> bool { @@ -48,7 +52,13 @@ impl PartialEq for RsaPrivateKey { } } -impl Eq for RsaPrivateKey {} +impl Hash for RsaPrivateKey { + fn hash(&self, state: &mut H) -> () { + // Domain separator for RSA private keys + state.write(b"RsaPrivateKey"); + Hash::hash(&self.pubkey_components, state); + } +} impl Zeroize for RsaPrivateKey { fn zeroize(&mut self) {