blockchain_zc_parser/
hash.rs1use core::fmt;
4
5#[derive(Clone, Copy, PartialEq, Eq)]
10pub struct Hash32<'a>(pub &'a [u8; 32]);
11
12impl<'a> Hash32<'a> {
13 #[inline]
15 pub fn as_bytes(&self) -> &[u8] {
16 self.0.as_slice()
17 }
18
19 #[inline]
21 pub fn as_array(&self) -> &[u8; 32] {
22 self.0
23 }
24
25 #[inline]
27 pub fn eq_array(&self, other: &[u8; 32]) -> bool {
28 self.0 == other
29 }
30}
31
32impl fmt::Debug for Hash32<'_> {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "Hash32(")?;
35 for b in self.0.iter().rev() {
37 write!(f, "{b:02x}")?;
38 }
39 write!(f, ")")
40 }
41}
42
43impl fmt::Display for Hash32<'_> {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 for b in self.0.iter().rev() {
46 write!(f, "{b:02x}")?;
47 }
48 Ok(())
49 }
50}
51
52#[derive(Clone, Copy, PartialEq, Eq)]
54pub struct Hash20<'a>(pub &'a [u8; 20]);
55
56impl fmt::Debug for Hash20<'_> {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "Hash20(")?;
59 for b in self.0.iter() {
60 write!(f, "{b:02x}")?;
61 }
62 write!(f, ")")
63 }
64}
65
66#[cfg(feature = "std")]
72pub fn double_sha256(data: &[u8]) -> [u8; 32] {
73 use sha2::{Digest, Sha256};
74 let first = Sha256::digest(data);
75 Sha256::digest(first).into()
76}