use crate::backend;
use crate::core::Raw;
use alloc::string::String;
#[derive(Clone, Debug)]
pub struct Md5 {
raw: Raw,
}
impl Default for Md5 {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Md5 {
#[inline]
pub const fn new() -> Self {
Self { raw: Raw::new() }
}
#[inline]
pub const fn from_parts(state: [u32; 4], count_bytes: u64) -> Self {
Self {
raw: Raw::from_parts(state, count_bytes),
}
}
#[inline(always)]
pub fn update(&mut self, data: &[u8]) {
self.raw.update_opt(data);
}
#[inline]
#[must_use]
pub fn chain_update(mut self, data: &[u8]) -> Self {
self.update(data);
self
}
#[inline]
pub fn finalize(self) -> [u8; 16] {
self.raw.digest_snapshot(backend::compress_block)
}
#[inline]
pub fn finalize_snapshot(&self) -> [u8; 16] {
self.raw.digest_snapshot(backend::compress_block)
}
#[inline]
pub fn finalize_reset(&mut self) -> [u8; 16] {
let d = self.raw.digest_snapshot(backend::compress_block);
self.raw.reset();
d
}
#[inline]
pub fn reset(&mut self) {
self.raw.reset();
}
#[inline]
pub const fn bytes_hashed(&self) -> u64 {
self.raw.count
}
#[inline]
pub fn zeroize(&mut self) {
self.raw.zeroize();
}
#[cfg(feature = "std")]
#[inline]
pub fn finalize_hex(&self) -> String {
hex_encode(&self.raw.digest_snapshot(backend::compress_block))
}
#[inline]
pub fn digest_so_far(&self) -> [u8; 16] {
self.finalize_snapshot()
}
}
#[cfg(feature = "zeroize")]
impl Drop for Md5 {
#[inline]
fn drop(&mut self) {
self.raw.zeroize();
}
}
#[inline(always)]
pub fn digest(data: &[u8]) -> [u8; 16] {
backend::hash(data)
}
pub fn digest_hex(data: &[u8]) -> String {
hex_encode(&digest(data))
}
pub fn hex_encode(bytes: &[u8]) -> String {
let len = bytes.len().checked_mul(2).expect("hex length overflow");
let mut buf = alloc::vec![0u8; len];
hex_encode_into(bytes, &mut buf);
unsafe { String::from_utf8_unchecked(buf) }
}
#[inline]
pub fn etag_hex(data: &[u8]) -> String {
digest_hex(data)
}
#[inline]
pub fn hex_encode_digest(digest: &[u8; 16]) -> [u8; 32] {
let mut out = [0u8; 32];
hex_encode_into(digest, &mut out);
out
}
const HEX_PAIRS: [[u8; 2]; 256] = {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut table = [[0u8; 2]; 256];
let mut i = 0;
while i < 256 {
table[i] = [HEX[i >> 4], HEX[i & 0x0f]];
i += 1;
}
table
};
#[inline(always)]
pub fn hex_encode_into(bytes: &[u8], out: &mut [u8]) -> usize {
assert!(bytes.len() <= out.len() / 2, "hex output is too short");
for (i, b) in bytes.iter().enumerate() {
let pair = HEX_PAIRS[*b as usize];
out[i * 2] = pair[0];
out[i * 2 + 1] = pair[1];
}
bytes.len() * 2
}
#[cfg(feature = "std")]
pub fn etag_hex_streaming<R: std::io::Read>(reader: R, chunk: usize) -> std::io::Result<String> {
let chunk = chunk.clamp(4096, 1024 * 1024);
let mut buf = vec![0u8; chunk];
digest_reader_with_buffer(reader, &mut buf).map(|digest| hex_encode(&digest))
}
#[cfg(feature = "std")]
pub fn digest_reader<R: std::io::Read>(reader: R) -> std::io::Result<[u8; 16]> {
let mut buf = [0u8; 65536];
digest_reader_with_buffer(reader, &mut buf)
}
#[cfg(feature = "std")]
fn digest_reader_with_buffer<R: std::io::Read>(
mut reader: R,
buf: &mut [u8],
) -> std::io::Result<[u8; 16]> {
let mut h = Md5::new();
loop {
match reader.read(buf) {
Ok(0) => return Ok(h.finalize()),
Ok(n) => h.update(&buf[..n]),
Err(err) if err.kind() == std::io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err),
}
}
}
#[cfg(feature = "std")]
impl std::io::Write for Md5 {
#[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.update(buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}