DispnetHash

Struct DispnetHash 

Source
pub struct DispnetHash {
    pub hash_type: HashType,
    pub digest_length: usize,
    pub digest_value: Vec<u8>,
    pub digest_encoded: u64,
    /* private fields */
}
Expand description

Dispnet hash is as self descriping hash format.

§Display format is structured as followed:

  • First 2 characters are the hash type as integer with a leading 0 (Default is 01 which is Blake3 hash).
  • Then come 4 characters as integer with leading 0 which is the length of the bytes from the digest.
  • Digest value as hex.

§Examles

fn new_hash() {
    let dispnet_hash = dispnet_hash::DispnetHash::new("test".as_bytes());
    let display_hash = format!("{}", dispnet_hash);
    assert_eq!(display_hash, "010324878ca0425c739fa427f7eda20fe845f6b2e46ba5fe2a14df5b1e32f50603215");
}

Fields§

§hash_type: HashType§digest_length: usize§digest_value: Vec<u8>§digest_encoded: u64

Implementations§

Source§

impl DispnetHash

Source

pub fn new(value: &[u8]) -> Self

Create a hash with the default typ (Blake3).

Source

pub fn create( hash_type: HashType, value: &[u8], config: Option<HashConfig>, ) -> Self

Create a new dispnet hash.

§Usage
use dispnet_hash::{DispnetHash, HashType, HashConfig};
 
fn create_hashes() {
    let dispnet_hash_Balke3 = DispnetHash::create(HashType::Blake3, "test".as_bytes(), None);
    let dispnet_hash_CRC = DispnetHash::create(HashType::CRC, "test".as_bytes(), None);
    let dispnet_hash_Argon2 = DispnetHash::create(HashType::Argon2, "test".as_bytes(), None);
    let dispnet_hash_Argon2_slat = DispnetHash::create(HashType::Argon2, "test".as_bytes(), Some(HashConfig { salt: Some(Box::new(b"12345678".to_vec())) }));
}
Source

pub fn verify(hash: &str, value: &[u8]) -> bool

Verify a dispnet hash string with raw value. The hash must be created with the Argon2 type

§Usage
use dispnet_hash::{DispnetHash, HashType};
 
fn verify_hash() {
    let dispnet_hash = DispnetHash::create(HashType::Argon2, "test".as_bytes(), None);
     
    DispnetHash::verify(&dispnet_hash.to_string(), "test".as_bytes());
}
Source

pub fn verify_instance(hash: &DispnetHash, value: &[u8]) -> bool

Verify a dispnet hash instance with raw value. The hash must be created with the Argon2 type

§Usage
use dispnet_hash::{DispnetHash, HashType};
 
fn verify_hash_instance() {
    let dispnet_hash = DispnetHash::create(HashType::Argon2, "test".as_bytes(), None);
     
    DispnetHash::verify_instance(&dispnet_hash, "test".as_bytes());
}
Source

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

Convert a hexadecimal string to a vector of bytes. Returns None if the input string has an odd length which makes it an invalid hex string.

§Usage
use dispnet_hash::DispnetHash;

fn hex_to_bytes() {
    let hex_string = "74657374";
    let bytes = DispnetHash::hex_to_bytes(hex_string).unwrap();
    assert_eq!(bytes, vec![116, 101, 115, 116]);
}
Source

pub fn bytes_to_hex(bytes: &[u8]) -> String

Convert a slice of bytes to a hexadecimal string.

§Usage
use dispnet_hash::DispnetHash;

fn bytes_to_hex() {
    let bytes = vec![116, 101, 115, 116];
    let hex_string = DispnetHash::bytes_to_hex(&bytes);
    assert_eq!(hex_string, "74657374");
}
Source

pub fn encoded_u64(bytes: &[u8]) -> u64

Convert a slice of bytes to a u64 integer. If the length of the slice is less than 8, it is converted to a u64 integer using little-endian byte order. Otherwise, the last 8 bytes of the slice are converted to a u64 integer using little-endian byte order.

§Usage
use dispnet_hash::DispnetHash;

fn encoded_u64() {
    let bytes = vec![0, 0, 0, 0, 0, 0, 0, 1];
    let encoded = DispnetHash::encoded_u64(&bytes);
    assert_eq!(encoded, 72057594037927936);
}

Trait Implementations§

Source§

impl Debug for DispnetHash

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for DispnetHash

Source§

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

Formats the value using the given formatter. Read more
Source§

impl FromStr for DispnetHash

Source§

type Err = HashError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, HashError>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq<String> for DispnetHash

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for DispnetHash

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.