diff options
author | Neal H. Walfield <neal@pep.foundation> | 2022-12-11 08:52:44 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2022-12-11 23:13:19 +0100 |
commit | 20c87912844bc324b4f5bbc0783aeaf34f6d373b (patch) | |
tree | 875ed0e3b95065c553f07ed2da349aacfb33fafd | |
parent | 750edf459c4304695befed32d9315d4af5c8a3f5 (diff) |
openpgp: Move HashingMode.
- `HashingMode` is mostly used by `HashedReader`.
- Move the `HashingMode` declaration and implementation from
`parse.rs` to `parse/hashed_reader.rs`.
-rw-r--r-- | openpgp/src/parse.rs | 89 | ||||
-rw-r--r-- | openpgp/src/parse/hashed_reader.rs | 92 |
2 files changed, 92 insertions, 89 deletions
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 9c365420..a34a880b 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -250,7 +250,11 @@ mod packet_pile_parser; pub use self::packet_pile_parser::PacketPileParser; mod hashed_reader; -pub(crate) use self::hashed_reader::{HashedReader, hash_update_text}; +pub(crate) use self::hashed_reader::{ + HashingMode, + HashedReader, + hash_update_text, +}; mod packet_parser_builder; pub use self::packet_parser_builder::{Dearmor, PacketParserBuilder}; @@ -740,89 +744,6 @@ pub(crate) struct SignatureGroup { pub(crate) hashes: Vec<HashingMode<Box<dyn crypto::hash::Digest>>>, } -/// Controls line-ending normalization during hashing. -/// -/// OpenPGP normalizes line endings when signing or verifying text -/// signatures. -pub(crate) enum HashingMode<T> { - /// Hash for a binary signature. - /// - /// The data is hashed as-is. - Binary(T), - - /// Hash for a text signature. - /// - /// The data is hashed with line endings normalized to `\r\n`. - Text(T), -} - -impl<T: Clone> Clone for HashingMode<T> { - fn clone(&self) -> Self { - use self::HashingMode::*; - match self { - Binary(t) => Binary(t.clone()), - Text(t) => Text(t.clone()), - } - } -} - -impl<T: std::fmt::Debug> std::fmt::Debug for HashingMode<T> { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - use self::HashingMode::*; - match self { - Binary(t) => write!(f, "Binary({:?})", t), - Text(t) => write!(f, "Text({:?})", t), - } - } -} - -impl<T: PartialEq> PartialEq for HashingMode<T> { - fn eq(&self, other: &Self) -> bool { - use self::HashingMode::*; - match (self, other) { - (Binary(s), Binary(o)) => s.eq(o), - (Text(s), Text(o)) => s.eq(o), - _ => false, - } - } -} - -impl<T: Eq> Eq for HashingMode<T> { } - -impl<T> HashingMode<T> { - fn map<U, F: Fn(&T) -> U>(&self, f: F) -> HashingMode<U> { - use self::HashingMode::*; - match self { - Binary(t) => Binary(f(t)), - Text(t) => Text(f(t)), - } - } - - pub(crate) fn as_ref(&self) -> &T { - use self::HashingMode::*; - match self { - Binary(t) => t, - Text(t) => t, - } - } - - pub(crate) fn as_mut(&mut self) -> &mut T { - use self::HashingMode::*; - match self { - Binary(t) => t, - Text(t) => t, - } - } - - fn for_signature(t: T, typ: SignatureType) -> Self { - if typ == SignatureType::Text { - HashingMode::Text(t) - } else { - HashingMode::Binary(t) - } - } -} - impl fmt::Debug for SignatureGroup { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let algos = self.hashes.iter().map(|mode| mode.map(|ctx| ctx.algo())) diff --git a/openpgp/src/parse/hashed_reader.rs b/openpgp/src/parse/hashed_reader.rs index 00f56427..e5d93023 100644 --- a/openpgp/src/parse/hashed_reader.rs +++ b/openpgp/src/parse/hashed_reader.rs @@ -6,14 +6,96 @@ use std::fmt; use buffered_reader::BufferedReader; use buffered_reader::buffered_reader_generic_read_impl; -use crate::{ - Result, - types::HashAlgorithm, -}; -use crate::parse::{Cookie, HashesFor, Hashing, HashingMode}; +use crate::Result; +use crate::parse::{Cookie, HashesFor, Hashing}; +use crate::types::HashAlgorithm; +use crate::types::SignatureType; const TRACE : bool = false; +/// Controls line-ending normalization during hashing. +/// +/// OpenPGP normalizes line endings when signing or verifying text +/// signatures. +pub(crate) enum HashingMode<T> { + /// Hash for a binary signature. + /// + /// The data is hashed as-is. + Binary(T), + + /// Hash for a text signature. + /// + /// The data is hashed with line endings normalized to `\r\n`. + Text(T), +} + +impl<T: Clone> Clone for HashingMode<T> { + fn clone(&self) -> Self { + use self::HashingMode::*; + match self { + Binary(t) => Binary(t.clone()), + Text(t) => Text(t.clone()), + } + } +} + +impl<T: std::fmt::Debug> std::fmt::Debug for HashingMode<T> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + use self::HashingMode::*; + match self { + Binary(t) => write!(f, "Binary({:?})", t), + Text(t) => write!(f, "Text({:?})", t), + } + } +} + +impl<T: PartialEq> PartialEq for HashingMode<T> { + fn eq(&self, other: &Self) -> bool { + use self::HashingMode::*; + match (self, other) { + (Binary(s), Binary(o)) => s.eq(o), + (Text(s), Text(o)) => s.eq(o), + _ => false, + } + } +} + +impl<T: Eq> Eq for HashingMode<T> { } + +impl<T> HashingMode<T> { + pub(crate) fn map<U, F: Fn(&T) -> U>(&self, f: F) -> HashingMode<U> { + use self::HashingMode::*; + match self { + Binary(t) => Binary(f(t)), + Text(t) => Text(f(t)), + } + } + + pub(crate) fn as_ref(&self) -> &T { + use self::HashingMode::*; + match self { + Binary(t) => t, + Text(t) => t, + } + } + + pub(crate) fn as_mut(&mut self) -> &mut T { + use self::HashingMode::*; + match self { + Binary(t) => t, + Text(t) => t, + } + } + + pub(crate) fn for_signature(t: T, typ: SignatureType) -> Self { + if typ == SignatureType::Text { + HashingMode::Text(t) + } else { + HashingMode::Binary(t) + } + } +} + pub(crate) struct HashedReader<R: BufferedReader<Cookie>> { reader: R, cookie: Cookie, |