solana_sha256_hasher/
lib.rs#![no_std]
#[cfg(any(feature = "sha2", not(target_os = "solana")))]
use sha2::{Digest, Sha256};
#[cfg(target_os = "solana")]
use solana_define_syscall::define_syscall;
use solana_hash::Hash;
#[cfg(any(feature = "sha2", not(target_os = "solana")))]
#[derive(Clone, Default)]
pub struct Hasher {
hasher: Sha256,
}
#[cfg(any(feature = "sha2", not(target_os = "solana")))]
impl Hasher {
pub fn hash(&mut self, val: &[u8]) {
self.hasher.update(val);
}
pub fn hashv(&mut self, vals: &[&[u8]]) {
for val in vals {
self.hash(val);
}
}
pub fn result(self) -> Hash {
let bytes: [u8; solana_hash::HASH_BYTES] = self.hasher.finalize().into();
bytes.into()
}
}
#[cfg(target_os = "solana")]
define_syscall!(fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64);
pub fn hashv(vals: &[&[u8]]) -> Hash {
#[cfg(not(target_os = "solana"))]
{
let mut hasher = Hasher::default();
hasher.hashv(vals);
hasher.result()
}
#[cfg(target_os = "solana")]
{
let mut hash_result = [0; solana_hash::HASH_BYTES];
unsafe {
sol_sha256(
vals as *const _ as *const u8,
vals.len() as u64,
&mut hash_result as *mut _ as *mut u8,
);
}
Hash::new_from_array(hash_result)
}
}
pub fn hash(val: &[u8]) -> Hash {
hashv(&[val])
}
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
let mut hash_data = id.as_ref().to_vec();
hash_data.extend_from_slice(val);
hash(&hash_data)
}