summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-02 19:05:38 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-02 22:41:02 +0100
commit759d57e79738f62d83ff852d7390279096688437 (patch)
treec366f993f1530cb6fac5f6926a4c351b582d178b
parentba17174e9482503c089ff9d6deab96c0f1168022 (diff)
downloadimag-759d57e79738f62d83ff852d7390279096688437.zip
imag-759d57e79738f62d83ff852d7390279096688437.tar.gz
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.rs2
-rw-r--r--lib/entry/libimagentryref/src/hasher.rs17
2 files changed, 15 insertions, 4 deletions
diff --git a/lib/domain/libimagmail/src/hasher.rs b/lib/domain/libimagmail/src/hasher.rs
index f686029..da7170b 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 e18e5c1..de254d2 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))
}
}