1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
    Appellation: hash <module>
    Contributors: FL03 <jo3mccain@icloud.com>
    Description: ... Summary ...
*/
pub use self::{hashes::*, hasher::Hash, utils::*};

pub(crate) mod hashes;
pub(crate) mod hasher;

pub(crate) mod utils {
    use crate::GenericHash;
    use std::string::ToString;

    /// hasher implements a generic hash function wrapper around blake3
    pub fn hasher<T: ToString>(data: &T) -> GenericHash {
        blake3::hash(data.to_string().as_bytes())
            .as_bytes()
            .to_owned()
            .into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{self, distributions::Alphanumeric, Rng};

    pub fn generate_random_string(length: Option<usize>) -> String {
        std::ops::Range {
            start: 0,
            end: length.unwrap_or(12),
        }
        .map(|_| rand::thread_rng().sample(Alphanumeric) as char)
        .collect::<String>()
    }

    #[test]
    fn test_hasher() {
        let a = hasher(&generate_random_string(None));
        let b = hasher(&generate_random_string(None));
        assert_ne!(&a, &b)
    }
}