#![forbid(unsafe_code)]
use core::net::IpAddr;
use zeroize::ZeroizeOnDrop;
use crate::ids::PeerId;
const REDACTED_HEX_LEN: usize = 16;
#[derive(Clone, ZeroizeOnDrop)]
pub struct RedactionKey([u8; 32]);
impl core::fmt::Debug for RedactionKey {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.debug_struct("RedactionKey").finish_non_exhaustive()
}
}
impl RedactionKey {
#[must_use]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
}
fn redact_bytes(key: &RedactionKey, input: &[u8]) -> String {
let digest = blake3::keyed_hash(key.as_bytes(), input);
let hex = digest.to_hex();
hex[..REDACTED_HEX_LEN].to_owned()
}
#[must_use]
pub fn redact_peer_id(key: &RedactionKey, peer_id: &PeerId) -> String {
let bytes = peer_id.into_ulid().to_bytes();
redact_bytes(key, &bytes)
}
#[must_use]
pub fn redact_ip(key: &RedactionKey, ip: IpAddr) -> String {
redact_bytes(key, ip.to_string().as_bytes())
}