Skip to content

Commit 2c3e02f

Browse files
authored
mcf: introduce basic no-alloc profile (#1915)
Support for using the `Fields`/`Field` types in heapless environments. Ideally we'd support something like `McfHashRef` for parsing MCF from a `str` input, but this PR doesn't add that for now.
1 parent bf50c2e commit 2c3e02f

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

mcf/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ description = """
1313
Pure Rust implementation of the Modular Crypt Format (MCF) which is used to store password hashes
1414
in the form `${id}$...`
1515
"""
16+
17+
[features]
18+
default = ["alloc"]
19+
alloc = []

mcf/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ dual licensed as above, without any additional terms or conditions.
5858

5959
[//]: # (badges)
6060

61-
[crate-image]: https://img.shields.io/crates/v/mcf
61+
[crate-image]: https://img.shields.io/crates/v/mcf?logo=rust
6262
[crate-link]: https://crates.io/crates/mcf
6363
[docs-image]: https://docs.rs/mcf/badge.svg
6464
[docs-link]: https://docs.rs/mcf/

mcf/src/fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Fields<'a>(&'a str);
1212
impl<'a> Fields<'a> {
1313
/// Create a new field iterator from an MCF hash, returning an error in the event the hash
1414
/// doesn't start with a leading `$` prefix.
15-
pub(crate) fn new(s: &'a str) -> Result<Self> {
15+
pub fn new(s: &'a str) -> Result<Self> {
1616
let mut ret = Self(s);
1717

1818
if ret.next() != Some(Field("")) {

mcf/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
unused_qualifications
1414
)]
1515

16+
#[cfg(feature = "alloc")]
1617
extern crate alloc;
1718

1819
mod fields;
1920

2021
pub use fields::{Field, Fields};
2122

22-
use alloc::string::String;
23-
use core::{fmt, str};
23+
use core::fmt;
24+
25+
#[cfg(feature = "alloc")]
26+
use {alloc::string::String, core::str};
2427

2528
/// Debug message used in panics when invariants aren't properly held.
29+
#[cfg(feature = "alloc")]
2630
const INVARIANT_MSG: &str = "should be ensured valid by constructor";
2731

2832
/// Modular Crypt Format (MCF) serialized password hash.
@@ -39,8 +43,10 @@ const INVARIANT_MSG: &str = "should be ensured valid by constructor";
3943
/// ```text
4044
/// $6$rounds=100000$exn6tVc2j/MZD8uG$BI1Xh8qQSK9J4m14uwy7abn.ctj/TIAzlaVCto0MQrOFIeTXsc1iwzH16XEWo/a7c7Y9eVJvufVzYAs4EsPOy0
4145
/// ```
46+
#[cfg(feature = "alloc")]
4247
pub struct McfHash(String);
4348

49+
#[cfg(feature = "alloc")]
4450
impl McfHash {
4551
/// Parse the given input string, returning an [`McfHash`] if valid.
4652
pub fn new(s: impl Into<String>) -> Result<McfHash> {
@@ -76,18 +82,21 @@ impl McfHash {
7682
}
7783
}
7884

85+
#[cfg(feature = "alloc")]
7986
impl AsRef<str> for McfHash {
8087
fn as_ref(&self) -> &str {
8188
self.as_str()
8289
}
8390
}
8491

92+
#[cfg(feature = "alloc")]
8593
impl fmt::Display for McfHash {
8694
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8795
f.write_str(self.as_str())
8896
}
8997
}
9098

99+
#[cfg(feature = "alloc")]
91100
impl str::FromStr for McfHash {
92101
type Err = Error;
93102

@@ -97,6 +106,7 @@ impl str::FromStr for McfHash {
97106
}
98107

99108
/// Perform validations that the given string is well-formed MCF.
109+
#[cfg(feature = "alloc")]
100110
fn validate(s: &str) -> Result<()> {
101111
// Validates the hash begins with a leading `$`
102112
let mut fields = Fields::new(s)?;
@@ -124,6 +134,7 @@ fn validate(s: &str) -> Result<()> {
124134
///
125135
/// Allowed characters match the regex: `[a-z0-9\-]`, where the first and last characters do NOT
126136
/// contain a `-`.
137+
#[cfg(feature = "alloc")]
127138
fn validate_id(id: &str) -> Result<()> {
128139
let first = id.chars().next().ok_or(Error {})?;
129140
let last = id.chars().last().ok_or(Error {})?;

mcf/tests/mcf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Modular Crypt Format integration tests.
22
3+
#![cfg(feature = "alloc")]
4+
35
use mcf::McfHash;
46

57
#[test]

0 commit comments

Comments
 (0)