use sha1::Sha1;
use sha2::{Digest, Sha256};
pub fn v4_fingerprint(key_body: &[u8]) -> [u8; 20] {
let mut hasher = Sha1::new();
hasher.update([0x99]);
hasher.update((key_body.len() as u16).to_be_bytes());
hasher.update(key_body);
hasher.finalize().into()
}
pub fn v6_fingerprint(key_body: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update([0x9B]);
hasher.update((key_body.len() as u32).to_be_bytes());
hasher.update(key_body);
hasher.finalize().into()
}
pub fn key_id(version: u8, fingerprint: &[u8]) -> String {
let bytes = match version {
6 => &fingerprint[..8],
_ => &fingerprint[fingerprint.len() - 8..],
};
to_hex(bytes)
}
pub fn to_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{:02X}", b)).collect()
}