1use fuel_types::Bytes32;
2use sha2::{
3 digest::Update,
4 Digest,
5 Sha256,
6};
7
8use core::iter;
9
10#[derive(Debug, Default, Clone)]
12pub struct Hasher(Sha256);
13
14impl Hasher {
15 pub const OUTPUT_LEN: usize = Bytes32::LEN;
17
18 pub fn input<B>(&mut self, data: B)
20 where
21 B: AsRef<[u8]>,
22 {
23 sha2::Digest::update(&mut self.0, data)
24 }
25
26 pub fn chain<B>(self, data: B) -> Self
28 where
29 B: AsRef<[u8]>,
30 {
31 Self(self.0.chain(data))
32 }
33
34 pub fn extend_chain<B, I>(mut self, iter: I) -> Self
36 where
37 B: AsRef<[u8]>,
38 I: IntoIterator<Item = B>,
39 {
40 self.extend(iter);
41
42 self
43 }
44
45 pub fn reset(&mut self) {
47 self.0.reset();
48 }
49
50 pub fn hash<B>(data: B) -> Bytes32
52 where
53 B: AsRef<[u8]>,
54 {
55 let mut hasher = Sha256::new();
56
57 sha2::Digest::update(&mut hasher, data);
58
59 <[u8; Bytes32::LEN]>::from(hasher.finalize()).into()
60 }
61
62 pub fn finalize(self) -> Bytes32 {
64 <[u8; Bytes32::LEN]>::from(self.0.finalize()).into()
65 }
66
67 pub fn digest(&self) -> Bytes32 {
69 <[u8; Bytes32::LEN]>::from(self.0.clone().finalize()).into()
70 }
71}
72
73impl<B> iter::FromIterator<B> for Hasher
74where
75 B: AsRef<[u8]>,
76{
77 fn from_iter<T>(iter: T) -> Self
78 where
79 T: IntoIterator<Item = B>,
80 {
81 iter.into_iter().fold(Hasher::default(), Hasher::chain)
82 }
83}
84
85impl<B> Extend<B> for Hasher
86where
87 B: AsRef<[u8]>,
88{
89 fn extend<T: IntoIterator<Item = B>>(&mut self, iter: T) {
90 iter.into_iter().for_each(|b| self.input(b))
91 }
92}