| Crates.io | sha_file_hashing |
| lib.rs | sha_file_hashing |
| version | 0.1.1 |
| created_at | 2025-12-30 20:52:02.370803+00 |
| updated_at | 2025-12-30 21:00:14.961775+00 |
| description | A Rust library for computing and validating SHA-1 file hashes with a clean, trait-based API |
| homepage | https://github.com/drew-chase/sha_file_hashing |
| repository | https://github.com/drew-chase/sha_file_hashing |
| max_upload_size | |
| id | 2013293 |
| size | 38,761 |
A Rust library for computing and validating SHA-1 file hashes with a clean, trait-based API.
Hashable traitPath, PathBuf, and File typesthiserrorAdd this to your Cargo.toml:
[dependencies]
sha_file_hashing = "0.1.0"
use sha_file_hashing::Hashable;
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new("example.txt");
// Compute hash
let hash = path.hash()?;
println!("SHA-1: {}", hash);
// Validate hash
let is_valid = path.validate(&hash)?;
println!("Valid: {}", is_valid);
Ok(())
}
use sha_file_hashing::Hashable;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = PathBuf::from("documents/file.pdf");
let hash = path.hash()?;
println!("Hash: {}", hash);
Ok(())
}
use sha_file_hashing::Hashable;
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("data.bin")?;
let hash = file.hash()?;
// Validate with a known hash
let expected = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
let is_valid = file.validate(expected)?;
Ok(())
}
If you prefer not to use the trait, you can use the functions directly:
use sha_file_hashing::{hash_file_from_path, validate_file_from_path};
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = Path::new("myfile.txt");
// Hash a file
let hash = hash_file_from_path(path)?;
// Validate a file
let is_valid = validate_file_from_path(path, &hash)?;
Ok(())
}
HashableThe main trait providing hashing functionality:
hash(&self) -> Result<String, SHAError>
Computes and returns the SHA-1 hash of the file as a hexadecimal string.
validate(&self, hash: impl AsRef<str>) -> Result<bool, SHAError>
Validates whether the file matches the provided hash (case-insensitive).
hash_file(file: File) -> Result<String, SHAError>
Computes SHA-1 hash from a File handle.
hash_file_from_path(path: impl AsRef<Path>) -> Result<String, SHAError>
Computes SHA-1 hash from a file path.
validate_file(file: File, hash: impl AsRef<str>) -> bool
Validates a file's hash from a File handle.
validate_file_from_path(path: impl AsRef<Path>, hash: impl AsRef<str>) -> Result<bool, SHAError>
Validates a file's hash from a file path.
pub enum SHAError {
FailedValidation(String),
IO(std::io::Error),
}
FailedValidation: Hash validation failedIO: I/O error occurred (file not found, permission denied, etc.)sha1 crate (v0.11.0) for SHA-1 computationSee LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.