drt_chain_vm/
crypto_functions.rs1use sha2::Sha256;
2use sha3::{Digest, Keccak256};
3
4pub const SHA256_RESULT_LEN: usize = 32;
5pub const KECCAK256_RESULT_LEN: usize = 32;
6
7pub fn sha256(data: &[u8]) -> [u8; SHA256_RESULT_LEN] {
8 let mut hasher = Sha256::new();
9 hasher.update(data);
10 hasher.finalize().into()
11}
12
13pub fn keccak256(data: &[u8]) -> [u8; KECCAK256_RESULT_LEN] {
14 let mut hasher = Keccak256::new();
15 hasher.update(data);
16 hasher.finalize().into()
17}
18
19#[cfg(feature = "wasm-incopatible")]
20pub fn verify_ed25519(key: &[u8], message: &[u8], signature: &[u8]) -> bool {
21 use ed25519_dalek::*;
22
23 let public = PublicKey::from_bytes(key);
24 if public.is_err() {
25 return false;
26 }
27
28 let sig = Signature::from_bytes(signature);
29 if sig.is_err() {
30 return false;
31 }
32
33 public.unwrap().verify(message, &sig.unwrap()).is_ok()
34}
35
36#[cfg(not(feature = "wasm-incopatible"))]
37pub fn verify_ed25519(_key: &[u8], _message: &[u8], _signature: &[u8]) -> bool {
38 panic!("verify_ed25519 not supported for wasm builds, feature `wasm-incopatible` needs to be enabled")
39}