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