From 24ca66cba68bcab2ad7f3d5c1afead23eaa0a136 Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Tue, 26 Jul 2022 12:00:32 +0200 Subject: [PATCH] Expand test add digest - Expanded the 'use_as_digest' test case to better demonstrate the equalities and unequalities. - Added convenience function `fn digest()` to `DomainSeparatedHasher`. --- src/hashing.rs | 64 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/hashing.rs b/src/hashing.rs index 69498481..27a32939 100644 --- a/src/hashing.rs +++ b/src/hashing.rs @@ -253,6 +253,12 @@ impl DomainSeparatedHasher { self } + /// A convenience function to update, then finalize the hasher and return the hash result. + pub fn digest(mut self, data: &[u8]) -> DomainSeparatedHash { + self.update(data); + self.finalize() + } + /// Finalize the hasher and return the hash result. pub fn finalize(self) -> DomainSeparatedHash { let output = self.inner.finalize(); @@ -520,30 +526,56 @@ mod test { /// Test that it can be used as a standard digest #[test] fn use_as_digest() { - fn hash_test(data: &[u8], expected: &str) { - let mut hasher = D::new(); - hasher.update(data); - let hash = hasher.finalize(); + fn hash_test_standard_digest(data: &[u8], expected: &str) { + let hash = D::digest(data); assert_eq!(to_hex(&hash), expected); } + let data = [0, 0, 0]; + + hash_domain!(GenericHashDomain, "com.tari.generic"); + type GenericHasher = DomainSeparatedHasher; + let expected_hash = "30b95da070278a2d0f13cc8c65b74d4be288560de2f93967074918ab7d06ec31"; + // Using as 'DomainSeparatedHasher' + let hash = GenericHasher::::new("").digest(&data); + assert_eq!(to_hex(hash.as_ref()), expected_hash); + let hash = GenericHasher::::new("my_label").digest(&data); + assert_ne!(to_hex(hash.as_ref()), expected_hash); + // Using as a standard digest + hash_test_standard_digest::>(&data, expected_hash); + hash_domain!(MyDemoHasher, "com.macro.test"); - hash_test::>( - &[0, 0, 0], - "5faa7d48b551362bbee8a02c43e6ab634ed47c58ecf7b353f9afedfe3d574608", - ); + type DemoDomainHasher1 = DomainSeparatedHasher; + let expected_hash = "5faa7d48b551362bbee8a02c43e6ab634ed47c58ecf7b353f9afedfe3d574608"; + // Using as 'DomainSeparatedHasher' + let hash = DemoDomainHasher1::::new("").digest(&data); + assert_eq!(to_hex(hash.as_ref()), expected_hash); + let hash = DemoDomainHasher1::::new("my_label").digest(&data); + assert_ne!(to_hex(hash.as_ref()), expected_hash); + // Using as a standard digest + hash_test_standard_digest::>(&data, expected_hash); hash_domain!(MyDemoHasher2, "com.macro.test", 2); - hash_test::>( - &[0, 0, 0], - "7ea9d671008380ea79d29205ac5436a62ba534c710298b9482f20d488c96060d", - ); + type DemoDomainHasher2 = DomainSeparatedHasher; + let expected_hash = "7ea9d671008380ea79d29205ac5436a62ba534c710298b9482f20d488c96060d"; + // Using as 'DomainSeparatedHasher' + let hash = DemoDomainHasher2::::new("").digest(&data); + assert_eq!(to_hex(hash.as_ref()), expected_hash); + let hash = DemoDomainHasher2::::new("my_label").digest(&data); + assert_ne!(to_hex(hash.as_ref()), expected_hash); + // Using as a standard digest + hash_test_standard_digest::>(&data, expected_hash); hash_domain!(TariHasher, "com.tari.hasher"); - hash_test::>( - &[0, 0, 0], - "0706e40badfe77547d143b0664e8ff190538d4077c6136abad915e4415d3c2ef", - ); + type TariDomainHasher = DomainSeparatedHasher; + let expected_hash = "0706e40badfe77547d143b0664e8ff190538d4077c6136abad915e4415d3c2ef"; + // Using as 'DomainSeparatedHasher' + let hash = TariDomainHasher::::new("").digest(&data); + assert_eq!(to_hex(hash.as_ref()), expected_hash); + let hash = TariDomainHasher::::new("my_label").digest(&data); + assert_ne!(to_hex(hash.as_ref()), expected_hash); + // Using as a standard digest + hash_test_standard_digest::>(&data, expected_hash); } #[test]