Skip to content

Commit 604171b

Browse files
authored
password-hash: enable and fix workspace-level lints (#2333)
Note: lint config was added in #2270
1 parent 42f8110 commit 604171b

3 files changed

Lines changed: 52 additions & 7 deletions

File tree

password-hash/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ alloc = ["phc?/alloc"]
2626
getrandom = ["dep:getrandom", "phc?/getrandom"]
2727
rand_core = ["dep:rand_core", "phc?/rand_core"]
2828

29+
[lints]
30+
workspace = true
31+
2932
[package.metadata.docs.rs]
3033
all-features = true

password-hash/src/lib.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,23 @@ pub trait PasswordHasher<H> {
8686
///
8787
/// The salt should be unique per password. When in doubt, use [`PasswordHasher::hash_password`]
8888
/// which will choose the salt for you.
89+
///
90+
/// # Errors
91+
/// These will vary by algorithm/implementation of this trait, but may be due to:
92+
/// - length restrictions on the password and/or salt
93+
/// - algorithm-specific internal error
8994
fn hash_password_with_salt(&self, password: &[u8], salt: &[u8]) -> Result<H>;
9095

9196
/// Compute the hash `H` from the given password, potentially using configuration stored in
9297
/// `&self` for the parameters, or otherwise the recommended defaults.
9398
///
9499
/// A large random salt will be generated automatically.
100+
///
101+
/// # Errors
102+
/// These will vary by algorithm/implementation of this trait, but may be due to:
103+
/// - length restrictions on the password
104+
/// - algorithm-specific internal error
105+
/// - RNG internal error
95106
#[cfg(feature = "getrandom")]
96107
fn hash_password(&self, password: &[u8]) -> Result<H> {
97108
let salt = try_generate_salt()?;
@@ -102,6 +113,12 @@ pub trait PasswordHasher<H> {
102113
/// `&self` for the parameters, or otherwise the recommended defaults.
103114
///
104115
/// A large random salt will be generated automatically from the provided RNG.
116+
///
117+
/// # Errors
118+
/// These will vary by algorithm/implementation of this trait, but may be due to:
119+
/// - length restrictions on the password
120+
/// - algorithm-specific internal error
121+
/// - RNG internal error
105122
#[cfg(feature = "rand_core")]
106123
fn hash_password_with_rng<R: TryCryptoRng + ?Sized>(
107124
&self,
@@ -121,11 +138,15 @@ pub trait CustomizedPasswordHasher<H> {
121138
/// Algorithm-specific parameters.
122139
type Params: Clone + Debug + Default;
123140

124-
/// Compute a [`PasswordHash`] from the provided password using an
125-
/// explicit set of customized algorithm parameters as opposed to the
126-
/// defaults.
141+
/// Compute a [`PasswordHash`] from the provided password using an explicit set of customized
142+
/// algorithm parameters as opposed to the defaults.
127143
///
128144
/// When in doubt, use [`PasswordHasher::hash_password`] instead.
145+
///
146+
/// # Errors
147+
/// These will vary by algorithm/implementation of this trait, but may be due to:
148+
/// - length restrictions on the password and/or salt
149+
/// - algorithm-specific params or internal error
129150
fn hash_password_customized(
130151
&self,
131152
password: &[u8],
@@ -137,6 +158,11 @@ pub trait CustomizedPasswordHasher<H> {
137158

138159
/// Compute a [`PasswordHash`] using customized parameters only, using the default
139160
/// algorithm and version.
161+
///
162+
/// # Errors
163+
/// These will vary by algorithm/implementation of this trait, but may be due to:
164+
/// - length restrictions on the password and/or salt
165+
/// - algorithm-specific params or internal error
140166
fn hash_password_with_params(
141167
&self,
142168
password: &[u8],
@@ -156,9 +182,14 @@ pub trait CustomizedPasswordHasher<H> {
156182
/// This trait is object safe and can be used to implement abstractions over
157183
/// multiple password hashing algorithms.
158184
pub trait PasswordVerifier<H: ?Sized> {
159-
/// Compute this password hashing function against the provided password
160-
/// using the parameters from the provided password hash and see if the
161-
/// computed output matches.
185+
/// Compute this password hashing function against the provided password using the parameters
186+
/// from the provided password hash and see if the computed output matches.
187+
///
188+
/// # Errors
189+
/// - Returns `Error::Algorithm` if the algorithm being requested by the hash `H` is unsupported
190+
/// - Returns `Error::PasswordInvalid` if the hash for the supplied password does not match
191+
/// the provided hash
192+
/// - May return other algorithm-specific errors
162193
fn verify_password(&self, password: &[u8], hash: &H) -> Result<()>;
163194
}
164195

@@ -207,17 +238,27 @@ pub trait McfHasher {
207238
///
208239
/// MCF hashes are otherwise largely unstructured and parsed according to
209240
/// algorithm-specific rules so hashers must parse a raw string themselves.
241+
///
242+
/// # Errors
243+
/// Returns errors if the the hash couldn't be converted
210244
fn upgrade_mcf_hash(&self, hash: &str) -> Result<phc::PasswordHash>;
211245
}
212246

213247
/// Generate a random salt value of the recommended length using the system's secure RNG.
248+
///
249+
/// # Panics
250+
/// If the system's secure RNG experiences an internal failure.
214251
#[cfg(feature = "getrandom")]
252+
#[must_use]
215253
pub fn generate_salt() -> [u8; RECOMMENDED_SALT_LEN] {
216254
try_generate_salt().expect("RNG failure")
217255
}
218256

219257
/// Try generating a random salt value of the recommended length using the system's secure RNG,
220258
/// returning errors if they occur.
259+
///
260+
/// # Errors
261+
/// If the system's secure RNG experiences an internal failure.
221262
#[cfg(feature = "getrandom")]
222263
pub fn try_generate_salt() -> core::result::Result<[u8; RECOMMENDED_SALT_LEN], getrandom::Error> {
223264
let mut salt = [0u8; RECOMMENDED_SALT_LEN];

password-hash/tests/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Password hashing tests
22
33
#![cfg(feature = "phc")]
4+
#![allow(missing_copy_implementations, missing_debug_implementations)]
45

56
use core::{fmt::Display, str::FromStr};
67
use password_hash::{CustomizedPasswordHasher, Error, PasswordHasher, Result};
@@ -58,7 +59,7 @@ impl PasswordHasher<PasswordHash> for StubPasswordHasher {
5859
pub struct StubParams;
5960

6061
impl Display for StubParams {
61-
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62+
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6263
Ok(())
6364
}
6465
}

0 commit comments

Comments
 (0)