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 b878e6b

Browse files
committed
[WIP] password-hash: use phc crate
Replaces the `phc` submodule with a re-export of the `phc` crate. A dependency on the crate is needed to make the blanket impl of `PasswordVerifier` for `PasswordHasher` work (notably the former is object safe, and a similar blanket impl can't be supported for MCF since algorithm-specific rules are required) This also significantly simplifies the `Error` type now that it's only responsible for errors involving the traits, rather than errors from parsing password hashes.
1 parent 2e52446 commit b878e6b

File tree

17 files changed

+84
-2514
lines changed

17 files changed

+84
-2514
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ digest = { path = "digest" }
1818
signature = { path = "signature" }
1919

2020
getrandom = { git = "https://github.com/rust-random/getrandom" }
21+
phc = { git = "https://github.com/RustCrypto/formats" }

password-hash/Cargo.toml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,10 @@ as well as a `no_std`-friendly implementation of the PHC string format
1717
"""
1818

1919
[dependencies]
20-
base64ct = "1.7"
21-
subtle = { version = "2", default-features = false }
22-
23-
# optional dependencies
24-
getrandom = { version = "0.3", optional = true, default-features = false }
25-
rand_core = { version = "0.10.0-rc-2", optional = true, default-features = false }
20+
phc = { version = "0.3.0-pre", optional = true, default-features = false }
2621

2722
[features]
28-
default = ["phc", "rand_core"]
29-
alloc = ["base64ct/alloc"]
30-
getrandom = ["dep:getrandom"]
31-
phc = []
32-
rand_core = ["dep:rand_core"]
23+
alloc = ["phc?/alloc"]
3324

3425
[package.metadata.docs.rs]
3526
all-features = true

password-hash/src/error.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! Error types.
2+
3+
use core::fmt;
4+
5+
/// Result type.
6+
pub type Result<T> = core::result::Result<T, Error>;
7+
8+
/// Password hashing errors.
9+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10+
#[non_exhaustive]
11+
pub enum Error {
12+
/// Unsupported algorithm.
13+
Algorithm,
14+
15+
/// Cryptographic error.
16+
Crypto,
17+
18+
/// Out of memory (heap allocation failure).
19+
OutOfMemory,
20+
21+
/// Output size invalid.
22+
OutputSize,
23+
24+
/// Invalid parameters.
25+
ParamsInvalid,
26+
27+
/// Invalid password.
28+
PasswordInvalid,
29+
30+
/// Invalid salt.
31+
SaltInvalid,
32+
33+
/// Invalid algorithm version.
34+
Version,
35+
}
36+
37+
impl fmt::Display for Error {
38+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
39+
match self {
40+
Self::Algorithm => write!(f, "unsupported algorithm"),
41+
Self::Crypto => write!(f, "cryptographic error"),
42+
Self::OutOfMemory => write!(f, "out of memory"),
43+
Self::OutputSize => write!(f, "password hash output size invalid"),
44+
Self::ParamsInvalid => write!(f, "invalid parameters"),
45+
Self::PasswordInvalid => write!(f, "invalid password"),
46+
Self::SaltInvalid => write!(f, "invalid salt"),
47+
Self::Version => write!(f, "invalid algorithm version"),
48+
}
49+
}
50+
}
51+
52+
impl core::error::Error for Error {}

password-hash/src/errors.rs

Lines changed: 0 additions & 178 deletions
This file was deleted.

password-hash/src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929
#[allow(unused_extern_crates)]
3030
extern crate alloc;
3131

32-
#[cfg(feature = "rand_core")]
33-
pub use rand_core;
32+
mod error;
3433

35-
pub mod errors;
36-
#[cfg(feature = "phc")]
37-
pub mod phc;
34+
pub use crate::error::{Error, Result};
3835

39-
pub use crate::errors::{Error, Result};
36+
#[cfg(feature = "phc")]
37+
pub use phc;
4038

4139
/// DEPRECATED: import this as `password_hash::phc::PasswordHash`.
4240
#[cfg(feature = "phc")]
@@ -46,11 +44,11 @@ pub use crate::errors::{Error, Result};
4644
)]
4745
pub type PasswordHash = phc::PasswordHash;
4846

49-
/// DEPRECATED: import this as `password_hash::phc::PasswordHashString`.
47+
/// DEPRECATED: use `password_hash::phc::PasswordHash` or `String`
5048
#[cfg(all(feature = "alloc", feature = "phc"))]
5149
#[deprecated(
5250
since = "0.6.0",
53-
note = "import as `password_hash::phc::PasswordHashString` instead"
51+
note = "use `password_hash::phc::PasswordHash` or `String`"
5452
)]
5553
pub type PasswordHashString = phc::PasswordHashString;
5654

@@ -97,13 +95,12 @@ pub trait CustomizedPasswordHasher<H> {
9795

9896
/// Trait for password verification.
9997
///
100-
/// Generic around a password hash to be returned (typically [`PasswordHash`])
98+
/// Generic around a password hash to be returned (typically [`phc::PasswordHash`])
10199
///
102-
/// Automatically impl'd for any type that impls [`PasswordHasher`] with [`PasswordHash`] as `H`.
100+
/// Automatically impl'd for type that impl [`PasswordHasher`] with [`phc::PasswordHash`] as `H`.
103101
///
104102
/// This trait is object safe and can be used to implement abstractions over
105-
/// multiple password hashing algorithms. One such abstraction is provided by
106-
/// the [`PasswordHash::verify_password`] method.
103+
/// multiple password hashing algorithms.
107104
pub trait PasswordVerifier<H> {
108105
/// Compute this password hashing function against the provided password
109106
/// using the parameters from the provided password hash and see if the
@@ -135,7 +132,7 @@ impl<T: CustomizedPasswordHasher<phc::PasswordHash>> PasswordVerifier<phc::Passw
135132
_ => (),
136133
}
137134

138-
Err(Error::Password)
135+
Err(Error::PasswordInvalid)
139136
}
140137
}
141138

0 commit comments

Comments
 (0)