use sha2::{Digest, Sha256};
pub(crate) fn sha256_hex(bytes: &[u8]) -> String {
hex(&Sha256::digest(bytes))
}
pub(crate) fn hex(bytes: &[u8]) -> String {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut s = String::with_capacity(bytes.len() * 2);
for &b in bytes {
s.push(char::from(DIGITS[usize::from(b >> 4)]));
s.push(char::from(DIGITS[usize::from(b & 0x0f)]));
}
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_renders_a_digest_as_sha256sum_does() {
assert_eq!(
sha256_hex(b"abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
assert_eq!(hex(&[0x00, 0x0f, 0xf0, 0xff]), "000ff0ff");
}
}