use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct DigestBytes {
bytes: Vec<u8>,
}
impl DigestBytes {
pub fn new(bytes: Vec<u8>) -> Self {
Self { bytes }
}
pub fn as_slice(&self) -> &[u8] {
&self.bytes
}
pub fn len(&self) -> usize {
self.bytes.len()
}
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
pub fn to_hex(&self) -> String {
encode_hex(&self.bytes)
}
}
impl AsRef<[u8]> for DigestBytes {
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl From<Vec<u8>> for DigestBytes {
fn from(bytes: Vec<u8>) -> Self {
Self::new(bytes)
}
}
impl fmt::Display for DigestBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
pub fn encode_hex(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for byte in bytes {
out.push(HEX[(byte >> 4) as usize] as char);
out.push(HEX[(byte & 0x0f) as usize] as char);
}
out
}