diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-01-02 19:05:38 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-01-02 22:41:02 +0100 |
commit | 759d57e79738f62d83ff852d7390279096688437 (patch) | |
tree | c366f993f1530cb6fac5f6926a4c351b582d178b | |
parent | ba17174e9482503c089ff9d6deab96c0f1168022 (diff) |
Fix: Do not assume UTF8 encoding of data to hash
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/domain/libimagmail/src/hasher.rs | 2 | ||||
-rw-r--r-- | lib/entry/libimagentryref/src/hasher.rs | 17 |
2 files changed, 15 insertions, 4 deletions
diff --git a/lib/domain/libimagmail/src/hasher.rs b/lib/domain/libimagmail/src/hasher.rs index f6860297..da7170b7 100644 --- a/lib/domain/libimagmail/src/hasher.rs +++ b/lib/domain/libimagmail/src/hasher.rs @@ -50,6 +50,6 @@ impl Hasher for MailHasher { path_str.push_str(&message_id); - Ok(Sha1Hasher::sha1_hash(&path_str)) + Ok(Sha1Hasher::sha1_hash(path_str.as_bytes())) } } diff --git a/lib/entry/libimagentryref/src/hasher.rs b/lib/entry/libimagentryref/src/hasher.rs index e18e5c15..de254d20 100644 --- a/lib/entry/libimagentryref/src/hasher.rs +++ b/lib/entry/libimagentryref/src/hasher.rs @@ -34,6 +34,8 @@ pub mod default { pub mod sha1 { use std::path::Path; + use std::fs::OpenOptions; + use std::io::Read; use failure::Fallible as Result; use sha1::{Sha1, Digest}; @@ -43,8 +45,8 @@ pub mod sha1 { pub struct Sha1Hasher; impl Sha1Hasher { - pub fn sha1_hash(s: &str) -> String { - format!("{:x}", Sha1::digest(s.as_bytes())) // TODO: Ugh... + pub fn sha1_hash(bytes: &[u8]) -> String { + format!("{:x}", Sha1::digest(bytes)) // TODO: Ugh... } } @@ -52,7 +54,16 @@ pub mod sha1 { const NAME : &'static str = "sha1"; fn hash<P: AsRef<Path>>(path: P) -> Result<String> { - Ok(Sha1Hasher::sha1_hash(&::std::fs::read_to_string(path)?)) + let buffer = { + let mut buffer = Vec::with_capacity(4096); // allocate new buffer with 4 KB space + OpenOptions::new() + .read(true) + .open(path.as_ref())? + .read_to_end(&mut buffer)?; + buffer + }; + + Ok(Sha1Hasher::sha1_hash(&buffer)) } } |