use crate::compress::state_to_bytes;
use crate::consts::STATE_INIT;
#[inline(always)]
pub fn build_final_blocks(total_bytes: u64, tail: &[u8], blocks: &mut [[u8; 64]; 2]) -> usize {
debug_assert!(tail.len() < 64);
let bit_len = total_bytes.wrapping_mul(8).to_le_bytes();
if tail.len() <= 55 {
blocks[0] = [0u8; 64];
blocks[0][..tail.len()].copy_from_slice(tail);
blocks[0][tail.len()] = 0x80;
blocks[0][56..64].copy_from_slice(&bit_len);
1
} else {
blocks[0] = [0u8; 64];
blocks[1] = [0u8; 64];
blocks[0][..tail.len()].copy_from_slice(tail);
blocks[0][tail.len()] = 0x80;
blocks[1][56..64].copy_from_slice(&bit_len);
2
}
}
#[inline(always)]
pub fn hash_with<F>(input: &[u8], mut compress: F) -> [u8; 16]
where
F: FnMut(&mut [u32; 4], &[u8; 64]),
{
if input.is_empty() {
return finalize_with(STATE_INIT, 0, &[], compress);
}
let mut state = STATE_INIT;
let (blocks, tail) = input.as_chunks::<64>();
for block in blocks {
compress(&mut state, block);
}
finalize_with(state, input.len() as u64, tail, compress)
}
#[inline(always)]
pub fn finalize_with<F>(state: [u32; 4], total_bytes: u64, tail: &[u8], mut compress: F) -> [u8; 16]
where
F: FnMut(&mut [u32; 4], &[u8; 64]),
{
let mut st = state;
let mut blocks = [[0u8; 64]; 2];
let used = build_final_blocks(total_bytes, tail, &mut blocks);
for block in &blocks[..used] {
compress(&mut st, block);
}
state_to_bytes(st)
}