summaryrefslogtreecommitdiffstats
path: root/src/package/source.rs
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-01-12 10:16:20 +0100
committerMatthias Beyer <matthias.beyer@atos.net>2021-01-12 10:17:43 +0100
commit18b256e040881ac674463913b2a7e290125ea738 (patch)
treee1c956a9b12ff6195675ae7611dedf1a2dcdecf0 /src/package/source.rs
parent63f705792e1e5abf6b8fe9211a10815d3478e21a (diff)
Reimplement hash verification
This patch re-implements hashing using the "ring" crypto library and implementing a streaming hashing with it. This way, we stream the file to the hasher rather than reading the full file to memory. Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
Diffstat (limited to 'src/package/source.rs')
-rw-r--r--src/package/source.rs45
1 files changed, 21 insertions, 24 deletions
diff --git a/src/package/source.rs b/src/package/source.rs
index 4d97365..4f8994b 100644
--- a/src/package/source.rs
+++ b/src/package/source.rs
@@ -1,3 +1,5 @@
+use std::io::Read;
+
use anyhow::Result;
use anyhow::anyhow;
use getset::Getters;
@@ -33,9 +35,9 @@ pub struct SourceHash {
}
impl SourceHash {
- pub fn matches_hash_of(&self, buf: &[u8]) -> Result<()> {
+ pub fn matches_hash_of<R: Read>(&self, reader: R) -> Result<()> {
trace!("Hashing buffer with: {:?}", self.hashtype);
- let h = self.hashtype.hash_buffer(&buf)?;
+ let h = self.hashtype.hash_from_reader(reader)?;
trace!("Hashing buffer with: {} finished", self.hashtype);
if h == self.value {
@@ -70,29 +72,24 @@ pub enum HashType {
}
impl HashType {
- fn hash_buffer(&self, buffer: &[u8]) -> Result<HashValue> {
- match self {
- HashType::Sha1 => {
- trace!("SHA1 hashing buffer");
- let mut m = sha1::Sha1::new();
- m.update(buffer);
- Ok(HashValue(m.digest().to_string()))
- },
- HashType::Sha256 => {
- trace!("SHA256 hashing buffer");
- //let mut m = sha2::Sha256::new();
- //m.update(buffer);
- //Ok(HashValue(String::from(m.finalize())))
- unimplemented!()
- },
- HashType::Sha512 => {
- trace!("SHA512 hashing buffer");
- //let mut m = sha2::Sha512::new();
- //m.update(buffer);
- //Ok(HashValue(String::from(m.finalize())))
- unimplemented!()
- },
+ fn hash_from_reader<R: Read>(&self, mut reader: R) -> Result<HashValue> {
+ use ring::digest::{Context, SHA1_FOR_LEGACY_USE_ONLY, SHA256, SHA512};
+ let mut context = match self {
+ HashType::Sha1 => Context::new(&SHA1_FOR_LEGACY_USE_ONLY),
+ HashType::Sha256 => Context::new(&SHA256),
+ HashType::Sha512 => Context::new(&SHA512),
+ };
+ let mut buffer = [0; 1024];
+
+ loop {
+ let count = reader.read(&mut buffer)?;
+ if count == 0 {
+ break;
+ }
+ context.update(&buffer[..count]);
}
+
+ Ok(HashValue(data_encoding::HEXLOWER.encode(context.finish().as_ref())))
}
}