Skip to content

Commit cc9f23a

Browse files
authored
Bump password-hash crate (RustCrypto#210)
Updates `argon2`, `pbkdf2`, and `scrypt` with the following upstream changes from the unreleased `password-hash` crate: - Add `version` param to `PasswordHasher` (RustCrypto#719) - Refactor `PasswordHasher` (RustCrypto#720)
1 parent 1cdb4d7 commit cc9f23a

File tree

8 files changed

+40
-21
lines changed

8 files changed

+40
-21
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ members = [
66
"scrypt",
77
"sha-crypt"
88
]
9+
10+
[patch.crates-io]
11+
password-hash = { git = "https://github.com/rustcrypto/traits.git" }

argon2/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! let argon2 = Argon2::default();
4444
//!
4545
//! // Hash password to PHC string ($argon2id$v=19$...)
46-
//! let password_hash = argon2.hash_password_simple(password, salt.as_ref()).unwrap().to_string();
46+
//! let password_hash = argon2.hash_password(password, salt.as_ref()).unwrap().to_string();
4747
//!
4848
//! // Verify password against PHC string
4949
//! let parsed_hash = PasswordHash::new(&password_hash).unwrap();
@@ -106,7 +106,7 @@ use core::convert::TryFrom;
106106
#[cfg(feature = "password-hash")]
107107
use {
108108
core::convert::TryInto,
109-
password_hash::{Ident, Salt},
109+
password_hash::{Decimal, Ident, Salt},
110110
};
111111

112112
/// Minimum and maximum number of lanes (degree of parallelism)
@@ -395,7 +395,7 @@ impl<'key> Argon2<'key> {
395395
impl PasswordHasher for Argon2<'_> {
396396
type Params = Params;
397397

398-
fn hash_password_simple<'a, S>(
398+
fn hash_password<'a, S>(
399399
&self,
400400
password: &[u8],
401401
salt: &'a S,
@@ -426,10 +426,11 @@ impl PasswordHasher for Argon2<'_> {
426426
})
427427
}
428428

429-
fn hash_password<'a>(
429+
fn hash_password_customized<'a>(
430430
&self,
431431
password: &[u8],
432432
alg_id: Option<Ident<'a>>,
433+
version: Option<Decimal>,
433434
params: Params,
434435
salt: impl Into<Salt<'a>>,
435436
) -> password_hash::Result<PasswordHash<'a>> {
@@ -445,14 +446,17 @@ impl PasswordHasher for Argon2<'_> {
445446
params.t_cost,
446447
params.m_cost,
447448
params.p_cost,
448-
params.version,
449+
version
450+
.map(Version::try_from)
451+
.transpose()?
452+
.unwrap_or_else(|| params.version),
449453
)?;
450454

451455
// TODO(tarcieri): pass these via `Params` when `Argon::new` accepts `Params`
452456
hasher.algorithm = Some(algorithm);
453457
hasher.output_size = Some(params.output_size);
454458

455-
hasher.hash_password_simple(password, salt.as_str())
459+
hasher.hash_password(password, salt.as_str())
456460
}
457461
}
458462

@@ -498,7 +502,8 @@ mod tests {
498502
// Too short after decoding
499503
let salt = Salt::new("somesalt").unwrap();
500504

501-
let res = argon2.hash_password(EXAMPLE_PASSWORD, None, Params::default(), salt);
505+
let res =
506+
argon2.hash_password_customized(EXAMPLE_PASSWORD, None, None, Params::default(), salt);
502507
assert_eq!(
503508
res,
504509
Err(password_hash::Error::SaltInvalid(
@@ -517,7 +522,7 @@ mod tests {
517522

518523
let hasher = Argon2::new(None, t_cost, m_cost, p_cost, version).unwrap();
519524
let hash = hasher
520-
.hash_password_simple(EXAMPLE_PASSWORD, EXAMPLE_SALT)
525+
.hash_password(EXAMPLE_PASSWORD, EXAMPLE_SALT)
521526
.unwrap();
522527

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

pbkdf2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//! let salt = SaltString::generate(&mut OsRng);
4040
//!
4141
//! // Hash password to PHC string ($pbkdf2-sha256$...)
42-
//! let password_hash = Pbkdf2.hash_password_simple(password, salt.as_ref()).unwrap().to_string();
42+
//! let password_hash = Pbkdf2.hash_password(password, salt.as_ref()).unwrap().to_string();
4343
//!
4444
//! // Verify password against PHC string
4545
//! let parsed_hash = PasswordHash::new(&password_hash).unwrap();

pbkdf2/src/simple.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::{
1010
};
1111
use hmac::Hmac;
1212
use password_hash::{
13-
errors::InvalidValue, Error, Ident, McfHasher, Output, ParamsString, PasswordHash,
13+
errors::InvalidValue, Decimal, Error, Ident, McfHasher, Output, ParamsString, PasswordHash,
1414
PasswordHasher, Result, Salt,
1515
};
1616
use sha2::{Sha256, Sha512};
@@ -36,14 +36,21 @@ pub struct Pbkdf2;
3636
impl PasswordHasher for Pbkdf2 {
3737
type Params = Params;
3838

39-
fn hash_password<'a>(
39+
fn hash_password_customized<'a>(
4040
&self,
4141
password: &[u8],
4242
alg_id: Option<Ident<'a>>,
43+
version: Option<Decimal>,
4344
params: Params,
4445
salt: impl Into<Salt<'a>>,
4546
) -> Result<PasswordHash<'a>> {
4647
let algorithm = Algorithm::try_from(alg_id.unwrap_or(PBKDF2_SHA256))?;
48+
49+
// Versions unsupported
50+
if version.is_some() {
51+
return Err(Error::Version);
52+
}
53+
4754
let salt = salt.into();
4855
let mut salt_arr = [0u8; 64];
4956
let salt_bytes = salt.b64_decode(&mut salt_arr)?;

pbkdf2/tests/simple.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn hash_with_default_algorithm() {
3030
};
3131

3232
let hash = Pbkdf2
33-
.hash_password(PASSWORD.as_bytes(), None, params, salt)
33+
.hash_password_customized(PASSWORD.as_bytes(), None, None, params, salt)
3434
.unwrap();
3535

3636
assert_eq!(hash.algorithm, Algorithm::Pbkdf2Sha256.ident());

scrypt/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! let salt = SaltString::generate(&mut OsRng);
2626
//!
2727
//! // Hash password to PHC string ($scrypt$...)
28-
//! let password_hash = Scrypt.hash_password_simple(password, salt.as_ref()).unwrap().to_string();
28+
//! let password_hash = Scrypt.hash_password(password, salt.as_ref()).unwrap().to_string();
2929
//!
3030
//! // Verify password against PHC string
3131
//! let parsed_hash = PasswordHash::new(&password_hash).unwrap();

scrypt/src/simple.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{scrypt, Params};
44
use base64ct::{Base64, Encoding};
55
use core::convert::TryInto;
66
use password_hash::{
7-
errors::InvalidValue, Error, Ident, McfHasher, Output, PasswordHash, PasswordHasher, Result,
8-
Salt,
7+
errors::InvalidValue, Decimal, Error, Ident, McfHasher, Output, PasswordHash, PasswordHasher,
8+
Result, Salt,
99
};
1010

1111
/// Algorithm identifier
@@ -19,16 +19,21 @@ pub struct Scrypt;
1919
impl PasswordHasher for Scrypt {
2020
type Params = Params;
2121

22-
fn hash_password<'a>(
22+
fn hash_password_customized<'a>(
2323
&self,
2424
password: &[u8],
2525
alg_id: Option<Ident<'a>>,
26+
version: Option<Decimal>,
2627
params: Params,
2728
salt: impl Into<Salt<'a>>,
2829
) -> Result<PasswordHash<'a>> {
29-
match alg_id {
30-
Some(ALG_ID) | None => (),
31-
_ => return Err(Error::Algorithm),
30+
if !matches!(alg_id, Some(ALG_ID) | None) {
31+
return Err(Error::Algorithm);
32+
}
33+
34+
// Versions unsupported
35+
if version.is_some() {
36+
return Err(Error::Version);
3237
}
3338

3439
let salt = salt.into();

0 commit comments

Comments
 (0)