b2sum_rust

Struct Blake2bSum

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

§Blake2b File Hash Constructor

This is the official constructor used to call the new() function with the parameter of the intended digest size.

§Example

use b2sum_rust::Blake2bSum;
 
fn main() {
    // Creates a new File Instance
    let context = Blake2bSum::new(64);
     
    // Outputs a Hexadecimal String
    let hash = context.read("example_file.txt");
 
    // Converts the hexadecimal string to a vector of bytes
    let _bytes = Blake2bSum::as_bytes(&hash);
 
    // Prints The Hexadecimal Representation
    println!("Hash: {}",hash);
 
    // Asserts That These Are Equal
    assert_eq!(hash,"33B20D15383F97EB46D4FA69442596170CCA01008963A7D0E47210C33AEEF991C78323850C012550C227954A40B3D7AD612568ABC73DB9233FAB9EA4F002B0CB");
}
 

All outputs are in UPPER Hexadecimal and between 1 and 64 bytes.

Implementations§

Source§

impl Blake2bSum

Source

pub fn new(digest: usize) -> Self

Examples found in repository?
examples/example_run.rs (line 6)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main(){
    // Creates a new File Instance
    let context = Blake2bSum::new(64);
    
    // Outputs a Hexadecimal String
    let hash = context.read("example_file.txt");

    // Converts the hexadecimal string to a vector of bytes
    let _bytes = Blake2bSum::as_bytes(&hash);

    // Prints The Hexadecimal Representation
    println!("Hash: {}",hash);

    // Asserts That These Are Equal
    assert_eq!(hash,"33B20D15383F97EB46D4FA69442596170CCA01008963A7D0E47210C33AEEF991C78323850C012550C227954A40B3D7AD612568ABC73DB9233FAB9EA4F002B0CB");
}
Source

pub fn read<T: AsRef<Path>>(&self, path: T) -> String

§Hash File

This is a function that hashes a file using Blake2b and returns the Hexadecimal Representation of it as a String. It takes as input any reference to Path.

It should be noted that changes to the file during hashing, such as truncating the file may cause problems.

§About Filebuffer

Filebuffer can map files into memory. This is often faster than using the primitives in std::io, and also more convenient. Furthermore this crate offers prefetching and checking whether file data is resident in physical memory (so access will not incur a page fault). This enables non-blocking file reading.

Examples found in repository?
examples/example_run.rs (line 9)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main(){
    // Creates a new File Instance
    let context = Blake2bSum::new(64);
    
    // Outputs a Hexadecimal String
    let hash = context.read("example_file.txt");

    // Converts the hexadecimal string to a vector of bytes
    let _bytes = Blake2bSum::as_bytes(&hash);

    // Prints The Hexadecimal Representation
    println!("Hash: {}",hash);

    // Asserts That These Are Equal
    assert_eq!(hash,"33B20D15383F97EB46D4FA69442596170CCA01008963A7D0E47210C33AEEF991C78323850C012550C227954A40B3D7AD612568ABC73DB9233FAB9EA4F002B0CB");
}
Source

pub fn read_with_key<T: AsRef<Path>>(&self, path: T, key: &[u8]) -> String

§Hash File (Using Key)

This is a function that hashes a file (using a key) with Blake2b and then returns the Hexadecimal Representation of it as a String. It takes as input any reference to Path.

Source

pub fn read_using_fs<T: AsRef<Path>>(&self, path: T) -> String

👎Deprecated: Please use the read() function instead which uses Filebuffer. This function uses the standard library.
§Hash File (using standard library)

Note: read() or read_with_key() should be used as opposed to this function.

This is a function that hashes a file using Blake2b and returns the Hexadecimal Representation of it as a String. It takes as input any reference to Path.

This does not use filebuffer and instead uses the standard library. Filebuffer is much faster.

Source

pub fn read_str<T: AsRef<str>>(&self, string: T) -> String

§Read String

This function will allow you to take a String or str, convert it to bytes, then hash it.

Source

pub fn read_bytes(&self, bytes: &[u8]) -> String

§Read Bytes

This function will allow you to read bytes and then hash the bytes given the digest size.

Source

pub fn as_bytes(s: &str) -> Vec<u8>

§as_bytes()

as_bytes() converts from a Hexadecimal String to a Vector of Bytes

Examples found in repository?
examples/example_run.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main(){
    // Creates a new File Instance
    let context = Blake2bSum::new(64);
    
    // Outputs a Hexadecimal String
    let hash = context.read("example_file.txt");

    // Converts the hexadecimal string to a vector of bytes
    let _bytes = Blake2bSum::as_bytes(&hash);

    // Prints The Hexadecimal Representation
    println!("Hash: {}",hash);

    // Asserts That These Are Equal
    assert_eq!(hash,"33B20D15383F97EB46D4FA69442596170CCA01008963A7D0E47210C33AEEF991C78323850C012550C227954A40B3D7AD612568ABC73DB9233FAB9EA4F002B0CB");
}
Source

pub fn return_digest_size(&self) -> usize

§Return Digest Size

This method will return the provided digest size that the struct contains. It should be between 1 and 64 of type usize.

Trait Implementations§

Source§

impl Debug for Blake2bSum

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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.