Skip to content

Commit

Permalink
Merge pull request #13 from ciffelia/zeroize
Browse files Browse the repository at this point in the history
feat: add `zeroize` feature
  • Loading branch information
ciffelia authored Apr 14, 2024
2 parents 2fc32a2 + 04af205 commit 8e0bb51
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude = ["/.github", "/.gitignore"]
thiserror = "1.0"
serde = { version = "1.0", optional = true }
rand = "0.8.5"
zeroize = { version = "1.7", features = ["zeroize_derive"], optional = true }

[dev-dependencies]
serde_json = "1.0"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ let id: Nanoid<9, Base62Alphabet> = "abc123XYZ".parse()?;

## Features

- `serde`: Enable serialization and deserialization of [`Nanoid`] using the [`serde`](https://docs.rs/serde) crate.
- `serde`: Add support for serialization and deserialization of [`Nanoid`] using the [`serde`](https://docs.rs/serde) crate.
- `zeroize`: Add support for zeroizing the memory of [`Nanoid`] using the [`zeroize`](https://docs.rs/zeroize) crate.

## Comparison with other implementations of Nano ID

Expand Down
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
//!
//! # Features
//!
//! - `serde`: Enable serialization and deserialization of [`Nanoid`] using the [`serde`](https://docs.rs/serde) crate.
//! - `serde`: Add support for serialization and deserialization of [`Nanoid`] using the [`serde`](https://docs.rs/serde) crate.
//! - `zeroize`: Add support for zeroizing the memory of [`Nanoid`] using the [`zeroize`](https://docs.rs/zeroize) crate.
//!
//! # Comparison with other implementations of Nano ID
//!
Expand Down Expand Up @@ -163,6 +164,7 @@ use alphabet::{Alphabet, AlphabetExt, Base64UrlAlphabet};
/// let id: Nanoid<9, Base62Alphabet> = "abc123XYZ".parse()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg_attr(feature = "zeroize", derive(zeroize::Zeroize))]
pub struct Nanoid<const N: usize = 21, A: Alphabet = Base64UrlAlphabet> {
/// The Nano ID string. All characters are ASCII.
inner: [u8; N],
Expand Down Expand Up @@ -762,6 +764,24 @@ mod tests {
inner::<12, Base58Alphabet>("\"abcdefghijkl\"");
}

#[cfg(feature = "zeroize")]
#[test]
fn test_zeroize() {
use zeroize::Zeroize;

fn inner<const N: usize, A: Alphabet>(s: &str) {
let mut id: Nanoid<N, A> = s.parse().unwrap();
id.zeroize();
}

inner::<21, Base64UrlAlphabet>("ABCDEFGHIJKLMNOPQ123_");
inner::<21, Base62Alphabet>("ABCDEFGHIJKLMNOPQ1234");
inner::<21, Base58Alphabet>("ABCDEFGHJKLMNPQ123456");
inner::<6, Base64UrlAlphabet>("abc12-");
inner::<10, Base62Alphabet>("abc1234XYZ");
inner::<12, Base58Alphabet>("abc123XYZ123");
}

#[test]
fn test_nanoid_macro() {
{
Expand Down

0 comments on commit 8e0bb51

Please sign in to comment.