diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-10-17 12:32:10 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-10-23 16:24:21 +0200 |
commit | 7b0a6fde9a8f65c16d5f49ac77afc27ece0c7333 (patch) | |
tree | 2bc52a0a4db60bfc8a1bc99369c4a15194ac34a4 | |
parent | 21a78e6f85aa29d3e2bbad1e2a78d6e20ee82d25 (diff) |
openpgp: Move the hash_file(..) function to openpgp::crypto.
-rw-r--r-- | openpgp/src/crypto/mod.rs | 63 | ||||
-rw-r--r-- | openpgp/src/lib.rs | 59 | ||||
-rw-r--r-- | tool/src/sqv.rs | 2 |
3 files changed, 64 insertions, 60 deletions
diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index d8292b36..dca4389b 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -1,8 +1,71 @@ //! Cryptographic primitives. +use std::io::Read; +use nettle::Hash; + +use constants::HashAlgorithm; +use Result; + pub(crate) mod aead; pub(crate) mod ecdh; mod hash; pub mod mpis; pub mod s2k; pub(crate) mod symmetric; + +/// Hash the specified file. +/// +/// This is useful when verifying detached signatures. +pub fn hash_file<R: Read>(reader: R, algos: &[HashAlgorithm]) + -> Result<Vec<(HashAlgorithm, Box<Hash>)>> +{ + use std::mem; + + use ::parse::HashedReader; + use ::parse::HashesFor; + + use buffered_reader::BufferedReader; + use buffered_reader::BufferedReaderGeneric; + + let reader + = BufferedReaderGeneric::with_cookie( + reader, None, Default::default()); + + let mut reader + = HashedReader::new(reader, HashesFor::Signature, algos.to_vec()); + + // Hash all of the data. + reader.drop_eof()?; + + let mut hashes = + mem::replace(&mut reader.cookie_mut().sig_group_mut().hashes, + Default::default()); + let hashes = hashes.drain().collect(); + Ok(hashes) +} + + +#[test] +fn hash_file_test() { + use std::collections::HashMap; + use std::fs::File; + + let expected: HashMap<HashAlgorithm, &str> = [ + (HashAlgorithm::SHA1, "7945E3DA269C25C04F9EF435A5C0F25D9662C771"), + (HashAlgorithm::SHA512, "DDE60DB05C3958AF1E576CD006A7F3D2C343DD8C8DECE789A15D148DF90E6E0D1454DE734F8343502CA93759F22C8F6221BE35B6BDE9728BD12D289122437CB1"), + ].iter().cloned().collect(); + + let result = + hash_file(File::open(::path_to("a-cypherpunks-manifesto.txt")).unwrap(), + &expected.keys().cloned().collect::<Vec<HashAlgorithm>>()) + .unwrap(); + + for (algo, mut hash) in result.into_iter() { + let mut digest = vec![0u8; hash.digest_size()]; + hash.digest(&mut digest); + + assert_eq!(*expected.get(&algo).unwrap(), + &::conversions::to_hex(&digest[..], false)); + } +} + diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 0df9ff30..4431b788 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -574,65 +574,6 @@ impl Drop for Password { } } } - -use std::io::Read; -use nettle::Hash; - -/// Hash the specified file. -/// -/// This is useful when verifying detached signatures. -pub fn hash_file<R: Read>(reader: R, algos: &[HashAlgorithm]) - -> Result<Vec<(HashAlgorithm, Box<Hash>)>> -{ - use std::mem; - - use ::parse::HashedReader; - use ::parse::HashesFor; - - use buffered_reader::BufferedReader; - use buffered_reader::BufferedReaderGeneric; - - let reader - = BufferedReaderGeneric::with_cookie( - reader, None, Default::default()); - - let mut reader - = HashedReader::new(reader, HashesFor::Signature, algos.to_vec()); - - // Hash all of the data. - reader.drop_eof()?; - - let mut hashes = - mem::replace(&mut reader.cookie_mut().sig_group_mut().hashes, - Default::default()); - let hashes = hashes.drain().collect(); - Ok(hashes) -} - - -#[test] -fn hash_file_test() { - use std::collections::HashMap; - use std::fs::File; - - let expected: HashMap<HashAlgorithm, &str> = [ - (HashAlgorithm::SHA1, "7945E3DA269C25C04F9EF435A5C0F25D9662C771"), - (HashAlgorithm::SHA512, "DDE60DB05C3958AF1E576CD006A7F3D2C343DD8C8DECE789A15D148DF90E6E0D1454DE734F8343502CA93759F22C8F6221BE35B6BDE9728BD12D289122437CB1"), - ].iter().cloned().collect(); - - let result = - hash_file(File::open(path_to("a-cypherpunks-manifesto.txt")).unwrap(), - &expected.keys().cloned().collect::<Vec<HashAlgorithm>>()) - .unwrap(); - - for (algo, mut hash) in result.into_iter() { - let mut digest = vec![0u8; hash.digest_size()]; - hash.digest(&mut digest); - - assert_eq!(*expected.get(&algo).unwrap(), - &::conversions::to_hex(&digest[..], false)); - } -} /// Time-constant comparison. fn secure_eq(a: &[u8], b: &[u8]) -> bool { diff --git a/tool/src/sqv.rs b/tool/src/sqv.rs index 305c0c2d..93a7254b 100644 --- a/tool/src/sqv.rs +++ b/tool/src/sqv.rs @@ -141,7 +141,7 @@ fn real_main() -> Result<(), failure::Error> { let hash_algos : Vec<HashAlgorithm> = sigs.iter().map(|&(ref sig, _, _)| sig.hash_algo()).collect(); let hashes: HashMap<_, _> = - openpgp::hash_file(File::open(file)?, &hash_algos[..])? + openpgp::crypto::hash_file(File::open(file)?, &hash_algos[..])? .into_iter().collect(); fn tpk_has_key(tpk: &TPK, keyid: &KeyID) -> bool { |