fe_common/utils/
keccak.rs

1use tiny_keccak::{Hasher, Keccak};
2
3/// Get the full 32 byte hash of the content.
4pub fn full(content: &[u8]) -> String {
5    partial(content, 32)
6}
7
8/// Take the first `size` number of bytes of the hash and pad the right side
9/// with zeros to 32 bytes.
10pub fn partial_right_padded(content: &[u8], size: usize) -> String {
11    let result = full_as_bytes(content);
12    let padded_output: Vec<u8> = result
13        .iter()
14        .enumerate()
15        .map(|(index, byte)| if index >= size { 0 } else { *byte })
16        .collect();
17
18    hex::encode(padded_output)
19}
20
21/// Take the first `size` number of bytes of the hash with no padding.
22pub fn partial(content: &[u8], size: usize) -> String {
23    let result = full_as_bytes(content);
24    hex::encode(&result[0..size])
25}
26
27/// Get the full 32 byte hash of the content as a byte array.
28pub fn full_as_bytes(content: &[u8]) -> [u8; 32] {
29    let mut keccak = Keccak::v256();
30    let mut selector = [0_u8; 32];
31
32    keccak.update(content);
33    keccak.finalize(&mut selector);
34
35    selector
36}