Skip to content

Commit

Permalink
password-hash: add encoding support to PasswordHash (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored Feb 1, 2021
1 parent d49eb6b commit e29a8f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion password-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub struct PasswordHash<'a> {
impl<'a> PasswordHash<'a> {
/// Parse a password hash from a string in the PHC string format.
pub fn new(s: &'a str) -> Result<Self, HashError> {
Self::parse(s, Encoding::B64)
}

/// Parse a password hash from the given [`Encoding`].
pub fn parse(s: &'a str, encoding: Encoding) -> Result<Self, HashError> {
if s.is_empty() {
return Err(ParseError::Empty.into());
}
Expand Down Expand Up @@ -301,7 +306,7 @@ impl<'a> PasswordHash<'a> {
}

if let Some(field) = fields.next() {
hash = Some(field.parse()?);
hash = Some(Output::decode(field, encoding)?);
}

if fields.next().is_some() {
Expand Down Expand Up @@ -341,6 +346,11 @@ impl<'a> PasswordHash<'a> {

Err(VerifyError)
}

/// Get the [`Encoding`] that this [`PasswordHash`] is serialized with.
pub fn encoding(&self) -> Encoding {
self.hash.map(|h| h.encoding()).unwrap_or_default()
}
}

// Note: this uses `TryFrom` instead of `FromStr` to support a lifetime on
Expand Down
2 changes: 1 addition & 1 deletion password-hash/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Output {
&self.bytes[..self.len()]
}

/// Get the encoding that this [`Output`] will be serialized with.
/// Get the [`Encoding`] that this [`Output`] is serialized with.
pub fn encoding(&self) -> Encoding {
self.encoding
}
Expand Down

0 comments on commit e29a8f5

Please sign in to comment.