clnrm_core/reporting/
digest.rs

1//! SHA-256 digest for reproducibility
2//!
3//! Generates cryptographic hashes of span data to ensure reproducible test results.
4
5use crate::error::{CleanroomError, Result};
6use sha2::{Digest, Sha256};
7use std::path::Path;
8
9/// SHA-256 digest generator for reproducibility
10pub struct DigestReporter;
11
12impl DigestReporter {
13    /// Write SHA-256 digest to file
14    ///
15    /// # Arguments
16    /// * `path` - File path for digest output
17    /// * `spans_json` - JSON string of spans to hash
18    ///
19    /// # Returns
20    /// * `Result<()>` - Success or error
21    ///
22    /// # Errors
23    /// Returns error if file write fails
24    pub fn write(path: &Path, spans_json: &str) -> Result<()> {
25        let digest = Self::compute_digest(spans_json);
26        Self::write_file(path, &digest)
27    }
28
29    /// Compute SHA-256 digest of input string
30    ///
31    /// # Arguments
32    /// * `spans_json` - JSON string to hash
33    ///
34    /// # Returns
35    /// * Hexadecimal string representation of SHA-256 hash
36    pub fn compute_digest(spans_json: &str) -> String {
37        let mut hasher = Sha256::new();
38        hasher.update(spans_json.as_bytes());
39        format!("{:x}", hasher.finalize())
40    }
41
42    /// Write digest to file with newline
43    fn write_file(path: &Path, digest: &str) -> Result<()> {
44        std::fs::write(path, format!("{}\n", digest))
45            .map_err(|e| CleanroomError::report_error(format!("Failed to write digest: {}", e)))
46    }
47}