1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-04-26 11:45:00 +02:00

add Hasher::count

This commit is contained in:
Jack O'Connor 2021-11-04 20:37:05 -04:00
parent 1042917e16
commit 04571021fb
2 changed files with 14 additions and 6 deletions

View File

@ -1301,6 +1301,11 @@ impl Hasher {
pub fn finalize_xof(&self) -> OutputReader {
OutputReader::new(self.final_output())
}
/// Return the total number of bytes hashed so far.
pub fn count(&self) -> u64 {
self.chunk_state.chunk_counter * CHUNK_LEN as u64 + self.chunk_state.len() as u64
}
}
// Don't derive(Debug), because the state may be secret.

View File

@ -240,8 +240,9 @@ mod tests {
) {
let mut out = vec![0; expected_hash.len()];
let mut hasher = blake3::Hasher::new();
for &b in input {
hasher.update(&[b]);
for i in 0..input.len() {
hasher.update(&[input[i]]);
assert_eq!(i as u64 + 1, hasher.count());
}
hasher.finalize_xof().fill(&mut out);
assert_eq!(expected_hash, &out[..]);
@ -249,8 +250,9 @@ mod tests {
let mut out = vec![0; expected_keyed_hash.len()];
let mut hasher = blake3::Hasher::new_keyed(key);
for &b in input {
hasher.update(&[b]);
for i in 0..input.len() {
hasher.update(&[input[i]]);
assert_eq!(i as u64 + 1, hasher.count());
}
hasher.finalize_xof().fill(&mut out);
assert_eq!(expected_keyed_hash, &out[..]);
@ -258,8 +260,9 @@ mod tests {
let mut out = vec![0; expected_derive_key.len()];
let mut hasher = blake3::Hasher::new_derive_key(TEST_CONTEXT);
for &b in input {
hasher.update(&[b]);
for i in 0..input.len() {
hasher.update(&[input[i]]);
assert_eq!(i as u64 + 1, hasher.count());
}
hasher.finalize_xof().fill(&mut out);
assert_eq!(expected_derive_key, &out[..]);