diff options
-rw-r--r-- | lib/domain/libimagmail/src/hasher.rs | 20 | ||||
-rw-r--r-- | lib/entry/libimagentryref/src/hasher.rs | 17 |
2 files changed, 18 insertions, 19 deletions
diff --git a/lib/domain/libimagmail/src/hasher.rs b/lib/domain/libimagmail/src/hasher.rs index f6860297..2ecbc7b1 100644 --- a/lib/domain/libimagmail/src/hasher.rs +++ b/lib/domain/libimagmail/src/hasher.rs @@ -22,34 +22,22 @@ use std::path::Path; use failure::Fallible as Result; use libimagentryref::hasher::Hasher; -use libimagerror::errors::ErrorMsg; -use libimagentryref::hasher::sha1::Sha1Hasher; pub struct MailHasher; impl Hasher for MailHasher { const NAME: &'static str = "MailHasher"; - /// hash the file at path `path` + /// Hash the Messgae-ID /// - /// We create a sha1 over the path of the file (which is NOT safe, because mails can move) and - /// the Message-ID of the mail itself. + /// We create a sha1 over the Message-ID of the mail itself. As a Message-ID should be unique, + /// this can be considered save. /// /// # TODO: Fix /// /// The file name is not constant with mail files, because flags are encoded in the filename. /// The path is not constant with mail files, because they can be moved between boxes. fn hash<P: AsRef<Path>>(path: P) -> Result<String> { - let mut path_str = path - .as_ref() - .to_str() - .map(String::from) - .ok_or_else(|| ErrorMsg::UTF8Error)?; - - let message_id : String = crate::util::get_message_id_for_mailfile(path)?.into(); - - path_str.push_str(&message_id); - - Ok(Sha1Hasher::sha1_hash(&path_str)) + crate::util::get_message_id_for_mailfile(path).map(Into::into) } } 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)) } } |