arcis-interface 0.11.1

Defines the data interface and core types for Arcis circuits.
Documentation
use sha2::{Digest, Sha256};
use std::io::Write;

fn compute_hash(src: &[u8]) -> [u8; 32] {
    Sha256::digest(src).into()
}

#[derive(Debug, Clone)]
pub struct PathWithHash {
    pub path: String,
    pub hash: [u8; 32],
}

impl PathWithHash {
    pub fn new(contents: &[u8], path: String) -> Result<Self, std::io::Error> {
        let mut file = std::fs::File::create(&path)?;
        file.write_all(contents)?;
        let hash = compute_hash(contents);
        Ok(PathWithHash { path, hash })
    }
    pub fn check(&self) -> bool {
        let Ok(file) = std::fs::read(&self.path) else {
            return false;
        };
        let hash = compute_hash(&file);
        self.hash == hash
    }
    pub fn checked_read(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        let file = std::fs::read(&self.path)?;
        let hash = compute_hash(&file);
        if self.hash == hash {
            Ok(file)
        } else {
            Err("Hash mismatch".into())
        }
    }
}