diff --git a/Cargo.lock b/Cargo.lock index 11f1553..f179910 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,7 @@ dependencies = [ "serde", "serde_json", "thiserror", + "zeroize", ] [[package]] @@ -197,3 +198,23 @@ name = "yansi" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 84b538f..d4a6607 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 879e07a..1ea5905 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 12d1b24..55b7064 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 //! @@ -163,6 +164,7 @@ use alphabet::{Alphabet, AlphabetExt, Base64UrlAlphabet}; /// let id: Nanoid<9, Base62Alphabet> = "abc123XYZ".parse()?; /// # Ok::<(), Box>(()) /// ``` +#[cfg_attr(feature = "zeroize", derive(zeroize::Zeroize))] pub struct Nanoid { /// The Nano ID string. All characters are ASCII. inner: [u8; N], @@ -762,6 +764,24 @@ mod tests { inner::<12, Base58Alphabet>("\"abcdefghijkl\""); } + #[cfg(feature = "zeroize")] + #[test] + fn test_zeroize() { + use zeroize::Zeroize; + + fn inner(s: &str) { + let mut id: Nanoid = 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() { {