WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c45480e

Browse files
authored
password-hash: restore getrandom feature (#2123)
Renames `PasswordHash::hash_password` to `hash_password_with_salt`, and adds a new `getrandom`-gated method to `PasswordHash` called `hash_password`, but which only takes `&self` and `password` as arguments, automatically generating a salt of the recommended length.
1 parent c1a92e9 commit c45480e

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
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
@@ -17,10 +17,12 @@ as well as a `no_std`-friendly implementation of the PHC string format
1717
"""
1818

1919
[dependencies]
20+
getrandom = { version = "0.3", optional = true, default-features = false }
2021
phc = { version = "0.6.0-rc.0", optional = true, default-features = false }
2122

2223
[features]
2324
alloc = ["phc?/alloc"]
25+
getrandom = ["dep:getrandom", "phc?/getrandom"]
2426

2527
[package.metadata.docs.rs]
2628
all-features = true

password-hash/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,40 @@ use core::{
6161
/// Numeric version identifier for password hashing algorithms.
6262
pub type Version = u32;
6363

64-
/// Trait for password hashing functions.
64+
/// Recommended length of a salt: 16-bytes.
6565
///
66-
/// Generic around a password hash to be returned (typically [`PasswordHash`])
66+
/// This recommendation comes from the [PHC string format specification]:
67+
///
68+
/// > The role of salts is to achieve uniqueness. A *random* salt is fine
69+
/// > for that as long as its length is sufficient; a 16-byte salt would
70+
/// > work well (by definition, UUID are very good salts, and they encode
71+
/// > over exactly 16 bytes). 16 bytes encode as 22 characters in B64.
72+
///
73+
/// [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")]
75+
const RECOMMENDED_SALT_LEN: usize = 16;
76+
77+
/// High-level trait for password hashing functions.
78+
///
79+
/// Generic around a password hash to be returned (typically [`phc::PasswordHash`])
6780
pub trait PasswordHasher<H> {
68-
/// Simple API for computing a [`PasswordHash`] from a password and
69-
/// salt value.
81+
/// Compute the hash `H` from the given password and salt, potentially using configuration
82+
/// stored in `&self` for the parameters, or otherwise the recommended defaults.
83+
///
84+
/// The salt should be unique per password. When in doubt, use [`PasswordHasher::hash_password`]
85+
/// which will choose the salt for you.
86+
fn hash_password_with_salt(&self, password: &[u8], salt: &[u8]) -> Result<H>;
87+
88+
/// Compute the hash `H` from the given password, potentially using configuration stored in
89+
/// `&self` for the parameters, or otherwise the recommended defaults.
7090
///
71-
/// Uses the default recommended parameters for a given algorithm.
72-
fn hash_password(&self, password: &[u8], salt: &[u8]) -> Result<H>;
91+
/// A large random salt will be generated automatically.
92+
#[cfg(feature = "getrandom")]
93+
fn hash_password(&self, password: &[u8]) -> Result<H> {
94+
let mut salt = [0u8; RECOMMENDED_SALT_LEN];
95+
getrandom::fill(&mut salt).map_err(|_| Error::Crypto)?;
96+
self.hash_password_with_salt(password, &salt)
97+
}
7398
}
7499

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

password-hash/tests/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl CustomizedPasswordHasher<PasswordHash> for StubPasswordHasher {
4848
}
4949

5050
impl PasswordHasher<PasswordHash> for StubPasswordHasher {
51-
fn hash_password(&self, password: &[u8], salt: &[u8]) -> Result<PasswordHash> {
51+
fn hash_password_with_salt(&self, password: &[u8], salt: &[u8]) -> Result<PasswordHash> {
5252
self.hash_password_customized(password, salt, None, None, StubParams)
5353
}
5454
}
@@ -84,7 +84,7 @@ fn verify_password_hash() {
8484
let valid_password = b"test password";
8585
let salt = Salt::from_b64("testsalt000").unwrap();
8686
let hash = StubPasswordHasher
87-
.hash_password(valid_password, &salt)
87+
.hash_password_with_salt(valid_password, &salt)
8888
.unwrap();
8989

9090
// Sanity tests for StubFunction impl above

0 commit comments

Comments
 (0)