| Crates.io | vitaminc-password |
| lib.rs | vitaminc-password |
| version | 0.1.0-pre4 |
| created_at | 2025-11-20 12:25:32.927535+00 |
| updated_at | 2025-11-23 23:26:40.872243+00 |
| description | Secure password generator. Part of the Vitamin-C cryptographic suite. |
| homepage | https://cipherstash.com |
| repository | https://github.com/cipherstash/vitaminc |
| max_upload_size | |
| id | 1941933 |
| size | 25,120 |
Secure password generator with protected memory handling.
This crate is part of the Vitamin C framework to make cryptography code healthy.
vitaminc-random for secure random generationProtected types that zeroize on dropAdd this to your Cargo.toml:
[dependencies]
vitaminc-password = "0.1.0-pre4"
vitaminc-random = "0.1.0-pre4"
use vitaminc_password::Password;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
// Generate a 16-character password with all printable characters
let mut rng = SafeRand::from_entropy();
let password: Password<16> = Generatable::random(&mut rng)?;
Vitamin C Password provides three types of passwords with different character sets:
Password<N> - Uses all 94 printable ASCII charactersAlphaNumericPassword<N> - Uses only letters (A-Z, a-z) and numbers (0-9) - 62 charactersAlphaPassword<N> - Uses only letters (A-Z, a-z) - 52 charactersThe N parameter specifies the password length at compile time.
Includes uppercase, lowercase, numbers, and special characters:
use vitaminc_password::Password;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
let mut rng = SafeRand::from_entropy();
// Generate a 20-character password
let password: Password<20> = Generatable::random(&mut rng)?;
Character set: A-Z, a-z, 0-9, and special characters:
~ ` ! @ # $ % ^ & * ( ) _ - + = { [ } ] | \ : ; " ' < , > . ? /
For systems that don't accept special characters:
use vitaminc_password::AlphaNumericPassword;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
let mut rng = SafeRand::from_entropy();
// Generate a 16-character alphanumeric password
let password: AlphaNumericPassword<16> = Generatable::random(&mut rng)?;
Character set: A-Z, a-z, 0-9 (62 characters)
For maximum compatibility or readability:
use vitaminc_password::AlphaPassword;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
let mut rng = SafeRand::from_entropy();
// Generate a 24-character alphabetic password
let password: AlphaPassword<24> = Generatable::random(&mut rng)?;
Character set: A-Z, a-z (52 characters)
The password length is specified as a const generic parameter. You can generate passwords of any length:
let short: Password<8> = Generatable::random(&mut rng)?;
let medium: Password<16> = Generatable::random(&mut rng)?;
let long: Password<32> = Generatable::random(&mut rng)?;
let very_long: Password<128> = Generatable::random(&mut rng)?;
Passwords are stored securely in Protected types. When you need to use them (e.g., to send to an API or display to the user), you can convert them to strings:
Maintains protection with automatic zeroization:
use vitaminc_protected::Protected;
let password: Password<16> = Generatable::random(&mut rng)?;
let protected_string: Protected<String> = password.into_protected_string();
// Use the protected string...
// It will be zeroized when dropped
Note: This functionality is currently under development (see WIP markers in code).
For final use where the password needs to leave protected memory:
let password: Password<16> = Generatable::random(&mut rng)?;
let string: String = password.into_unprotected_string();
// Use the string immediately
// No automatic zeroization after this point
Important: Once converted to an unprotected String, the password is no longer automatically zeroized. Only use this as a final step right before the password is needed.
The entropy (randomness) of a password depends on both its length and the character set size:
Entropy formula: log2(charset_size^length) bits
Recommendation: For most use cases, 16-20 characters provides excellent security (>100 bits of entropy).
All passwords are generated using vitaminc-random's SafeRand, which uses ChaCha20, a cryptographically secure pseudo-random number generator (CSPRNG).
Passwords are stored in Protected<[char; N]>, which ensures:
Protected { ... } instead of the actual passwordThe crate uses bounded random number generation to ensure each character in the character set has an equal probability of being selected, preventing bias that could reduce password entropy.
Generate secure passwords for user accounts:
use vitaminc_password::Password;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
fn generate_user_password(length: usize) -> Result<String, RandomError> {
let mut rng = SafeRand::from_entropy();
match length {
16 => {
let password: Password<16> = Generatable::random(&mut rng)?;
Ok(password.into_unprotected_string())
},
20 => {
let password: Password<20> = Generatable::random(&mut rng)?;
Ok(password.into_unprotected_string())
},
32 => {
let password: Password<32> = Generatable::random(&mut rng)?;
Ok(password.into_unprotected_string())
},
_ => panic!("Unsupported length"),
}
}
Generate alphanumeric API keys:
use vitaminc_password::AlphaNumericPassword;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
fn generate_api_key() -> Result<String, RandomError> {
let mut rng = SafeRand::from_entropy();
let key: AlphaNumericPassword<32> = Generatable::random(&mut rng)?;
Ok(key.into_unprotected_string())
}
Generate pronounceable temporary passwords:
use vitaminc_password::AlphaPassword;
use vitaminc_random::{SafeRand, SeedableRng, Generatable};
fn generate_temp_password() -> Result<String, RandomError> {
let mut rng = SafeRand::from_entropy();
let password: AlphaPassword<12> = Generatable::random(&mut rng)?;
Ok(password.into_unprotected_string())
}
Password for maximum security, AlphaNumericPassword for compatibilityProtected wrapper prevents accidental logging, but be careful after conversionThis crate is currently in early development (pre-release). Some functionality is not yet complete:
into_protected_string() is not yet implementedinto_unprotected_string() is not yet implemented#[ignore] pending full implementationCheck the GitHub repository for the latest development status.
Future enhancements under consideration:
into_protected_string() implementationVitamin C is brought to you by the team at CipherStash.
License: MIT