Skip to content

Commit 19aa016

Browse files
authored
password-hash: add rand_core feature (#2126)
Adds a `PasswordHash::hash_password_with_rng` method gated on the `rand_core` feature which uses a `TryCryptoRng` to generate a random salt, analogous to the `getrandom`-based `PasswordHash::hash_password`.
1 parent 1cfd410 commit 19aa016

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

password-hash/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ as well as a `no_std`-friendly implementation of the PHC string format
1919
[dependencies]
2020
getrandom = { version = "0.3", optional = true, default-features = false }
2121
phc = { version = "0.6.0-rc.0", optional = true, default-features = false }
22+
rand_core = { version = "0.10.0-rc-2", optional = true, default-features = false }
2223

2324
[features]
2425
alloc = ["phc?/alloc"]
2526
getrandom = ["dep:getrandom", "phc?/getrandom"]
27+
rand_core = ["dep:rand_core", "phc?/rand_core"]
2628

2729
[package.metadata.docs.rs]
2830
all-features = true

password-hash/src/lib.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub use crate::error::{Error, Result};
3636
#[cfg(feature = "phc")]
3737
pub use phc;
3838

39+
#[cfg(feature = "rand_core")]
40+
pub use rand_core::{self, TryCryptoRng};
41+
3942
/// DEPRECATED: import this as `password_hash::phc::PasswordHash`.
4043
#[cfg(feature = "phc")]
4144
#[deprecated(
@@ -71,7 +74,7 @@ pub type Version = u32;
7174
/// > over exactly 16 bytes). 16 bytes encode as 22 characters in B64.
7275
///
7376
/// [PHC string format specification]: https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md#function-duties
74-
#[cfg(feature = "getrandom")]
77+
#[cfg(any(feature = "getrandom", feature = "rand_core"))]
7578
const RECOMMENDED_SALT_LEN: usize = 16;
7679

7780
/// High-level trait for password hashing functions.
@@ -95,6 +98,21 @@ pub trait PasswordHasher<H> {
9598
getrandom::fill(&mut salt).map_err(|_| Error::Crypto)?;
9699
self.hash_password_with_salt(password, &salt)
97100
}
101+
102+
/// Compute the hash `H` from the given password, potentially using configuration stored in
103+
/// `&self` for the parameters, or otherwise the recommended defaults.
104+
///
105+
/// A large random salt will be generated automatically from the provided RNG.
106+
#[cfg(feature = "rand_core")]
107+
fn hash_password_with_rng<R: TryCryptoRng + ?Sized>(
108+
&self,
109+
rng: &mut R,
110+
password: &[u8],
111+
) -> Result<H> {
112+
let mut salt = [0u8; RECOMMENDED_SALT_LEN];
113+
rng.try_fill_bytes(&mut salt).map_err(|_| Error::Crypto)?;
114+
self.hash_password_with_salt(password, &salt)
115+
}
98116
}
99117

100118
/// Trait for password hashing functions which support customization.

0 commit comments

Comments
 (0)