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
//! Provides a wrapper around the `sha2` crate for K-Anonymous SHA256
//!
//! Usage:
//! ```
//! let data = String::from("remember to drink water uwu");
//! let hash = KAnonHash::calculate_string_hash(&data);
//! println!("K-Anonymous hash: {}", hash.k_anon());
//! ```

pub mod hash;

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use crate::hash::KAnonHash;
    
    #[test]
    fn it_works() {
        let hash = KAnonHash::calculate_string_hash(&String::from("test uwu"));
        
        assert_eq!(hash.to_string(), String::from("f29647e40fa8e71f059fae548c4179adba008e29237aae93d37709d059a69e7e"));
        assert_eq!(hash.k_anon(), 2);
    }

    #[test]
    fn custom_mask() {
        let hash = KAnonHash::calculate_string_hash(&String::from("fox is soft uwu"));

        assert_eq!(hash.to_string(), String::from("227cb7692de461abd56166c95d704bd46f1c5e247635209fec3560af8fbfee9f"));
        assert_eq!(hash.k_anon_custom_mask(0b0011_1111), 34);
    }

    #[test]
    fn to_from_string() {
        let hash = KAnonHash::calculate_string_hash(&String::from("<script>alert(document.domain)</script>"));

        assert_eq!(hash.to_string(), String::from("09ef621068dedf82998a50bc0a60b9edbc2462765d6e384fab045af8843cd60b"));
        assert_eq!(KAnonHash::from_str("09ef621068dedf82998a50bc0a60b9edbc2462765d6e384fab045af8843cd60b").unwrap(), hash);
    }
}