Skip to content

Commit e5b0000

Browse files
committed
sha2: Add tests that check stability of the serialized state
These tests use SHA-512 and serialize its state, comparing it to a well-known values. Then, they rebuild the hasher from that state and check if the final digest is correctly calculated. As documented in the compatibility section fro the `SerializableState` these values should be stable for semver-compatible releases. See: RustCrypto/traits#1693 Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 8f5155c commit e5b0000

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

sha2/tests/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,37 @@ fn sha512_rand() {
4141
),
4242
);
4343
}
44+
45+
#[test]
46+
fn sha512_serializable_state_is_stable() {
47+
use sha2::digest::common::hazmat::SerializableState;
48+
let mut h = Sha512::new();
49+
h.update("just a random example text");
50+
let state: &[u8] = &h.serialize();
51+
assert_eq!(
52+
state,
53+
[
54+
8, 201, 188, 243, 103, 230, 9, 106, 59, 167, 202, 132, 133, 174, 103, 187, 43, 248,
55+
148, 254, 114, 243, 110, 60, 241, 54, 29, 95, 58, 245, 79, 165, 209, 130, 230, 173,
56+
127, 82, 14, 81, 31, 108, 62, 43, 140, 104, 5, 155, 107, 189, 65, 251, 171, 217, 131,
57+
31, 121, 33, 126, 19, 25, 205, 224, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58+
26, 106, 117, 115, 116, 32, 97, 32, 114, 97, 110, 100, 111, 109, 32, 101, 120, 97, 109,
59+
112, 108, 101, 32, 116, 101, 120, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
63+
]
64+
);
65+
let h = Sha512::deserialize(state.try_into().expect("state to be of correct length"))
66+
.expect("state to be deserialized");
67+
let digest: &[u8] = &h.finalize();
68+
assert_eq!(
69+
digest,
70+
[
71+
193, 156, 193, 73, 92, 41, 172, 27, 181, 186, 218, 80, 174, 96, 210, 30, 235, 136, 156,
72+
147, 88, 203, 90, 202, 9, 103, 158, 201, 133, 185, 63, 164, 60, 128, 240, 190, 211,
73+
151, 102, 19, 52, 169, 152, 70, 87, 22, 183, 67, 32, 99, 129, 240, 116, 246, 210, 141,
74+
238, 243, 135, 41, 115, 15, 166, 87
75+
]
76+
);
77+
}

0 commit comments

Comments
 (0)