pub struct Hasher { /* private fields */ }Expand description
A dynamic Hasher struct to handle all supported algorithms
Implementations§
Source§impl Hasher
impl Hasher
Sourcepub fn new(algo: &str) -> Result<Self>
pub fn new(algo: &str) -> Result<Self>
Create a hasher for the given algorithm if it’s supported
Will raise an error on an unsupported algorithm
§Examples
use libhasher::Hasher;
let mut hasher = Hasher::new("blake3").unwrap();Sourcepub fn update(&mut self, data: &[u8])
pub fn update(&mut self, data: &[u8])
A low-level way to directly update the internal hasher. Only use if you know what you’re doing!
§Examples
use libhasher::Hasher;
let mut hasher = Hasher::new("blake3").unwrap();
hasher.update("Hello, World".as_bytes());Sourcepub fn finalize(&mut self) -> Vec<u8> ⓘ
pub fn finalize(&mut self) -> Vec<u8> ⓘ
A low-level way to directly finalize the internal hasher. Only use if you know what you’re doing!
§Examples
use libhasher::Hasher;
use hex;
let mut hasher = Hasher::new("blake3").unwrap();
hasher.update("Hello, World".as_bytes());
let hash = hasher.finalize();
println!("{}", hex::encode(hash));Sourcepub fn hash_text(&mut self, text: &str) -> Result<String>
pub fn hash_text(&mut self, text: &str) -> Result<String>
High-level way to hash text
§Examples
use libhasher::Hasher;
let mut hasher = Hasher::new("blake3").unwrap();
// This can also be an `&String`
let result = hasher.hash_text("Hello, World").unwrap();
println!("{}", result);Sourcepub fn hash_file_progressbar(
&mut self,
path: &Path,
progress: bool,
mmap: bool,
min_len: Option<u64>,
) -> Result<HashResult>
pub fn hash_file_progressbar( &mut self, path: &Path, progress: bool, mmap: bool, min_len: Option<u64>, ) -> Result<HashResult>
Hash a file with an exposed progress bar. Useful for large files
Hashes a path, optionally showing progress. Allows you to use
the Blake3 mmap feature as well.
If the Hasher’s algorithm does not support mmap (only blake3 supports mmap),
it will quietly fall back to not using it
If min_len is specified, a progress bar will not display unless the file
is larger than min_len bytes, default 256MB
§Examples
use libhasher::Hasher;
use std::path::PathBuf;
// We'll use SHA256 this time
let mut hasher = Hasher::new("sha256").unwrap();
let result = hasher.hash_file_progressbar(&PathBuf::from("Cargo.toml"), true, true, None).unwrap();
println!("{}", result.hash);Sourcepub fn hash_file(&mut self, path: &Path, mmap: bool) -> Result<HashResult>
pub fn hash_file(&mut self, path: &Path, mmap: bool) -> Result<HashResult>
Hash a file with an exposed progress bar. Useful for large files
Hashes a path. Allows you to use the Blake3 mmap feature as well.
If the Hasher’s algorithm does not support mmap (only blake3 supports mmap),
it will quietly fall back to not using it
§Examples
use libhasher::Hasher;
use std::path::PathBuf;
// We'll use SHA256 this time
let mut hasher = Hasher::new("sha256").unwrap();
let result = hasher.hash_file(&PathBuf::from("Cargo.toml"), true).unwrap();
println!("{}", result.hash);