hashes icon indicating copy to clipboard operation
hashes copied to clipboard

Implement SerializableState for hashes

Open rpiasetskyi opened this issue 3 years ago • 4 comments

The internal state of different hashes can be used for serialization.

rpiasetskyi avatar Aug 10 '22 22:08 rpiasetskyi

Could you please suggest the best solution for tests if the expected values are very long?

digest::hash_serialization_test!(md5_serialization, Md5, 
    hex!("e5ca2295db93d6dd07ab990fcad2219e00000000000000010113000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
digest::hash_serialization_test!(
    md5_serialization,
    Md5,
    [
        0xe5, 0xca, 0x22, 0x95, 0xdb, 0x93, 0xd6, 0xdd, 0x07, 0xab, 0x99, 0x0f, 0xca, 0xd2, 0x21,
        0x9e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x13, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    ]
);

rpiasetskyi avatar Sep 04 '22 16:09 rpiasetskyi

You can split it into several lines:

let bytes = hex!(
    "0001020304050607"
    "08090a0b0c0d0e0f"
);
// equivalent to:
let bytes = hex!("000102030405060708090a0b0c0d0e0f");

It requires hex-literal v0.3.3.

newpavlov avatar Sep 04 '22 17:09 newpavlov

Is it okay if I bump hex-literal? This change is moving MSRV of crates to 1.45. If yes, what is the preferable width of the line?

rpiasetskyi avatar Sep 04 '22 20:09 rpiasetskyi

Ah, I forgot about that. In this case simply use new lines:

let bytes = hex!("
    0001020304050607
    08090a0b0c0d0e0f
");

Unfortunately, rustfmt does not like it too much, so you may need to add #[rustfmt::ignore] or use an alternative formatting choice.

newpavlov avatar Sep 04 '22 20:09 newpavlov

Merged as #574

tarcieri avatar Mar 19 '24 21:03 tarcieri