clone_solana_keccak_hasher/
lib.rs

1//! Hashing with the [keccak] (SHA-3) hash function.
2//!
3//! [keccak]: https://keccak.team/keccak.html
4#![cfg_attr(docsrs, feature(doc_auto_cfg))]
5#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
6#![no_std]
7#[cfg(feature = "std")]
8extern crate std;
9
10pub use clone_solana_hash::{ParseHashError, HASH_BYTES, MAX_BASE58_LEN};
11#[cfg(any(feature = "sha3", not(target_os = "solana")))]
12use sha3::{Digest, Keccak256};
13#[cfg(feature = "borsh")]
14use {
15    borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
16    std::string::ToString,
17};
18use {
19    clone_solana_sanitize::Sanitize,
20    core::{fmt, str::FromStr},
21};
22
23// TODO: replace this with `clone_solana_hash::Hash` in the
24// next breaking change.
25// It's a breaking change because the field is public
26// here and private in `clone_solana_hash`, and making
27// it public in `clone_solana_hash` would break wasm-bindgen
28#[cfg_attr(
29    feature = "frozen-abi",
30    derive(clone_solana_frozen_abi_macro::AbiExample)
31)]
32#[cfg_attr(
33    feature = "borsh",
34    derive(BorshSerialize, BorshDeserialize, BorshSchema),
35    borsh(crate = "borsh")
36)]
37#[cfg_attr(
38    feature = "serde",
39    derive(serde_derive::Deserialize, serde_derive::Serialize)
40)]
41#[derive(Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
42#[repr(transparent)]
43pub struct Hash(pub [u8; HASH_BYTES]);
44
45#[cfg(any(feature = "sha3", not(target_os = "solana")))]
46#[derive(Clone, Default)]
47pub struct Hasher {
48    hasher: Keccak256,
49}
50
51#[cfg(any(feature = "sha3", not(target_os = "solana")))]
52impl Hasher {
53    pub fn hash(&mut self, val: &[u8]) {
54        self.hasher.update(val);
55    }
56    pub fn hashv(&mut self, vals: &[&[u8]]) {
57        for val in vals {
58            self.hash(val);
59        }
60    }
61    pub fn result(self) -> Hash {
62        Hash(self.hasher.finalize().into())
63    }
64}
65
66impl From<clone_solana_hash::Hash> for Hash {
67    fn from(val: clone_solana_hash::Hash) -> Self {
68        Self(val.to_bytes())
69    }
70}
71
72impl From<Hash> for clone_solana_hash::Hash {
73    fn from(val: Hash) -> Self {
74        Self::new_from_array(val.0)
75    }
76}
77
78impl Sanitize for Hash {}
79
80impl AsRef<[u8]> for Hash {
81    fn as_ref(&self) -> &[u8] {
82        &self.0[..]
83    }
84}
85
86impl fmt::Debug for Hash {
87    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88        let converted: clone_solana_hash::Hash = (*self).into();
89        fmt::Debug::fmt(&converted, f)
90    }
91}
92
93impl fmt::Display for Hash {
94    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95        let converted: clone_solana_hash::Hash = (*self).into();
96        fmt::Display::fmt(&converted, f)
97    }
98}
99
100impl FromStr for Hash {
101    type Err = ParseHashError;
102
103    fn from_str(s: &str) -> Result<Self, Self::Err> {
104        let unconverted = clone_solana_hash::Hash::from_str(s)?;
105        Ok(unconverted.into())
106    }
107}
108
109impl Hash {
110    #[deprecated(since = "2.2.0", note = "Use 'Hash::new_from_array' instead")]
111    pub fn new(hash_slice: &[u8]) -> Self {
112        #[allow(deprecated)]
113        Self::from(clone_solana_hash::Hash::new(hash_slice))
114    }
115
116    pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
117        Self(hash_array)
118    }
119
120    /// unique Hash for tests and benchmarks.
121    pub fn new_unique() -> Self {
122        Self::from(clone_solana_hash::Hash::new_unique())
123    }
124
125    pub fn to_bytes(self) -> [u8; HASH_BYTES] {
126        self.0
127    }
128}
129
130/// Return a Keccak256 hash for the given data.
131pub fn hashv(vals: &[&[u8]]) -> Hash {
132    // Perform the calculation inline, calling this from within a program is
133    // not supported
134    #[cfg(not(target_os = "solana"))]
135    {
136        let mut hasher = Hasher::default();
137        hasher.hashv(vals);
138        hasher.result()
139    }
140    // Call via a system call to perform the calculation
141    #[cfg(target_os = "solana")]
142    {
143        let mut hash_result = [0; HASH_BYTES];
144        unsafe {
145            clone_solana_define_syscall::definitions::sol_keccak256(
146                vals as *const _ as *const u8,
147                vals.len() as u64,
148                &mut hash_result as *mut _ as *mut u8,
149            );
150        }
151        Hash::new_from_array(hash_result)
152    }
153}
154
155/// Return a Keccak256 hash for the given data.
156pub fn hash(val: &[u8]) -> Hash {
157    hashv(&[val])
158}
159
160#[cfg(feature = "std")]
161/// Return the hash of the given hash extended with the given value.
162pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
163    let mut hash_data = id.as_ref().to_vec();
164    hash_data.extend_from_slice(val);
165    hash(&hash_data)
166}