fuel_crypto/
hasher.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use fuel_types::Bytes32;
use sha2::{
    digest::Update,
    Digest,
    Sha256,
};

use core::iter;

/// Standard hasher
#[derive(Debug, Default, Clone)]
pub struct Hasher(Sha256);

impl Hasher {
    /// Length of the output
    pub const OUTPUT_LEN: usize = Bytes32::LEN;

    /// Append data to the hasher
    pub fn input<B>(&mut self, data: B)
    where
        B: AsRef<[u8]>,
    {
        sha2::Digest::update(&mut self.0, data)
    }

    /// Consume, append data and return the hasher
    pub fn chain<B>(self, data: B) -> Self
    where
        B: AsRef<[u8]>,
    {
        Self(self.0.chain(data))
    }

    /// Consume, append the items of the iterator and return the hasher
    pub fn extend_chain<B, I>(mut self, iter: I) -> Self
    where
        B: AsRef<[u8]>,
        I: IntoIterator<Item = B>,
    {
        self.extend(iter);

        self
    }

    /// Reset the hasher to the default state
    pub fn reset(&mut self) {
        self.0.reset();
    }

    /// Hash the provided data, returning its digest
    pub fn hash<B>(data: B) -> Bytes32
    where
        B: AsRef<[u8]>,
    {
        let mut hasher = Sha256::new();

        sha2::Digest::update(&mut hasher, data);

        <[u8; Bytes32::LEN]>::from(hasher.finalize()).into()
    }

    /// Consume the hasher, returning the digest
    pub fn finalize(self) -> Bytes32 {
        <[u8; Bytes32::LEN]>::from(self.0.finalize()).into()
    }

    /// Return the digest without consuming the hasher
    pub fn digest(&self) -> Bytes32 {
        <[u8; Bytes32::LEN]>::from(self.0.clone().finalize()).into()
    }
}

impl<B> iter::FromIterator<B> for Hasher
where
    B: AsRef<[u8]>,
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = B>,
    {
        iter.into_iter().fold(Hasher::default(), Hasher::chain)
    }
}

impl<B> Extend<B> for Hasher
where
    B: AsRef<[u8]>,
{
    fn extend<T: IntoIterator<Item = B>>(&mut self, iter: T) {
        iter.into_iter().for_each(|b| self.input(b))
    }
}