Skip to content

Commit

Permalink
add Hasher::count
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663 committed Nov 5, 2021
1 parent 1042917 commit 0457102
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Expand Up @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions test_vectors/src/lib.rs
Expand Up @@ -240,26 +240,29 @@ 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[..]);
assert_eq!(&expected_hash[..32], hasher.finalize().as_bytes());

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[..]);
assert_eq!(&expected_keyed_hash[..32], hasher.finalize().as_bytes());

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[..]);
Expand Down

0 comments on commit 0457102

Please sign in to comment.