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 mut data = vec![0u8; data_len];
19    caller.memory_read(data_offset, &mut data)?;
20    let hash = syscall_hashing_sha256_impl(&data);
21    caller.memory_write(output_offset, hash.as_slice())?;
22    Ok(())
23}
24
25pub fn syscall_hashing_sha256_impl(data: &[u8]) -> B256 {
26    let mut hasher = Sha256::default();
27    hasher.update(data);
28    let hash: [u8; 32] = hasher.finalize().into();
29    hash.into()
30}