scsys_crypto/hash/
hasher.rs1#![allow(clippy::from_over_into)]
7use crate::hash::{hasher, hashes::H256};
8use crate::{GenericHash, Hashable};
9
10#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
11pub struct Hash(pub GenericHash);
12
13impl Hash {
14 pub fn new<T>(data: T) -> Self
15 where
16 T: ToString,
17 {
18 Self(hasher(&data))
19 }
20}
21
22impl Hashable for Hash {
23 fn hash(&self) -> H256 {
24 self.clone().into()
25 }
26}
27
28impl std::convert::From<GenericHash> for Hash {
29 fn from(data: GenericHash) -> Self {
30 Self(data)
31 }
32}
33
34impl std::convert::From<&Hash> for Hash {
35 fn from(data: &Hash) -> Self {
36 Self::new(data.clone())
37 }
38}
39
40impl std::convert::Into<Vec<u8>> for Hash {
41 fn into(self) -> Vec<u8> {
42 self.0.as_slice().to_owned()
43 }
44}
45
46impl std::convert::Into<H256> for Hash {
47 fn into(self) -> H256 {
48 H256::from(self.0.as_slice().to_owned())
49 }
50}
51
52impl std::fmt::Display for Hash {
53 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54 write!(f, "{:x}", self.0)
55 }
56}