| 1 |
use crate::{Digest, FixedOutput, FixedOutputReset, HashMarker, dev::TestVector}; |
| 2 |
|
| 3 |
/// Fixed-output resettable digest test via the `Digest` trait |
| 4 |
pub fn fixed_reset_test<D: FixedOutputReset + Clone + Default + HashMarker>( |
| 5 |
&TestVector { input, output }: &TestVector, |
| 6 |
) -> Result<(), &'static str> { |
| 7 |
let mut hasher = D::new(); |
| 8 |
// Test that it works when accepting the message all at once |
| 9 |
hasher.update(input); |
| 10 |
let mut hasher2 = hasher.clone(); |
| 11 |
if hasher.finalize()[..] != output[..] { |
| 12 |
return Err("whole message"); |
| 13 |
} |
| 14 |
|
| 15 |
// Test if reset works correctly |
| 16 |
hasher2.reset(); |
| 17 |
hasher2.update(input); |
| 18 |
if hasher2.finalize_reset()[..] != output[..] { |
| 19 |
return Err("whole message after reset"); |
| 20 |
} |
| 21 |
|
| 22 |
// Test that it works when accepting the message in chunks |
| 23 |
for n in 1..core::cmp::min(17, input.len()) { |
| 24 |
let mut hasher = D::new(); |
| 25 |
for chunk in input.chunks(n) { |
| 26 |
hasher.update(chunk); |
| 27 |
hasher2.update(chunk); |
| 28 |
} |
| 29 |
if hasher.finalize()[..] != output[..] { |
| 30 |
return Err("message in chunks"); |
| 31 |
} |
| 32 |
if hasher2.finalize_reset()[..] != output[..] { |
| 33 |
return Err("message in chunks"); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
|
| 40 |
/// Variable-output resettable digest test |
| 41 |
pub fn fixed_test<D: FixedOutput + Default + HashMarker>( |
| 42 |
&TestVector { input, output }: &TestVector, |
| 43 |
) -> Result<(), &'static str> { |
| 44 |
let mut hasher = D::default(); |
| 45 |
// Test that it works when accepting the message all at once |
| 46 |
hasher.update(input); |
| 47 |
if hasher.finalize_fixed()[..] != output[..] { |
| 48 |
return Err("whole message"); |
| 49 |
} |
| 50 |
|
| 51 |
// Test that it works when accepting the message in chunks |
| 52 |
for n in 1..core::cmp::min(17, input.len()) { |
| 53 |
let mut hasher = D::default(); |
| 54 |
for chunk in input.chunks(n) { |
| 55 |
hasher.update(chunk); |
| 56 |
} |
| 57 |
if hasher.finalize_fixed()[..] != output[..] { |
| 58 |
return Err("message in chunks"); |
| 59 |
} |
| 60 |
} |
| 61 |
Ok(()) |
| 62 |
} |