pub struct PwHash<Hash: Bytes + Zeroize, Salt: Bytes + Zeroize> { /* private fields */ }alloc only.Expand description
Password hash implementation based on Argon2, compatible with libsodium’s
crypto_pwhash_* functions.
The hash bytes are redacted from Debug output and wiped when the
instance is dropped. PwHash::into_parts transfers ownership of the hash
to the caller, who is then responsible for its handling and zeroization.
Implementations§
Source§impl<Hash: NewBytes + ResizableBytes + Zeroize, Salt: NewBytes + ResizableBytes + Zeroize> PwHash<Hash, Salt>
impl<Hash: NewBytes + ResizableBytes + Zeroize, Salt: NewBytes + ResizableBytes + Zeroize> PwHash<Hash, Salt>
Sourcepub fn hash<Password: Bytes + ?Sized>(
password: &Password,
config: Config,
) -> Result<Self, Error>
pub fn hash<Password: Bytes + ?Sized>( password: &Password, config: Config, ) -> Result<Self, Error>
Hashes password with a random salt and config, returning
the hash, salt, and config upon success.
§Errors
Returns an error if a work limit, memory limit, hash length, or password length is outside the supported range, or if the underlying Argon2 operation fails.
Source§impl<Hash: NewBytes + ResizableBytes + Zeroize, Salt: Bytes + Zeroize> PwHash<Hash, Salt>
impl<Hash: NewBytes + ResizableBytes + Zeroize, Salt: Bytes + Zeroize> PwHash<Hash, Salt>
Sourcepub fn hash_with_salt<Password: Bytes + ?Sized>(
password: &Password,
salt: Salt,
config: Config,
) -> Result<Self, Error>
pub fn hash_with_salt<Password: Bytes + ?Sized>( password: &Password, salt: Salt, config: Config, ) -> Result<Self, Error>
Hashes password with salt and config, returning
the hash, salt, and config upon success.
The caller must provide a unique, unpredictable salt for each password.
Prefer PwHash::hash unless an existing salt must be reused.
§Errors
Returns an error if a work limit, memory limit, hash length, salt length, or password length is outside the supported range, or if the underlying Argon2 operation fails.
Source§impl<Hash: Bytes + From<Vec<u8>> + Zeroize, Salt: Bytes + From<Vec<u8>> + Zeroize> PwHash<Hash, Salt>
impl<Hash: Bytes + From<Vec<u8>> + Zeroize, Salt: Bytes + From<Vec<u8>> + Zeroize> PwHash<Hash, Salt>
Sourcepub fn from_string(hashed_password: &str) -> Result<Self, Error>
Available on crate feature base64 only.
pub fn from_string(hashed_password: &str) -> Result<Self, Error>
base64 only.Creates a new password hash instance by parsing hashed_password.
Compatible with libsodium’s crypto_pwhash_str* functions, including
valid Argon2 strings with non-default salt lengths or parallelism.
§Errors
Returns an error if the string is malformed, uses an unsupported algorithm or version, omits a required field, or contains an invalid encoded value.
Source§impl<Hash: Bytes + Zeroize, Salt: Bytes + Zeroize> PwHash<Hash, Salt>
impl<Hash: Bytes + Zeroize, Salt: Bytes + Zeroize> PwHash<Hash, Salt>
Sourcepub fn to_encoded_string(&self) -> Result<String, Error>
Available on crate feature base64 only.
pub fn to_encoded_string(&self) -> Result<String, Error>
base64 only.Returns a string-encoded representation of this hash, salt, and config, suitable for storage in a database.
The string returned is compatible with libsodium’s crypto_pwhash_str,
crypto_pwhash_str_verify, and crypto_pwhash_str_needs_rehash
functions when the hash length matches libsodium’s string format. The
lower-level hashing API also supports variable-length hash output.
§Errors
Returns an error if the stored parameters are invalid or the resulting string would not fit libsodium’s password-hash string format.
§Example
use dryoc::pwhash::*;
let password = b"Come what come may, time and the hour runs through the roughest day.";
let pwhash = VecPwHash::hash(password, Config::interactive()).expect("unable to hash");
let pw_string = pwhash.to_encoded_string().expect("unable to encode hash");
let parsed_pwhash = VecPwHash::from_string(&pw_string).expect("couldn't parse hashed password");
parsed_pwhash.verify(password).expect("verification failed");
parsed_pwhash
.verify(b"invalid password")
.expect_err("verification should have failed");Sourcepub fn verify<Password: Bytes + ?Sized>(
&self,
password: &Password,
) -> Result<(), Error>
pub fn verify<Password: Bytes + ?Sized>( &self, password: &Password, ) -> Result<(), Error>
Verifies password against this hash using its salt and configuration.
§Errors
Returns an error if the password does not match, if the stored salt or configuration is invalid, or if the underlying Argon2 operation fails.
Sourcepub fn from_parts(hash: Hash, salt: Salt, config: Config) -> Self
pub fn from_parts(hash: Hash, salt: Salt, config: Config) -> Self
Constructs a new instance from hash, salt, and config, consuming
them.
This function does not validate the parts. Invalid values are reported
when an operation such as PwHash::verify or
PwHash::to_encoded_string uses them.
Sourcepub fn into_parts(self) -> (Hash, Salt, Config)
pub fn into_parts(self) -> (Hash, Salt, Config)
Moves the hash, salt, and config out of this instance, returning them as a tuple. The returned hash no longer benefits from the instance’s drop-time zeroization.
Source§impl<Salt: Bytes + Zeroize> PwHash<Hash, Salt>
impl<Salt: Bytes + Zeroize> PwHash<Hash, Salt>
Sourcepub fn derive_keypair<PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize, Password: Bytes + Zeroize + ?Sized>(
password: &Password,
salt: Salt,
config: Config,
) -> Result<KeyPair<PublicKey, SecretKey>, Error>
pub fn derive_keypair<PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize, Password: Bytes + Zeroize + ?Sized>( password: &Password, salt: Salt, config: Config, ) -> Result<KeyPair<PublicKey, SecretKey>, Error>
Derives a keypair from password and salt, using config.
The same password and salt derive the same keypair. Store the salt, keep it unique per derived key, and do not treat it as secret.
§Errors
Returns an error if a work limit, memory limit, salt length, or password length is outside the supported range, or if the underlying Argon2 operation fails.