#[non_exhaustive]
pub struct Hash { pub hash: Vec<u8>, pub salt: Vec<u8>, pub algorithm: HashAlgorithm, }
Expand description

A struct for storing and verifying hashed passwords. It uses #[non_exhaustive] and derive macros for common functionalities.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§hash: Vec<u8>

The password hash.

§salt: Vec<u8>

The salt used for hashing.

§algorithm: HashAlgorithm

The hash algorithm used.

Implementations§

source§

impl Hash

source

pub fn new_argon2i(password: &str, salt: Vec<u8>) -> Result<Hash, String>

Creates a new Hash instance using Argon2i algorithm for password hashing.

Example
use hsh::models::hash::{Hash, Salt};

let password = "my_password";
let salt: Salt = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

let result = Hash::new_argon2i(password, salt);
match result {
    Ok(hash) => println!("Successfully created Argon2i hash"),
    Err(e) => println!("An error occurred: {}", e),
}
source

pub fn new_bcrypt(password: &str, cost: u32) -> Result<Hash, String>

Creates a new Hash instance using Bcrypt algorithm for password hashing.

Example
use hsh::models::hash::Hash;

let password = "my_password";
let cost: u32 = 16;

let result = Hash::new_bcrypt(password, cost);
match result {
    Ok(hash) => println!("Successfully created Bcrypt hash"),
    Err(e) => println!("An error occurred: {}", e),
}
source

pub fn new_scrypt(password: &str, salt: Vec<u8>) -> Result<Hash, String>

Creates a new Hash instance using Scrypt algorithm for password hashing.

Example
use hsh::models::hash::{Hash, Salt};

let password = "my_password";
let salt: Salt = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

let result = Hash::new_scrypt(password, salt);
match result {
    Ok(hash) => println!("Successfully created Scrypt hash"),
    Err(e) => println!("An error occurred: {}", e),
}
source

pub fn algorithm(&self) -> HashAlgorithm

A function that returns the hash algorithm used by the hash map.

source

pub fn from_hash(hash: &[u8], algo: &str) -> Result<Hash, String>

A function that creates a new hash object from a hash value and a hash algorithm.

source

pub fn from_string(hash_str: &str) -> Result<Hash, String>

A function that creates a new hash object from a hash string in the format algorithm$salt$hash.

source

pub fn generate_hash( password: &str, salt: &str, algo: &str ) -> Result<Vec<u8>, String>

A function that generates a hash value for a password using the specified hash algorithm. The function takes three arguments:

  • password: The password to be hashed.
  • salt: A random string used to make the hash value unique.
  • algo: The name of the hash algorithm to use.

The function returns a Result object containing the hash value if successful, or an error message if unsuccessful.

source

pub fn generate_random_string(len: usize) -> String

A function that generates a random string of the specified length.

source

pub fn generate_salt(algo: &str) -> Result<String, String>

A function that generates a random salt for a password using the specified hash algorithm.

source

pub fn hash(&self) -> &[u8]

A function that returns the hash value of a hash object.

source

pub fn hash_length(&self) -> usize

A function that returns the length of the hash value of a hash object.

source

pub fn new(password: &str, salt: &str, algo: &str) -> Result<Hash, String>

A function that creates a new hash object from a password, salt, and hash algorithm.

source

pub fn parse(input: &str) -> Result<Hash, Box<dyn Error>>

A function that parses a JSON string into a hash object.

source

pub fn parse_algorithm(hash_str: &str) -> Result<HashAlgorithm, String>

A function that parses a hash string into a hash algorithm.

source

pub fn salt(&self) -> &[u8]

A function that returns the salt used to hash a password.

source

pub fn set_hash(&mut self, hash: &[u8])

A function that sets the hash value of a hash object.

source

pub fn set_password( &mut self, password: &str, salt: &str, algo: &str ) -> Result<(), String>

A function that sets the password of a hash object.

source

pub fn set_salt(&mut self, salt: &[u8])

A function that sets the salt of a hash object.

source

pub fn to_string_representation(&self) -> String

A function that converts a hash object to a string representation.

source

pub fn verify(&self, password: &str) -> Result<bool, &'static str>

A function that verifies a password against a hash object.

Trait Implementations§

source§

impl Clone for Hash

source§

fn clone(&self) -> Hash

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Hash

source§

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

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

impl<'de> Deserialize<'de> for Hash

source§

fn deserialize<__D>( __deserializer: __D ) -> Result<Hash, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Hash

source§

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

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

impl Hash for Hash

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Hash

source§

fn cmp(&self, other: &Hash) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Hash

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Hash

source§

fn partial_cmp(&self, other: &Hash) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Hash

source§

fn serialize<__S>( &self, __serializer: __S ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Hash

source§

impl StructuralEq for Hash

source§

impl StructuralPartialEq for Hash

Auto Trait Implementations§

§

impl RefUnwindSafe for Hash

§

impl Send for Hash

§

impl Sync for Hash

§

impl Unpin for Hash

§

impl UnwindSafe for Hash

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.

§

impl<T> FromBase64 for T
where T: for<'de> Deserialize<'de>,

§

fn from_base64<Input>(raw: &Input) -> Result<T, Error>
where Input: AsRef<[u8]> + ?Sized,

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<C> SignWithKey<String> for C
where C: ToBase64,

§

fn sign_with_key(self, key: &impl SigningAlgorithm) -> Result<String, Error>

§

impl<T> ToBase64 for T
where T: Serialize,

§

fn to_base64(&self) -> Result<Cow<'_, str>, Error>

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

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

§

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

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,