oasis_core_runtime/common/crypto/
hash.rs

1//! Hash type.
2use std::convert::TryInto;
3
4use sha2::{Digest, Sha512_256};
5
6use crate::common::key_format::KeyFormatAtom;
7
8impl_bytes!(Hash, 32, "A 32-byte SHA-512/256 hash.");
9
10impl Hash {
11    /// Compute a digest of the passed slice of bytes.
12    pub fn digest_bytes(data: &[u8]) -> Hash {
13        let mut result = [0u8; 32];
14        result[..].copy_from_slice(Sha512_256::digest(data).as_ref());
15
16        Hash(result)
17    }
18
19    /// Compute a digest of the passed slices of bytes.
20    pub fn digest_bytes_list(data: &[&[u8]]) -> Hash {
21        let mut ctx = Sha512_256::new();
22        for datum in data {
23            ctx.update(datum);
24        }
25
26        let mut result = [0u8; 32];
27        result[..].copy_from_slice(ctx.finalize().as_ref());
28
29        Hash(result)
30    }
31
32    /// Returns true if the hash is of an empty string.
33    pub fn is_empty(&self) -> bool {
34        self == &Hash::empty_hash()
35    }
36
37    /// Hash of an empty string.
38    pub fn empty_hash() -> Hash {
39        // This is SHA-512/256 of an empty string.
40        Hash([
41            0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28, 0xab, 0x87, 0xc3, 0x62, 0x2c, 0x51,
42            0x14, 0x06, 0x9b, 0xdd, 0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74, 0x98, 0xd0, 0xc0, 0x1e,
43            0xce, 0xf0, 0x96, 0x7a,
44        ])
45    }
46
47    /// Hash truncated to the given number of bytes.
48    pub fn truncated(&self, n: usize) -> &[u8] {
49        &self.0[..n]
50    }
51}
52
53impl KeyFormatAtom for Hash {
54    fn size() -> usize {
55        Hash::len()
56    }
57
58    fn encode_atom(self) -> Vec<u8> {
59        self.as_ref().to_vec()
60    }
61
62    fn decode_atom(data: &[u8]) -> Self
63    where
64        Self: Sized,
65    {
66        Hash(data.try_into().expect("hash: invalid decode atom data"))
67    }
68}