Skip to main content

fluentbase_runtime/syscall_handler/hashing/
sha256.rs

1/// This function will be removed in the future.
2/// We keep it only for backward compatibility with testnet.
3use crate::RuntimeContext;
4use fluentbase_types::B256;
5use rwasm::{StoreTr, TrapCode, Value};
6use sha2::{Digest, Sha256};
7
8pub fn syscall_hashing_sha256_handler(
9    caller: &mut impl StoreTr<RuntimeContext>,
10    params: &[Value],
11    _result: &mut [Value],
12) -> Result<(), TrapCode> {
13    let (data_offset, data_len, output_offset) = (
14        params[0].i32().unwrap() as usize,
15        params[1].i32().unwrap() as usize,
16        params[2].i32().unwrap() as usize,
17    );
18    let data = caller.memory_read_into_vec(data_offset, data_len)?;
19    let hash = syscall_hashing_sha256_impl(&data);
20    caller.memory_write(output_offset, hash.as_slice())?;
21    Ok(())
22}
23
24pub fn syscall_hashing_sha256_impl(data: &[u8]) -> B256 {
25    let mut hasher = Sha256::default();
26    hasher.update(data);
27    let hash: [u8; 32] = hasher.finalize().into();
28    hash.into()
29}