summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-02-22 11:34:26 +0100
committerMatthias Beyer <mail@beyermatthias.de>2019-02-22 11:34:26 +0100
commitac914b27e24f6fbe9e1737bc0348197fec5688d9 (patch)
tree236ad10598af8bd0f7e7a29bffa0617d13a8e16f
parent211d6fa60bb2c306ea674b9fb37c11ca9b19b8cc (diff)
parent596af2fc76c2bd03fa9217e26c4ffd711b961b8a (diff)
downloadimag-ac914b27e24f6fbe9e1737bc0348197fec5688d9.zip
imag-ac914b27e24f6fbe9e1737bc0348197fec5688d9.tar.gz
Merge branch 'libimagmail-better-hash' into master
This merges improvements for the libimagmail hasher. Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/domain/libimagmail/src/hasher.rs23
-rw-r--r--lib/entry/libimagentryref/src/hasher.rs9
2 files changed, 26 insertions, 6 deletions
diff --git a/lib/domain/libimagmail/src/hasher.rs b/lib/domain/libimagmail/src/hasher.rs
index 270b177..3d6dd59 100644
--- a/lib/domain/libimagmail/src/hasher.rs
+++ b/lib/domain/libimagmail/src/hasher.rs
@@ -22,6 +22,8 @@ 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;
@@ -30,11 +32,24 @@ impl Hasher for MailHasher {
/// hash the file at path `path`
///
- /// TODO: This is the expensive implementation. We use the message Id as hash, which is
- /// convenient and _should_ be safe
+ /// 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.
///
- /// TODO: Confirm that this approach is right
+ /// # 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> {
- ::util::get_message_id_for_mailfile(path)
+ let mut path_str = path
+ .as_ref()
+ .to_str()
+ .map(String::from)
+ .ok_or_else(|| ErrorMsg::UTF8Error)?;
+
+ let message_id = ::util::get_message_id_for_mailfile(path)?;
+
+ path_str.push_str(&message_id);
+
+ Ok(Sha1Hasher::sha1_hash(&path_str))
}
}
diff --git a/lib/entry/libimagentryref/src/hasher.rs b/lib/entry/libimagentryref/src/hasher.rs
index aec7eeb..adadd60 100644
--- a/lib/entry/libimagentryref/src/hasher.rs
+++ b/lib/entry/libimagentryref/src/hasher.rs
@@ -42,12 +42,17 @@ pub mod sha1 {
pub struct Sha1Hasher;
+ impl Sha1Hasher {
+ pub fn sha1_hash(s: &str) -> String {
+ format!("{:x}", Sha1::digest(s.as_bytes())) // TODO: Ugh...
+ }
+ }
+
impl Hasher for Sha1Hasher {
const NAME : &'static str = "sha1";
fn hash<P: AsRef<Path>>(path: P) -> Result<String> {
- let digest = Sha1::digest(::std::fs::read_to_string(path)?.as_bytes());
- Ok(format!("{:x}", digest)) // TODO: Ugh...
+ Ok(Sha1Hasher::sha1_hash(&::std::fs::read_to_string(path)?))
}
}