aboutsummaryrefslogtreecommitdiffstats
path: root/src/k_anon.rs
blob: b8b7757c683f9e871cd0f418c757ecf3505e1aa9 (plain) (blame)
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
45
46
47
// checkpw - Check passwords against pwnedpasswords.com
// Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use ring::digest;

//
// Convert a slice of bytes into a string of hex values
//
fn to_hex(data: &[u8]) -> String {
    data.iter().map(|b| format!("{:02X}", b)).collect()
}

#[test]
fn test_to_hex() {
    let input = [0x01, 0x00, 0xff, 0xaa, 0x10, 0x07];
    assert_eq!(to_hex(&input), "0100FFAA1007");
}

///
/// Split the digest into the range and rest parts for k-anonymity
///
pub fn to_k_anon(d: digest::Digest) -> (String, String) {
    let mut hash = to_hex(d.as_ref());
    let rest = hash.split_off(5);
    (hash, rest)
}

#[test]
fn test_k_anon() {
    let input = digest::digest(&digest::SHA1, "Passw0rd".as_bytes());
    let (range, rest) = to_k_anon(input);
    assert_eq!(&range, "EBFC7");
    assert_eq!(&rest, "910077770C8340F63CD2DCA2AC1F120444F");
}