summaryrefslogtreecommitdiffstats
path: root/lib/entry/libimagentryref/src/hasher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/entry/libimagentryref/src/hasher.rs')
-rw-r--r--lib/entry/libimagentryref/src/hasher.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/entry/libimagentryref/src/hasher.rs b/lib/entry/libimagentryref/src/hasher.rs
new file mode 100644
index 00000000..aec7eebc
--- /dev/null
+++ b/lib/entry/libimagentryref/src/hasher.rs
@@ -0,0 +1,55 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use std::path::Path;
+
+use failure::Fallible as Result;
+
+pub trait Hasher {
+ const NAME: &'static str;
+
+ /// hash the file at path `path`
+ fn hash<P: AsRef<Path>>(path: P) -> Result<String>;
+}
+
+pub mod default {
+ pub use super::sha1::Sha1Hasher as DefaultHasher;
+}
+
+pub mod sha1 {
+ use std::path::Path;
+
+ use failure::Fallible as Result;
+ use sha1::{Sha1, Digest};
+
+ use hasher::Hasher;
+
+ pub struct Sha1Hasher;
+
+ 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...
+ }
+ }
+
+}
+