diff --git a/rust/tool/src/pe.rs b/rust/tool/src/pe.rs index f30ccdd..f0fc7ba 100644 --- a/rust/tool/src/pe.rs +++ b/rust/tool/src/pe.rs @@ -6,13 +6,10 @@ use std::process::Command; use anyhow::{Context, Result}; use goblin::pe::PE; -use sha2::{Digest, Sha256}; use tempfile::TempDir; use crate::esp::EspGenerationPaths; -use crate::utils::{tmpname, SecureTempDirExt}; - -type Hash = sha2::digest::Output; +use crate::utils::{file_hash, tmpname, SecureTempDirExt}; /// Assemble a lanzaboote image. #[allow(clippy::too_many_arguments)] @@ -61,11 +58,6 @@ pub fn lanzaboote_image( Ok(image_path) } -/// Compute the SHA 256 hash of a file. -fn file_hash(file: &Path) -> Result { - Ok(Sha256::digest(fs::read(file)?)) -} - /// Take a PE binary stub and attach sections to it. /// /// The resulting binary is then written to a newly created file at the provided output path. diff --git a/rust/tool/src/utils.rs b/rust/tool/src/utils.rs index 2597150..4ca649c 100644 --- a/rust/tool/src/utils.rs +++ b/rust/tool/src/utils.rs @@ -6,6 +6,7 @@ use std::os::unix::fs::OpenOptionsExt; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; +use sha2::{Digest, Sha256}; use tempfile::TempDir; /// The number of random alphanumeric characters in the tempfiles. @@ -64,3 +65,12 @@ pub fn tmpname() -> OsString { } buf } + +type Hash = sha2::digest::Output; + +/// Compute the SHA 256 hash of a file. +pub fn file_hash(file: &Path) -> Result { + Ok(Sha256::digest(fs::read(file).with_context(|| { + format!("Failed to read file to hash: {file:?}") + })?)) +}