2018-02-14 13:09:59 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2018-02-14 13:09:59 +00:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
use failure::Fallible as Result;
|
2018-02-14 13:09:59 +00:00
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
pub trait Hasher {
|
|
|
|
const NAME: &'static str;
|
2018-02-14 13:09:59 +00:00
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
/// hash the file at path `path`
|
|
|
|
fn hash<P: AsRef<Path>>(path: P) -> Result<String>;
|
|
|
|
}
|
2018-10-30 17:40:52 +00:00
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
pub mod default {
|
|
|
|
pub use super::sha1::Sha1Hasher as DefaultHasher;
|
|
|
|
}
|
2018-02-14 13:09:59 +00:00
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
pub mod sha1 {
|
|
|
|
use std::path::Path;
|
2018-02-14 13:09:59 +00:00
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
use failure::Fallible as Result;
|
|
|
|
use sha1::{Sha1, Digest};
|
|
|
|
|
|
|
|
use hasher::Hasher;
|
|
|
|
|
|
|
|
pub struct Sha1Hasher;
|
2018-02-14 13:09:59 +00:00
|
|
|
|
2019-02-22 10:23:57 +00:00
|
|
|
impl Sha1Hasher {
|
|
|
|
pub fn sha1_hash(s: &str) -> String {
|
|
|
|
format!("{:x}", Sha1::digest(s.as_bytes())) // TODO: Ugh...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 13:39:23 +00:00
|
|
|
impl Hasher for Sha1Hasher {
|
|
|
|
const NAME : &'static str = "sha1";
|
|
|
|
|
|
|
|
fn hash<P: AsRef<Path>>(path: P) -> Result<String> {
|
2019-02-22 10:23:57 +00:00
|
|
|
Ok(Sha1Hasher::sha1_hash(&::std::fs::read_to_string(path)?))
|
2018-10-23 13:39:23 +00:00
|
|
|
}
|
2018-02-14 13:09:59 +00:00
|
|
|
}
|
2018-10-23 13:39:23 +00:00
|
|
|
|
2018-02-14 13:09:59 +00:00
|
|
|
}
|
|
|
|
|