#[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
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
impl Hash
sourcepub fn new_argon2i(password: &str, salt: Vec<u8>) -> Result<Hash, String>
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),
}
sourcepub fn new_bcrypt(password: &str, cost: u32) -> Result<Hash, String>
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),
}
sourcepub fn new_scrypt(password: &str, salt: Vec<u8>) -> Result<Hash, String>
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),
}
sourcepub fn algorithm(&self) -> HashAlgorithm
pub fn algorithm(&self) -> HashAlgorithm
A function that returns the hash algorithm used by the hash map.
sourcepub fn from_hash(hash: &[u8], algo: &str) -> Result<Hash, String>
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.
sourcepub fn from_string(hash_str: &str) -> Result<Hash, String>
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.
sourcepub fn generate_hash(
password: &str,
salt: &str,
algo: &str
) -> Result<Vec<u8>, String>
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.
sourcepub fn generate_random_string(len: usize) -> String
pub fn generate_random_string(len: usize) -> String
A function that generates a random string of the specified length.
sourcepub fn generate_salt(algo: &str) -> Result<String, String>
pub fn generate_salt(algo: &str) -> Result<String, String>
A function that generates a random salt for a password using the specified hash algorithm.
sourcepub fn hash_length(&self) -> usize
pub fn hash_length(&self) -> usize
A function that returns the length of the hash value of a hash object.
sourcepub fn new(password: &str, salt: &str, algo: &str) -> Result<Hash, String>
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.
sourcepub fn parse(input: &str) -> Result<Hash, Box<dyn Error>>
pub fn parse(input: &str) -> Result<Hash, Box<dyn Error>>
A function that parses a JSON string into a hash object.
sourcepub fn parse_algorithm(hash_str: &str) -> Result<HashAlgorithm, String>
pub fn parse_algorithm(hash_str: &str) -> Result<HashAlgorithm, String>
A function that parses a hash string into a hash algorithm.
sourcepub fn set_password(
&mut self,
password: &str,
salt: &str,
algo: &str
) -> Result<(), String>
pub fn set_password( &mut self, password: &str, salt: &str, algo: &str ) -> Result<(), String>
A function that sets the password of a hash object.
sourcepub fn to_string_representation(&self) -> String
pub fn to_string_representation(&self) -> String
A function that converts a hash object to a string representation.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Hash
impl<'de> Deserialize<'de> for Hash
source§fn deserialize<__D>(
__deserializer: __D
) -> Result<Hash, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Hash, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
source§impl Ord for Hash
impl Ord for Hash
source§impl PartialEq for Hash
impl PartialEq for Hash
source§impl PartialOrd for Hash
impl PartialOrd for Hash
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more