pub struct Hash<'a> { /* private fields */ }Expand description
A container for an Argon2 hash, the corresponding salt, and the parameters used for hashing
Implementations§
Source§impl<'a> Hash<'a>
impl<'a> Hash<'a>
Sourcepub fn from_parts(
hash: &'a [u8],
salt: &'a [u8],
alg: Algorithm,
mem_cost_kib: u32,
iterations: u32,
threads: u32,
) -> Self
pub fn from_parts( hash: &'a [u8], salt: &'a [u8], alg: Algorithm, mem_cost_kib: u32, iterations: u32, threads: u32, ) -> Self
Constructs a Hash from a slice of the hash and salt and the parameters used for hashing.
Sourcepub fn from_parts_owned(
hash: Vec<u8>,
salt: Vec<u8>,
alg: Algorithm,
mem_cost_kib: u32,
iterations: u32,
threads: u32,
) -> Self
pub fn from_parts_owned( hash: Vec<u8>, salt: Vec<u8>, alg: Algorithm, mem_cost_kib: u32, iterations: u32, threads: u32, ) -> Self
Constructs a Hash from a Vec<u8> of the hash and salt and the parameters used for hashing,
taking ownership of the hash and salt Vecs.
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns a reference to a byte slice of the computed hash/key.
Sourcepub fn salt_bytes(&self) -> &[u8] ⓘ
pub fn salt_bytes(&self) -> &[u8] ⓘ
Returns a reference to a byte slice of the salt used to generate the hash.
Sourcepub fn memory_cost_kib(&self) -> u32
pub fn memory_cost_kib(&self) -> u32
Returns the memory cost used to generate the hash.
Sourcepub fn iterations(&self) -> u32
pub fn iterations(&self) -> u32
Returns the number of iterations used to generate the hash.
Sourcepub fn verify(&self, password: &[u8]) -> bool
pub fn verify(&self, password: &[u8]) -> bool
Checks if the hash matches the provided password.
Because verification requires re-hashing the password, this is an expensive operation.
For some appliations, it might make sense to move this operation to a separate thread
using std::thread or something like
the Rayon crate to avoid blocking main threads.
Sourcepub fn verify_with_secret(&self, password: &[u8], secret: Secret<'_>) -> bool
pub fn verify_with_secret(&self, password: &[u8], secret: Secret<'_>) -> bool
Checks if the hash matches the provided password using the provided secret.
Because verification requires re-hashing the password, this is an expensive operation.
For some appliations, it might make sense to move this operation to a separate thread
using std::thread or something like
the Rayon crate to avoid blocking main threads.
Trait Implementations§
Source§impl FromStr for Hash<'_>
impl FromStr for Hash<'_>
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Deserializes a hash string into parts (e.g. the hash, the salt, parameters) that can be used for purposes such as verification or encryption.
A hash string looks something like this:
$argon2id$v=19$m=62500,t=18,p=2$AQIDBAUGBwg$ypJ3pKxN4aWGkwMv0TOb08OIzwrfK1SZWy64vyTLKo8
Source§type Err = Argon2Error
type Err = Argon2Error
Source§impl ToString for Hash<'_>
impl ToString for Hash<'_>
Source§fn to_string(&self) -> String
fn to_string(&self) -> String
Generates a hash string. Aside from the hash, the hash string also includes the salt and paramters used to generate the hash, making it easy to store in a database or a cache. This string is formatted to a standard shared by most implementations of argon2, so other argon2 libraries should be able to use this hash string.
A hash string looks something like this:
$argon2id$v=19$m=62500,t=18,p=2$AQIDBAUGBwg$ypJ3pKxN4aWGkwMv0TOb08OIzwrfK1SZWy64vyTLKo8