From cc7982b277e302ad23069f31f7347f453e0a5e55 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Tue, 11 Dec 2018 17:47:00 -0800 Subject: [PATCH] Fixes a bug that was not returning the hash The way this library works is that it is demand-driven, not supply-driven; i.e. it will only fill as many bytes as you provide in a given slice. The prior implementation was a vector of length 0 so the backing slice requested no bytes. --- beacon_chain/utils/hashing/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/beacon_chain/utils/hashing/src/lib.rs b/beacon_chain/utils/hashing/src/lib.rs index 40dddb7a5..02203dc16 100644 --- a/beacon_chain/utils/hashing/src/lib.rs +++ b/beacon_chain/utils/hashing/src/lib.rs @@ -5,7 +5,26 @@ use tiny_keccak::Keccak; pub fn canonical_hash(input: &[u8]) -> Vec { let mut keccak = Keccak::new_keccak256(); keccak.update(input); - let mut result = Vec::with_capacity(32); + let mut result = vec![0; 32]; keccak.finalize(result.as_mut_slice()); result } + +#[cfg(test)] +mod tests { + use super::*; + use std::convert::From; + + #[test] + fn test_hashing() { + let input: Vec = From::from("hello"); + + let output = canonical_hash(input.as_ref()); + let expected = &[ + 0x1c, 0x8a, 0xff, 0x95, 0x06, 0x85, 0xc2, 0xed, 0x4b, 0xc3, 0x17, 0x4f, 0x34, 0x72, + 0x28, 0x7b, 0x56, 0xd9, 0x51, 0x7b, 0x9c, 0x94, 0x81, 0x27, 0x31, 0x9a, 0x09, 0xa7, + 0xa3, 0x6d, 0xea, 0xc8, + ]; + assert_eq!(expected, output.as_slice()); + } +}