cargo/util/
hex.rs

1use super::StableHasher;
2use std::fs::File;
3use std::hash::{Hash, Hasher};
4use std::io::Read;
5
6pub fn to_hex(num: u64) -> String {
7    hex::encode(num.to_le_bytes())
8}
9
10pub fn hash_u64<H: Hash>(hashable: H) -> u64 {
11    let mut hasher = StableHasher::new();
12    hashable.hash(&mut hasher);
13    Hasher::finish(&hasher)
14}
15
16pub fn hash_u64_file(mut file: &File) -> std::io::Result<u64> {
17    let mut hasher = StableHasher::new();
18    let mut buf = [0; 64 * 1024];
19    loop {
20        let n = file.read(&mut buf)?;
21        if n == 0 {
22            break;
23        }
24        hasher.write(&buf[..n]);
25    }
26    Ok(Hasher::finish(&hasher))
27}
28
29pub fn short_hash<H: Hash>(hashable: &H) -> String {
30    to_hex(hash_u64(hashable))
31}