Skip to main content

cosmwasm_crypto/
identity_digest.rs

1//! Dummy 256-bits Digest impl.
2//! This digest stores/accepts a value of the proper length.
3//! To be used for / with already hashed values, just to comply with the Digest contract.
4//!
5//! Adapted from `sha2` [sha256.rs](https://github.com/RustCrypto/hashes/blob/master/sha2/src/sha256.rs)
6use digest::consts::U32;
7use digest::generic_array::GenericArray;
8use digest::{FixedOutput, HashMarker, Output, OutputSizeUser, Reset, Update};
9
10/// The 256-bits identity container
11#[derive(Clone, Default)]
12pub struct Identity256 {
13    array: GenericArray<u8, U32>,
14}
15
16impl Update for Identity256 {
17    fn update(&mut self, hash: &[u8]) {
18        assert_eq!(hash.as_ref().len(), 32);
19        self.array = *GenericArray::from_slice(hash);
20    }
21}
22
23impl OutputSizeUser for Identity256 {
24    type OutputSize = U32;
25}
26
27impl FixedOutput for Identity256 {
28    fn finalize_into(self, out: &mut Output<Self>) {
29        *out = self.array;
30    }
31}
32
33impl HashMarker for Identity256 {}
34
35impl Reset for Identity256 {
36    fn reset(&mut self) {
37        *self = Self::default();
38    }
39}