Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mcf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<'a> TryFrom<&'a str> for PasswordHashRef<'a> {
#[cfg(feature = "alloc")]
mod allocating {
use crate::{Error, Field, Fields, PasswordHashRef, Result, fields, validate, validate_id};
use alloc::string::String;
use alloc::string::{String, ToString};
use core::{fmt, str};

#[cfg(feature = "base64")]
Expand Down Expand Up @@ -181,6 +181,13 @@ mod allocating {
self.0.push_str(&base64_encoding.encode_string(field));
}

/// Push a type which impls [`fmt::Display`], first adding a `$` delimiter and ensuring the
/// added characters comprise a valid field.
pub fn push_displayable<D: fmt::Display>(&mut self, displayable: D) -> Result<()> {
// TODO(tarcieri): avoid intermediate allocation?
self.push_str(&displayable.to_string())
}

/// Push an additional field onto the password hash string, first adding a `$` delimiter.
pub fn push_field(&mut self, field: Field<'_>) {
self.0.push(fields::DELIMITER);
Expand Down
9 changes: 8 additions & 1 deletion mcf/tests/mcf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ fn parse_sha512_hash() {

#[cfg(feature = "base64")]
#[test]
fn push_fields() {
fn push_base64() {
let mut hash = PasswordHash::new("$6$rounds=100000").unwrap();
hash.push_base64(EXAMPLE_SALT, Base64::ShaCrypt);
hash.push_base64(EXAMPLE_HASH, Base64::ShaCrypt);
assert_eq!(SHA512_HASH, hash.as_str());
}

#[test]
fn push_displayable() {
let mut hash = PasswordHash::from_id("6").unwrap();
hash.push_displayable("rounds=100000").unwrap();
assert_eq!("$6$rounds=100000", hash.as_str());
}