#![forbid(unsafe_code)]
use core::fmt;
use core::hash::{Hash, Hasher};
use subtle::ConstantTimeEq;
const DEBUG_HEX_PREFIX: usize = 16;
#[derive(Clone, Copy)]
pub struct Fingerprint([u8; 32]);
impl Fingerprint {
#[must_use]
pub fn from_x25519_pubkey(pubkey: &[u8; 32]) -> Self {
Self(*blake3::hash(pubkey).as_bytes())
}
#[must_use]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl PartialEq for Fingerprint {
fn eq(&self, other: &Self) -> bool {
self.0.ct_eq(&other.0).into()
}
}
impl Eq for Fingerprint {}
impl Hash for Fingerprint {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl fmt::Debug for Fingerprint {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let hex = blake3::Hash::from_bytes(self.0).to_hex();
write!(formatter, "Fingerprint({})", &hex[..DEBUG_HEX_PREFIX])
}
}