~matthiasbeyer/imag

759d57e79738f62d83ff852d7390279096688437 — Matthias Beyer 1 year, 3 months ago ba17174
Fix: Do not assume UTF8 encoding of data to hash

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
2 files changed, 15 insertions(+), 4 deletions(-)

M lib/domain/libimagmail/src/hasher.rs
M lib/entry/libimagentryref/src/hasher.rs
M lib/domain/libimagmail/src/hasher.rs => lib/domain/libimagmail/src/hasher.rs +1 -1
@@ 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()))
    }
}

M lib/entry/libimagentryref/src/hasher.rs => lib/entry/libimagentryref/src/hasher.rs +14 -3
@@ 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))
        }
    }