pub struct PwHash<Hash: Bytes + Zeroize, Salt: Bytes + Zeroize> { /* private fields */ }Expand description
Password hash implementation based on Argon2, compatible with libsodium’s
crypto_pwhash_* functions.
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>(
password: &Password,
config: Config,
) -> Result<Self, Error>
pub fn hash<Password: Bytes>( 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.
Sourcepub fn hash_interactive<Password: Bytes>(
password: &Password,
) -> Result<Self, Error>
pub fn hash_interactive<Password: Bytes>( password: &Password, ) -> Result<Self, Error>
Hashes password with a random salt and a default configuration
suitable for interactive hashing, returning the hash, salt, and config
upon success.
§Errors
Returns the same errors as PwHash::hash.
Sourcepub fn hash_moderate<Password: Bytes>(
password: &Password,
) -> Result<Self, Error>
pub fn hash_moderate<Password: Bytes>( password: &Password, ) -> Result<Self, Error>
Hashes password with a random salt and a default configuration
suitable for moderate hashing, returning the hash, salt, and config upon
success.
§Errors
Returns the same errors as PwHash::hash.
Sourcepub fn hash_sensitive<Password: Bytes>(
password: &Password,
) -> Result<Self, Error>
pub fn hash_sensitive<Password: Bytes>( password: &Password, ) -> Result<Self, Error>
Hashes password with a random salt and a default configuration
suitable for sensitive hashing, returning the hash, salt, and config
upon success.
§Errors
Returns the same errors as PwHash::hash.
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>(
password: &Password,
salt: Salt,
config: Config,
) -> Result<Self, Error>
pub fn hash_with_salt<Password: Bytes>( 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 = PwHash::hash_with_defaults(password).expect("unable to hash");
let pw_string = pwhash.to_encoded_string().expect("unable to encode hash");
let parsed_pwhash =
PwHash::from_string_with_defaults(&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>(&self, password: &Password) -> Result<(), Error>
pub fn verify<Password: Bytes>(&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.
Source§impl<Salt: Bytes + Zeroize> PwHash<Hash, Salt>
impl<Salt: Bytes + Zeroize> PwHash<Hash, Salt>
Sourcepub fn derive_keypair<Password: Bytes + Zeroize, PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize>(
password: &Password,
salt: Salt,
config: Config,
) -> Result<KeyPair<PublicKey, SecretKey>, Error>
pub fn derive_keypair<Password: Bytes + Zeroize, PublicKey: NewByteArray<CRYPTO_BOX_PUBLICKEYBYTES> + Zeroize, SecretKey: NewByteArray<CRYPTO_BOX_SECRETKEYBYTES> + Zeroize>( 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.
Source§impl PwHash<Hash, Salt>
impl PwHash<Hash, Salt>
Sourcepub fn hash_with_defaults<Password: Bytes>(
password: &Password,
) -> Result<Self, Error>
pub fn hash_with_defaults<Password: Bytes>( password: &Password, ) -> Result<Self, Error>
Hashes password using default (interactive) config parameters,
returning the Vec<u8>-based hash and salt, with config, upon success.
This function provides reasonable defaults, and is provided for convenience.
§Errors
Returns an error if the password length is unsupported or the underlying Argon2 operation fails.
Sourcepub fn from_string_with_defaults(hashed_password: &str) -> Result<Self, Error>
Available on crate feature base64 only.
pub fn from_string_with_defaults(hashed_password: &str) -> Result<Self, Error>
base64 only.Parses the hashed_password string, returning a new hash instance upon
success. Wraps PwHash::from_string, provided for convenience.
§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.