summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-09-10 12:42:15 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-09-10 13:33:02 +0200
commit4596b9359b51a2d7922c73e46f4b79b99cc46481 (patch)
treea6d98469222146ca5e7096829ce03fc657f2ea87 /openpgp/src/crypto
parent41860175721697c1050c5449bd4153c5663c0232 (diff)
openpgp: Keep track of the hash algorithm in hash context.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r--openpgp/src/crypto/hash.rs23
1 files changed, 17 insertions, 6 deletions
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs
index ff2ba2e8..34189653 100644
--- a/openpgp/src/crypto/hash.rs
+++ b/openpgp/src/crypto/hash.rs
@@ -23,17 +23,25 @@ const DUMP_HASHED_VALUES: Option<&str> = None;
/// State of a hash function.
#[derive(Clone)]
-pub struct Context(Box<nettle::Hash>);
+pub struct Context {
+ algo: HashAlgorithm,
+ ctx: Box<nettle::Hash>,
+}
impl Context {
+ /// Returns the algorithm.
+ pub fn algo(&self) -> HashAlgorithm {
+ self.algo
+ }
+
/// Size of the digest in bytes
pub fn digest_size(&self) -> usize {
- self.0.digest_size()
+ self.ctx.digest_size()
}
/// Writes data into the hash function.
pub fn update<D: AsRef<[u8]>>(&mut self, data: D) {
- self.0.update(data.as_ref());
+ self.ctx.update(data.as_ref());
}
/// Finalizes the hash function and writes the digest into the
@@ -44,7 +52,7 @@ impl Context {
/// `digest` must be at least `self.digest_size()` bytes large,
/// otherwise the digest will be truncated.
pub fn digest<D: AsMut<[u8]>>(&mut self, mut digest: D) {
- self.0.digest(digest.as_mut());
+ self.ctx.digest(digest.as_mut());
}
}
@@ -102,10 +110,13 @@ impl HashAlgorithm {
if let Some(prefix) = DUMP_HASHED_VALUES {
c.map(|c: Box<nettle::Hash>| {
- Context(Box::new(HashDumper::new(c, prefix)))
+ Context {
+ algo: self,
+ ctx: Box::new(HashDumper::new(c, prefix)),
+ }
})
} else {
- c.map(|c| Context(c))
+ c.map(|c| Context { algo: self, ctx: c })
}
}