summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto/backend/cng/hash.rs
diff options
context:
space:
mode:
Diffstat (limited to 'openpgp/src/crypto/backend/cng/hash.rs')
-rw-r--r--openpgp/src/crypto/backend/cng/hash.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/openpgp/src/crypto/backend/cng/hash.rs b/openpgp/src/crypto/backend/cng/hash.rs
index 3c797797..1f58d4cc 100644
--- a/openpgp/src/crypto/backend/cng/hash.rs
+++ b/openpgp/src/crypto/backend/cng/hash.rs
@@ -1,4 +1,4 @@
-use core::convert::TryFrom;
+use core::convert::{TryFrom, TryInto};
use std::sync::Mutex;
use crate::crypto::hash::Digest;
@@ -22,6 +22,13 @@ impl Clone for Hash {
}
impl Digest for Hash {
+ fn algo(&self) -> HashAlgorithm {
+ self.0.lock().expect("Mutex not to be poisoned")
+ .hash_algorithm().expect("CNG to not fail internally")
+ .try_into()
+ .expect("We created the object, algo is representable")
+ }
+
fn digest_size(&self) -> usize {
self.0.lock().expect("Mutex not to be poisoned")
.hash_size().expect("CNG to not fail internally")
@@ -69,6 +76,22 @@ impl TryFrom<HashAlgorithm> for cng::HashAlgorithmId {
}
}
+impl TryFrom<cng::HashAlgorithmId> for HashAlgorithm {
+ type Error = Error;
+
+ fn try_from(value: cng::HashAlgorithmId) -> std::result::Result<Self, Self::Error> {
+ Ok(match value {
+ cng::HashAlgorithmId::Sha1 => HashAlgorithm::SHA1,
+ cng::HashAlgorithmId::Sha256 => HashAlgorithm::SHA256,
+ cng::HashAlgorithmId::Sha384 => HashAlgorithm::SHA384,
+ cng::HashAlgorithmId::Sha512 => HashAlgorithm::SHA512,
+ cng::HashAlgorithmId::Md5 => HashAlgorithm::MD5,
+ algo => Err(Error::InvalidArgument(
+ format!("Algorithm {:?} not representable", algo)))?,
+ })
+ }
+}
+
impl HashAlgorithm {
/// Whether Sequoia supports this algorithm.
pub fn is_supported(self) -> bool {