#[allow(deprecated)]
use std::hash::SipHasher;
use std::hash::{Hash, Hasher};
use data_encoding::BASE32HEX_NOPAD;
#[allow(deprecated)]
pub struct StableHasher(SipHasher);
impl StableHasher {
pub fn new() -> Self {
#[allow(deprecated)]
Self(SipHasher::new())
}
pub fn finish_as_short_hash(&self) -> String {
let hash = self.finish();
BASE32HEX_NOPAD.encode(&hash.to_le_bytes())
}
}
impl Hasher for StableHasher {
fn finish(&self) -> u64 {
self.0.finish()
}
fn write(&mut self, bytes: &[u8]) {
self.0.write(bytes)
}
}
pub fn short_hash(hashable: &impl Hash) -> String {
let mut hasher = StableHasher::new();
hashable.hash(&mut hasher);
hasher.finish_as_short_hash()
}
#[cfg(test)]
mod tests {
use super::short_hash;
#[test]
fn short_hash_is_stable() {
assert_eq!(short_hash(&"abcd"), "LA8VKK9KUOE2M");
assert_eq!(short_hash(&123), "8B89NJO1D02MG");
}
}