rite_runtime/
transcript.rs1use std::io;
4use std::path::Path;
5
6use sha2::{Digest, Sha256};
7
8#[must_use]
10pub fn sha256_hex(data: &[u8]) -> String {
11 let mut hasher = Sha256::new();
12 hasher.update(data);
13 base16ct::lower::encode_string(&hasher.finalize())
14}
15
16#[must_use]
21pub fn compute_fingerprint(data: &[u8]) -> String {
22 format!("sha256:{}", sha256_hex(data))
23}
24
25pub fn compute_file_fingerprint(path: &Path) -> io::Result<String> {
31 let data = std::fs::read(path)?;
32 Ok(compute_fingerprint(&data))
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38
39 #[test]
40 fn fingerprint_of_known_bytes() {
41 let fp = compute_fingerprint(b"hello world");
42 assert_eq!(
43 fp,
44 "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
45 );
46 }
47}