MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / file_sha256

Function file_sha256

crates/openshell-supervisor-network/src/procfs.rs:475–504  ·  view source on GitHub ↗

Compute the SHA256 hash of a file, returned as a hex-encoded string. Used for binary integrity verification in the trust-on-first-use (TOFU) model: the proxy hashes a binary on first network request and caches the result. Subsequent requests from the same binary path must produce the same hash, or the request is denied.

(path: &Path)

Source from the content-addressed store, hash-verified

473/// result. Subsequent requests from the same binary path must produce the
474/// same hash, or the request is denied.
475pub fn file_sha256(path: &Path) -> Result<String> {
476 use sha2::{Digest, Sha256};
477 use std::io::Read;
478
479 let start = std::time::Instant::now();
480 let mut file = std::fs::File::open(path)
481 .map_err(|e| miette::miette!("Failed to open {}: {e}", path.display()))?;
482 let mut hasher = Sha256::new();
483 let mut buf = vec![0u8; 65536].into_boxed_slice();
484 let mut total_read = 0u64;
485 loop {
486 let n = file
487 .read(&mut buf)
488 .map_err(|e| miette::miette!("Failed to read {}: {e}", path.display()))?;
489 if n == 0 {
490 break;
491 }
492 total_read += n as u64;
493 hasher.update(&buf[..n]);
494 }
495
496 let hash = hasher.finalize();
497 debug!(
498 " file_sha256: {}ms size={} path={}",
499 start.elapsed().as_millis(),
500 total_read,
501 path.display()
502 );
503 Ok(hex::encode(hash))
504}
505
506#[cfg(test)]
507mod tests {

Calls 1

readMethod · 0.45