use sha2::{Digest, Sha256};
pub trait NameHashable {
fn hash_name(name: &str) -> String {
let normalized = name
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
.to_uppercase();
let mut hasher = Sha256::new();
hasher.update(normalized.as_bytes());
let result = hasher.finalize();
hex::encode(result)
}
}
pub fn hash_name(name: &str) -> String {
struct Hasher;
impl NameHashable for Hasher {}
Hasher::hash_name(name)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_name_basic() {
let hash = hash_name("Alice Lee");
assert_eq!(
hash,
"b117f44426c9670da91b563db728cd0bc8bafa7d1a6bb5e764d1aad2ca25032e"
);
}
#[test]
fn test_hash_name_bob_smith() {
let hash = hash_name("Bob Smith");
assert_eq!(
hash,
"5432e86b4d4a3a2b4be57b713b12c5c576c88459fe1cfdd760fd6c99a0e06686"
);
}
#[test]
fn test_hash_name_normalization() {
let expected = hash_name("ALICELEE");
assert_eq!(hash_name("Alice Lee"), expected);
assert_eq!(hash_name("alice lee"), expected);
assert_eq!(hash_name("ALICE LEE"), expected);
assert_eq!(hash_name("Alice Lee"), expected);
assert_eq!(hash_name(" Alice Lee "), expected);
assert_eq!(hash_name("Alice\tLee"), expected);
assert_eq!(hash_name("Alice\nLee"), expected);
}
#[test]
fn test_hash_name_with_middle_name() {
let hash = hash_name("Alice Marie Lee");
assert_eq!(hash.len(), 64); }
#[test]
fn test_hash_name_organization() {
let hash = hash_name("Example VASP Ltd.");
assert_eq!(hash.len(), 64);
}
#[test]
fn test_hash_name_special_characters() {
let hash1 = hash_name("O'Brien");
let hash2 = hash_name("OBrien");
assert_ne!(hash1, hash2); }
#[test]
fn test_hash_name_unicode() {
let hash = hash_name("José García");
assert_eq!(hash.len(), 64);
}
#[test]
fn test_trait_implementation() {
struct TestHasher;
impl NameHashable for TestHasher {}
let hash = TestHasher::hash_name("Alice Lee");
assert_eq!(
hash,
"b117f44426c9670da91b563db728cd0bc8bafa7d1a6bb5e764d1aad2ca25032e"
);
}
}