Skip to content

Commit f6e7ca7

Browse files
committed
Leverage password-hash/getrandom
Companion PR to RustCrypto/traits#2123 The aforementioned traits PR renames `PasswordHash::hash_password` to `hash_password_with_salt`, so this PR updates the method name change accordingly. When the `getrandom` feature is enabled, it provides a new `PasswordHash::hash_password` which *just* accepts a password as an argument, relying on `getrandom` to internally generate a salt. This updates all the code examples to use the new API so users don't have to deal with randomly generating a salt.
1 parent c0a6be5 commit f6e7ca7

17 files changed

Lines changed: 61 additions & 64 deletions

File tree

Cargo.lock

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

argon2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ hex-literal = "1"
3636
default = ["alloc", "getrandom", "simple"]
3737
alloc = ["password-hash?/alloc"]
3838

39-
getrandom = ["simple", "phc/getrandom"]
39+
getrandom = ["simple", "password-hash/getrandom"]
4040
parallel = ["dep:rayon"]
4141
simple = ["password-hash", "phc"]
4242
zeroize = ["dep:zeroize"]

argon2/src/lib.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@
3535
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
3636
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
3737
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
38+
//! // NOTE: example requires `getrandom` feature is enabled
39+
//!
3840
//! use argon2::{
39-
//! password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, phc::Salt},
41+
//! password_hash::{PasswordHasher, PasswordVerifier, phc::PasswordHash},
4042
//! Argon2
4143
//! };
4244
//!
4345
//! let password = b"hunter42"; // Bad password; don't actually use!
44-
//! let salt = Salt::generate(); // Note: needs the `getrandom` feature of `argon2` enabled
4546
//!
46-
//! // Argon2 with default params (Argon2id v19)
47+
//! // Argon2 with default params (Argon2id v19), generating a random salt
4748
//! let argon2 = Argon2::default();
4849
//!
4950
//! // Hash password to PHC string ($argon2id$v=19$...)
50-
//! let password_hash = argon2.hash_password(password, &salt)?.to_string();
51+
//! let password_hash = argon2.hash_password(password)?.to_string();
5152
//!
5253
//! // Verify password against PHC string.
5354
//! //
@@ -66,28 +67,25 @@
6667
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
6768
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
6869
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
70+
//! // NOTE: example requires `getrandom` feature is enabled
71+
//!
6972
//! use argon2::{
70-
//! password_hash::{
71-
//! phc::{PasswordHash, Salt},
72-
//! PasswordHasher, PasswordVerifier,
73-
//! },
73+
//! password_hash::{PasswordHasher, PasswordVerifier, phc::PasswordHash},
7474
//! Algorithm, Argon2, Params, Version
7575
//! };
7676
//!
7777
//! let password = b"hunter42"; // Bad password; don't actually use!
78-
//! let salt = Salt::generate(); // Note: needs the `getrandom` feature of `argon2` enabled
7978
//!
8079
//! // Argon2 with default params (Argon2id v19) and pepper
8180
//! let argon2 = Argon2::new_with_secret(
8281
//! b"secret pepper",
8382
//! Algorithm::default(),
8483
//! Version::default(),
8584
//! Params::default()
86-
//! )
87-
//! .unwrap();
85+
//! )?;
8886
//!
89-
//! // Hash password to PHC string ($argon2id$v=19$...)
90-
//! let password_hash = argon2.hash_password(password, &salt)?.to_string();
87+
//! // Hash password to PHC string ($argon2id$v=19$...), generating a random salt
88+
//! let password_hash = argon2.hash_password(password)?.to_string();
9189
//!
9290
//! // Verify password against PHC string.
9391
//! //
@@ -640,13 +638,17 @@ impl CustomizedPasswordHasher<PasswordHash> for Argon2<'_> {
640638
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
641639
cpu_feat_avx2: self.cpu_feat_avx2,
642640
}
643-
.hash_password(password, salt)
641+
.hash_password_with_salt(password, salt)
644642
}
645643
}
646644

647645
#[cfg(all(feature = "alloc", feature = "password-hash"))]
648646
impl PasswordHasher<PasswordHash> for Argon2<'_> {
649-
fn hash_password(&self, password: &[u8], salt: &[u8]) -> password_hash::Result<PasswordHash> {
647+
fn hash_password_with_salt(
648+
&self,
649+
password: &[u8],
650+
salt: &[u8],
651+
) -> password_hash::Result<PasswordHash> {
650652
let salt = Salt::new(salt).map_err(|_| password_hash::Error::SaltInvalid)?;
651653

652654
let output_len = self
@@ -719,7 +721,7 @@ mod tests {
719721
let params = Params::new(m_cost, t_cost, p_cost, None).unwrap();
720722
let hasher = Argon2::new(Algorithm::default(), version, params);
721723
let hash = hasher
722-
.hash_password(EXAMPLE_PASSWORD, EXAMPLE_SALT)
724+
.hash_password_with_salt(EXAMPLE_PASSWORD, EXAMPLE_SALT)
723725
.unwrap();
724726

725727
assert_eq!(hash.version.unwrap(), version.into());

argon2/tests/kat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn hashtest(
366366
assert_eq!(out, expected_raw_hash);
367367

368368
// Test hash encoding
369-
let phc_hash = ctx.hash_password(pwd, salt).unwrap().to_string();
369+
let phc_hash = ctx.hash_password_with_salt(pwd, salt).unwrap().to_string();
370370
assert_eq!(phc_hash, expected_phc_hash);
371371

372372
let hash = PasswordHash::new(alternative_phc_hash).unwrap();

argon2/tests/phc_strings.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ fn check_hash_encoding_parameters_order() {
211211

212212
let password = b"password";
213213
let salt = [0u8; 8];
214-
let password_hash = ctx.hash_password(password, &salt).unwrap().to_string();
214+
let password_hash = ctx
215+
.hash_password_with_salt(password, &salt)
216+
.unwrap()
217+
.to_string();
215218

216219
// The parameters shall appear in the m,t,p,keyid,data order
217220
assert_eq!(

balloon-hash/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ sha2 = "0.11.0-rc.3"
3131
default = ["alloc", "getrandom", "password-hash"]
3232
alloc = ["password-hash/alloc"]
3333

34-
getrandom = ["phc/getrandom"]
34+
getrandom = ["password-hash/getrandom"]
3535
parallel = ["dep:rayon"]
3636
password-hash = ["dep:password-hash", "dep:phc"]
3737
zeroize = ["dep:zeroize"]

balloon-hash/src/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,26 @@
99

1010
//! # Usage (simple with default params)
1111
//!
12-
//! Note: this example requires the `rand_core` crate with the `std` feature
13-
//! enabled for `rand_core::OsRng` (embedded platforms can substitute their
14-
//! own RNG)
15-
//!
16-
//! Add the following to your crate's `Cargo.toml` to import it:
17-
//!
18-
//! ```toml
19-
//! [dependencies]
20-
//! balloon-hash = "0.2"
21-
//! rand_core = { version = "0.6", features = ["std"] }
22-
//! sha2 = "0.9"
23-
//! ```
24-
//!
25-
//! The `zeroize` crate feature will zeroize allocated memory created when
26-
//! using the [`Balloon::hash`] function. It will do nothing when the `alloc`
27-
//! crate feature is not active.
28-
//!
2912
//! The following example demonstrates the high-level password hashing API:
3013
//!
31-
#![cfg_attr(feature = "getrandom", doc = "```")]
32-
#![cfg_attr(not(feature = "getrandom"), doc = "```ignore")]
14+
#![cfg_attr(all(feature = "alloc", feature = "getrandom"), doc = "```")]
15+
#![cfg_attr(not(all(feature = "alloc", feature = "getrandom")), doc = "```ignore")]
3316
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
17+
//! // NOTE: example requires `getrandom` feature is enabled
18+
//!
3419
//! use balloon_hash::{
35-
//! password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, phc::Salt},
20+
//! password_hash::{PasswordHasher, PasswordVerifier, phc::PasswordHash},
3621
//! Balloon
3722
//! };
3823
//! use sha2::Sha256;
3924
//!
4025
//! let password = b"hunter42"; // Bad password; don't actually use!
41-
//! let salt = Salt::generate(); // Note: needs the `getrandom` feature of `balloon-hash` enabled
4226
//!
4327
//! // Balloon with default params
4428
//! let balloon = Balloon::<Sha256>::default();
4529
//!
4630
//! // Hash password to PHC string ($balloon$v=1$...)
47-
//! let password_hash = balloon.hash_password(password, &salt)?.to_string();
31+
//! let password_hash = balloon.hash_password(password)?.to_string();
4832
//!
4933
//! // Verify password against PHC string
5034
//! let parsed_hash = PasswordHash::new(&password_hash)?;
@@ -235,7 +219,7 @@ where
235219
}
236220
}
237221

238-
Self::new(algorithm, params, self.secret).hash_password(password, salt)
222+
Self::new(algorithm, params, self.secret).hash_password_with_salt(password, salt)
239223
}
240224
}
241225

@@ -245,7 +229,11 @@ where
245229
D: Digest + FixedOutputReset,
246230
Array<u8, D::OutputSize>: ArrayDecoding,
247231
{
248-
fn hash_password(&self, password: &[u8], salt: &[u8]) -> password_hash::Result<PasswordHash> {
232+
fn hash_password_with_salt(
233+
&self,
234+
password: &[u8],
235+
salt: &[u8],
236+
) -> password_hash::Result<PasswordHash> {
249237
let salt = Salt::new(salt).map_err(|_| password_hash::Error::SaltInvalid)?;
250238
let hash = self.hash(password, &salt)?;
251239
let output = Output::new(&hash).map_err(|_| password_hash::Error::OutputSize)?;

balloon-hash/tests/balloon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn hash_simple_retains_configured_params() {
9292
let params = Params::new(s_cost, t_cost, p_cost).unwrap();
9393
let hasher = Balloon::<Sha256>::new(Algorithm::default(), params, None);
9494
let hash = hasher
95-
.hash_password(EXAMPLE_PASSWORD, EXAMPLE_SALT)
95+
.hash_password_with_salt(EXAMPLE_PASSWORD, EXAMPLE_SALT)
9696
.unwrap();
9797

9898
assert_eq!(hash.version.unwrap(), 1);

password-auth/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ fn generate_phc_hash(password: &[u8], salt: &[u8]) -> password_hash::Result<Pass
6161
// Algorithms below are in order of preference
6262
//
6363
#[cfg(feature = "argon2")]
64-
return Argon2::default().hash_password(password, salt);
64+
return Argon2::default().hash_password_with_salt(password, salt);
6565

6666
#[cfg(feature = "scrypt")]
67-
return Scrypt.hash_password(password, salt);
67+
return Scrypt.hash_password_with_salt(password, salt);
6868

6969
#[cfg(feature = "pbkdf2")]
70-
return Pbkdf2.hash_password(password, salt);
70+
return Pbkdf2.hash_password_with_salt(password, salt);
7171
}
7272

7373
/// Verify the provided password against the provided password hash.

pbkdf2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ belt-hash = "0.2.0-rc.3"
3333

3434
[features]
3535
default = ["hmac"]
36-
getrandom = ["simple", "phc/getrandom"]
36+
getrandom = ["simple", "password-hash/getrandom"]
3737
simple = ["hmac", "dep:password-hash", "dep:phc", "sha2"]
3838

3939
[package.metadata.docs.rs]

0 commit comments

Comments
 (0)