tool: move file_hash() to utils module

This commit is contained in:
nikstur 2023-02-11 23:38:01 +01:00
parent 3a3ad7c40d
commit 06b9cdc69e
2 changed files with 11 additions and 9 deletions

View File

@ -6,13 +6,10 @@ use std::process::Command;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use goblin::pe::PE; use goblin::pe::PE;
use sha2::{Digest, Sha256};
use tempfile::TempDir; use tempfile::TempDir;
use crate::esp::EspGenerationPaths; use crate::esp::EspGenerationPaths;
use crate::utils::{tmpname, SecureTempDirExt}; use crate::utils::{file_hash, tmpname, SecureTempDirExt};
type Hash = sha2::digest::Output<Sha256>;
/// Assemble a lanzaboote image. /// Assemble a lanzaboote image.
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -61,11 +58,6 @@ pub fn lanzaboote_image(
Ok(image_path) Ok(image_path)
} }
/// Compute the SHA 256 hash of a file.
fn file_hash(file: &Path) -> Result<Hash> {
Ok(Sha256::digest(fs::read(file)?))
}
/// Take a PE binary stub and attach sections to it. /// 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. /// The resulting binary is then written to a newly created file at the provided output path.

View File

@ -6,6 +6,7 @@ use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use sha2::{Digest, Sha256};
use tempfile::TempDir; use tempfile::TempDir;
/// The number of random alphanumeric characters in the tempfiles. /// The number of random alphanumeric characters in the tempfiles.
@ -64,3 +65,12 @@ pub fn tmpname() -> OsString {
} }
buf buf
} }
type Hash = sha2::digest::Output<Sha256>;
/// Compute the SHA 256 hash of a file.
pub fn file_hash(file: &Path) -> Result<Hash> {
Ok(Sha256::digest(fs::read(file).with_context(|| {
format!("Failed to read file to hash: {file:?}")
})?))
}