Skip to main content

opencode_ralph_loop_cli/hash/
mod.rs

1use sha2::{Digest, Sha256};
2
3pub fn sha256_hex(bytes: &[u8]) -> String {
4    let result = Sha256::digest(bytes);
5    hex::encode(result)
6}
7
8#[cfg(test)]
9mod tests {
10    use super::*;
11
12    #[test]
13    fn empty_hash_is_expected() {
14        let hash = sha256_hex(b"");
15        assert_eq!(
16            hash,
17            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
18        );
19    }
20
21    #[test]
22    fn hash_is_deterministic() {
23        let h1 = sha256_hex(b"hello world");
24        let h2 = sha256_hex(b"hello world");
25        assert_eq!(h1, h2);
26        assert_eq!(h1.len(), 64);
27    }
28}