Skip to content

Commit 17d792f

Browse files
authored
mcf: rename PasswordHash/PasswordHashRef (#2031)
Renames the following: - `McfHash` => `PasswordHash` - `McfHashRef` => `PasswordHashRef` This makes it much clearer the types represent password hash strings
1 parent 511607f commit 17d792f

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

mcf/src/lib.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ pub use error::{Error, Result};
2525
pub use fields::{Field, Fields};
2626

2727
#[cfg(feature = "alloc")]
28-
pub use allocating::McfHash;
28+
pub use allocating::PasswordHash;
2929

3030
/// Debug message used in panics when invariants aren't properly held.
3131
const INVARIANT_MSG: &str = "should be ensured valid by constructor";
3232

33-
/// Zero-copy decoder for hashes in the Modular Crypt Format (MCF).
33+
/// Password hash reference type for hashes encoded in the Modular Crypt Format (MCF),
34+
/// e.g. `$<id>$...`.
3435
///
35-
/// For more information, see [`McfHash`].
36+
/// For more information, see [`PasswordHash`].
3637
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
37-
pub struct McfHashRef<'a>(&'a str);
38+
pub struct PasswordHashRef<'a>(&'a str);
3839

39-
impl<'a> McfHashRef<'a> {
40-
/// Parse the given input string, returning an [`McfHashRef`] if valid.
40+
impl<'a> PasswordHashRef<'a> {
41+
/// Parse the given input string, returning an [`PasswordHashRef`] if valid.
4142
pub fn new(s: &'a str) -> Result<Self> {
4243
validate(s)?;
4344
Ok(Self(s))
@@ -69,20 +70,20 @@ impl<'a> McfHashRef<'a> {
6970
}
7071
}
7172

72-
impl<'a> From<McfHashRef<'a>> for &'a str {
73-
fn from(hash: McfHashRef<'a>) -> &'a str {
73+
impl<'a> From<PasswordHashRef<'a>> for &'a str {
74+
fn from(hash: PasswordHashRef<'a>) -> &'a str {
7475
hash.0
7576
}
7677
}
7778

7879
#[cfg(feature = "alloc")]
79-
impl From<McfHashRef<'_>> for alloc::string::String {
80-
fn from(hash: McfHashRef<'_>) -> Self {
80+
impl From<PasswordHashRef<'_>> for alloc::string::String {
81+
fn from(hash: PasswordHashRef<'_>) -> Self {
8182
hash.0.into()
8283
}
8384
}
8485

85-
impl<'a> TryFrom<&'a str> for McfHashRef<'a> {
86+
impl<'a> TryFrom<&'a str> for PasswordHashRef<'a> {
8687
type Error = Error;
8788

8889
fn try_from(s: &'a str) -> Result<Self> {
@@ -92,11 +93,14 @@ impl<'a> TryFrom<&'a str> for McfHashRef<'a> {
9293

9394
#[cfg(feature = "alloc")]
9495
mod allocating {
95-
use crate::{Base64, Error, Field, Fields, McfHashRef, Result, fields, validate, validate_id};
96+
use crate::{
97+
Base64, Error, Field, Fields, PasswordHashRef, Result, fields, validate, validate_id,
98+
};
9699
use alloc::string::String;
97100
use core::{fmt, str};
98101

99-
/// Modular Crypt Format (MCF) serialized password hash.
102+
/// Password hash encoded in the Modular Crypt Format (MCF). Owned form with builder
103+
/// functionality.
100104
///
101105
/// Password hashes in this format take the form `${id}$...`, where `{id}` is a short numeric or
102106
/// alphanumeric algorithm identifier optionally containing a `-`, followed by `$` as a delimiter,
@@ -111,25 +115,25 @@ mod allocating {
111115
/// $6$rounds=100000$exn6tVc2j/MZD8uG$BI1Xh8qQSK9J4m14uwy7abn.ctj/TIAzlaVCto0MQrOFIeTXsc1iwzH16XEWo/a7c7Y9eVJvufVzYAs4EsPOy0
112116
/// ```
113117
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
114-
pub struct McfHash(String);
118+
pub struct PasswordHash(String);
115119

116-
impl McfHash {
117-
/// Parse the given input string, returning an [`McfHash`] if valid.
118-
pub fn new(s: impl Into<String>) -> Result<McfHash> {
120+
impl PasswordHash {
121+
/// Parse the given input string, returning an [`PasswordHash`] if valid.
122+
pub fn new(s: impl Into<String>) -> Result<PasswordHash> {
119123
let s = s.into();
120124
validate(&s)?;
121125
Ok(Self(s))
122126
}
123127

124-
/// Create an [`McfHash`] from an identifier.
128+
/// Create an [`PasswordHash`] from an identifier.
125129
///
126130
/// # Returns
127131
///
128132
/// Error if the identifier is invalid.
129133
///
130134
/// Allowed characters match the regex: `[a-z0-9\-]`, where the first and last characters do NOT
131135
/// contain a `-`.
132-
pub fn from_id(id: &str) -> Result<McfHash> {
136+
pub fn from_id(id: &str) -> Result<PasswordHash> {
133137
validate_id(id)?;
134138

135139
// TODO(tarcieri): overestimate capacity so most password hashes fit?
@@ -144,9 +148,9 @@ mod allocating {
144148
&self.0
145149
}
146150

147-
/// Get an [`McfHashRef`] which corresponds to this owned [`McfHash`].
148-
pub fn as_mcf_hash_ref(&self) -> McfHashRef<'_> {
149-
McfHashRef(self.as_str())
151+
/// Get an [`PasswordHashRef`] which corresponds to this owned [`PasswordHash`].
152+
pub fn as_mcf_hash_ref(&self) -> PasswordHashRef<'_> {
153+
PasswordHashRef(self.as_str())
150154
}
151155

152156
/// Get the algorithm identifier for this MCF hash.
@@ -186,47 +190,47 @@ mod allocating {
186190
}
187191
}
188192

189-
impl<'a> AsRef<str> for McfHashRef<'a> {
193+
impl<'a> AsRef<str> for PasswordHashRef<'a> {
190194
fn as_ref(&self) -> &str {
191195
self.as_str()
192196
}
193197
}
194198

195-
impl AsRef<str> for McfHash {
199+
impl AsRef<str> for PasswordHash {
196200
fn as_ref(&self) -> &str {
197201
self.as_str()
198202
}
199203
}
200204

201-
impl From<McfHash> for String {
202-
fn from(hash: McfHash) -> Self {
205+
impl From<PasswordHash> for String {
206+
fn from(hash: PasswordHash) -> Self {
203207
hash.0
204208
}
205209
}
206210

207-
impl TryFrom<String> for McfHash {
211+
impl TryFrom<String> for PasswordHash {
208212
type Error = Error;
209213

210214
fn try_from(s: String) -> Result<Self> {
211215
Self::new(s)
212216
}
213217
}
214218

215-
impl TryFrom<&str> for McfHash {
219+
impl TryFrom<&str> for PasswordHash {
216220
type Error = Error;
217221

218222
fn try_from(s: &str) -> Result<Self> {
219223
Self::new(s)
220224
}
221225
}
222226

223-
impl fmt::Display for McfHash {
227+
impl fmt::Display for PasswordHash {
224228
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225229
f.write_str(self.as_str())
226230
}
227231
}
228232

229-
impl str::FromStr for McfHash {
233+
impl str::FromStr for PasswordHash {
230234
type Err = Error;
231235

232236
fn from_str(s: &str) -> Result<Self> {

mcf/tests/mcf.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![cfg(feature = "alloc")]
44

55
use hex_literal::hex;
6-
use mcf::{Base64, McfHash};
6+
use mcf::{Base64, PasswordHash};
77

88
const SHA512_HASH: &str = "$6$rounds=100000$exn6tVc2j/MZD8uG$BI1Xh8qQSK9J4m14uwy7abn.ctj/TIAzlaVCto0MQrOFIeTXsc1iwzH16XEWo/a7c7Y9eVJvufVzYAs4EsPOy0";
99

@@ -14,31 +14,31 @@ const EXAMPLE_HASH: &[u8] = &hex!(
1414

1515
#[test]
1616
fn from_id() {
17-
let mcf_hash = McfHash::from_id("6").unwrap();
17+
let mcf_hash = PasswordHash::from_id("6").unwrap();
1818
assert_eq!("$6", mcf_hash.as_str());
1919
}
2020

2121
#[test]
2222
fn parse_malformed() {
23-
assert!("Hello, world!".parse::<McfHash>().is_err());
24-
assert!("$".parse::<McfHash>().is_err());
25-
assert!("$$".parse::<McfHash>().is_err());
26-
assert!("$$foo".parse::<McfHash>().is_err());
27-
assert!("$foo$".parse::<McfHash>().is_err());
28-
assert!("$-$foo".parse::<McfHash>().is_err());
29-
assert!("$foo-$bar".parse::<McfHash>().is_err());
30-
assert!("$-foo$bar".parse::<McfHash>().is_err());
23+
assert!("Hello, world!".parse::<PasswordHash>().is_err());
24+
assert!("$".parse::<PasswordHash>().is_err());
25+
assert!("$$".parse::<PasswordHash>().is_err());
26+
assert!("$$foo".parse::<PasswordHash>().is_err());
27+
assert!("$foo$".parse::<PasswordHash>().is_err());
28+
assert!("$-$foo".parse::<PasswordHash>().is_err());
29+
assert!("$foo-$bar".parse::<PasswordHash>().is_err());
30+
assert!("$-foo$bar".parse::<PasswordHash>().is_err());
3131
}
3232

3333
#[test]
3434
fn parse_id_only() {
35-
let hash: McfHash = "$6".parse().unwrap();
35+
let hash: PasswordHash = "$6".parse().unwrap();
3636
assert_eq!("6", hash.id());
3737
}
3838

3939
#[test]
4040
fn parse_sha512_hash() {
41-
let hash: McfHash = SHA512_HASH.parse().unwrap();
41+
let hash: PasswordHash = SHA512_HASH.parse().unwrap();
4242
assert_eq!("6", hash.id());
4343

4444
let mut fields = hash.fields();
@@ -64,7 +64,7 @@ fn parse_sha512_hash() {
6464

6565
#[test]
6666
fn push_fields() {
67-
let mut hash = McfHash::new("$6$rounds=100000").unwrap();
67+
let mut hash = PasswordHash::new("$6$rounds=100000").unwrap();
6868
hash.push_base64(EXAMPLE_SALT, Base64::ShaCrypt);
6969
hash.push_base64(EXAMPLE_HASH, Base64::ShaCrypt);
7070
assert_eq!(SHA512_HASH, hash.as_str());

0 commit comments

Comments
 (0)