Skip to main content

Hasher

Struct Hasher 

Source
pub struct Hasher { /* private fields */ }
Expand description

A dynamic Hasher struct to handle all supported algorithms

Implementations§

Source§

impl Hasher

Source

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();
Source

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());
Source

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));
Source

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);
Source

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);
Source

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);

Auto Trait Implementations§

§

impl Freeze for Hasher

§

impl !RefUnwindSafe for Hasher

§

impl Send for Hasher

§

impl !Sync for Hasher

§

impl Unpin for Hasher

§

impl UnsafeUnpin for Hasher

§

impl !UnwindSafe for Hasher

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.