bitcoin_parser/
hash.rs

1use sha2::{Digest, Sha256};
2
3/// A structure that represents the bitcoin Sha256 Hash
4#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, Ord, PartialOrd)]
5pub struct Hash([u8; 32]);
6
7impl Hash {
8  pub fn from_data(data: &[u8]) -> Hash {
9    let mut hasher1 = Sha256::default();
10    let mut hasher2 = Sha256::default();
11
12    hasher1.input(data);
13    let out = hasher1.result();
14
15    hasher2.input(&out);
16    let out = hasher2.result();
17
18    let slice = out.as_slice();
19    let mut a: [u8; 32] = [0u8; 32];
20    a.copy_from_slice(&slice);
21
22    Hash(a)
23  }
24
25  pub fn from_slice(slice: &[u8; 32]) -> Hash {
26    let mut out = [0u8; 32];
27    out.copy_from_slice(slice);
28    Hash(out)
29  }
30
31  pub fn as_slice(&self) -> &[u8] {
32    &self.0
33  }
34
35  pub fn as_mut_slice(&mut self) -> &mut [u8] {
36    &mut self.0
37  }
38}
39
40pub static ZERO_HASH: Hash = Hash([0; 32]);