Skip to content

Commit 4c59f8d

Browse files
authored
mcf: add push_field* methods to McfHash (#1923)
Support for pushing additional fields onto an MCF hash string: - `push_field`: pushes a `Field` - `push_field_base64`: encodes a `&[u8]` using the given Base64 alphabet
1 parent 73e513e commit 4c59f8d

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

mcf/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ impl McfHash {
8585

8686
fields
8787
}
88+
89+
/// Push an additional field onto the password hash string.
90+
pub fn push_field(&mut self, field: Field<'_>) {
91+
self.0.push(fields::DELIMITER);
92+
self.0.push_str(field.as_str());
93+
}
94+
95+
/// Push an additional field onto the password hash string, encoding it first as Base64.
96+
pub fn push_field_base64(&mut self, field: &[u8], base64_encoding: Base64) {
97+
self.0.push(fields::DELIMITER);
98+
self.0.push_str(&base64_encoding.encode_string(field));
99+
}
88100
}
89101

90102
#[cfg(feature = "alloc")]

mcf/tests/mcf.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
use hex_literal::hex;
66
use mcf::{Base64, McfHash};
77

8+
const SHA512_HASH: &str = "$6$rounds=100000$exn6tVc2j/MZD8uG$BI1Xh8qQSK9J4m14uwy7abn.ctj/TIAzlaVCto0MQrOFIeTXsc1iwzH16XEWo/a7c7Y9eVJvufVzYAs4EsPOy0";
9+
10+
const EXAMPLE_SALT: &[u8] = &hex!("6a3f237988126f80958fa24b");
11+
const EXAMPLE_HASH: &[u8] = &hex!(
12+
"0d358cad62739eb554863c183aef27e6390368fe061fc5fcb1193a392d60dcad4594fa8d383ab8fc3f0dc8088974602668422e6a58edfa1afe24831b10be69be"
13+
);
14+
815
#[test]
916
fn parse_malformed() {
1017
assert!("Hello, world!".parse::<McfHash>().is_err());
@@ -20,8 +27,7 @@ fn parse_malformed() {
2027

2128
#[test]
2229
fn parse_sha512_hash() {
23-
let s = "$6$rounds=100000$exn6tVc2j/MZD8uG$BI1Xh8qQSK9J4m14uwy7abn.ctj/TIAzlaVCto0MQrOFIeTXsc1iwzH16XEWo/a7c7Y9eVJvufVzYAs4EsPOy0";
24-
let hash: McfHash = s.parse().unwrap();
30+
let hash: McfHash = SHA512_HASH.parse().unwrap();
2531
assert_eq!("6", hash.id());
2632

2733
let mut fields = hash.fields();
@@ -31,7 +37,7 @@ fn parse_sha512_hash() {
3137
assert_eq!("exn6tVc2j/MZD8uG", salt.as_str());
3238

3339
let salt_bytes = salt.decode_base64(Base64::ShaCrypt).unwrap();
34-
assert_eq!(&hex!("6a3f237988126f80958fa24b"), salt_bytes.as_slice());
40+
assert_eq!(EXAMPLE_SALT, salt_bytes.as_slice());
3541

3642
let hash = fields.next().unwrap();
3743
assert_eq!(
@@ -40,12 +46,15 @@ fn parse_sha512_hash() {
4046
);
4147

4248
let hash_bytes = hash.decode_base64(Base64::ShaCrypt).unwrap();
43-
assert_eq!(
44-
&hex!(
45-
"0d358cad62739eb554863c183aef27e6390368fe061fc5fcb1193a392d60dcad4594fa8d383ab8fc3f0dc8088974602668422e6a58edfa1afe24831b10be69be"
46-
),
47-
hash_bytes.as_slice()
48-
);
49+
assert_eq!(EXAMPLE_HASH, hash_bytes.as_slice());
4950

5051
assert_eq!(None, fields.next());
5152
}
53+
54+
#[test]
55+
fn push_fields() {
56+
let mut hash = McfHash::new("$6$rounds=100000").unwrap();
57+
hash.push_field_base64(EXAMPLE_SALT, Base64::ShaCrypt);
58+
hash.push_field_base64(EXAMPLE_HASH, Base64::ShaCrypt);
59+
assert_eq!(SHA512_HASH, hash.as_str());
60+
}

0 commit comments

Comments
 (0)