accumulate_client/runtime/
hashing.rs1use sha2::{Digest, Sha256};
2
3pub fn sha256(data: &[u8]) -> [u8; 32] {
5 let mut hasher = Sha256::new();
6 hasher.update(data);
7 let out = hasher.finalize();
8 let mut arr = [0u8; 32];
9 arr.copy_from_slice(&out);
10 arr
11}
12
13pub fn sha256_hex(data: &[u8]) -> String {
15 hex::encode(sha256(data))
16}
17
18#[cfg(test)]
19mod tests {
20 use super::*;
21
22 #[test]
23 fn test_sha256_basic() {
24 let input = b"hello world";
25 let expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
26 assert_eq!(sha256_hex(input), expected);
27 }
28
29 #[test]
30 fn test_sha256_empty() {
31 let input = b"";
32 let expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
33 assert_eq!(sha256_hex(input), expected);
34 }
35
36 #[test]
37 fn test_sha256_deterministic() {
38 let input = b"test deterministic";
39 let hash1 = sha256(input);
40 let hash2 = sha256(input);
41 assert_eq!(hash1, hash2, "Hash should be deterministic");
42 }
43}