| Crates.io | bip39_mnemonic_generator |
| lib.rs | bip39_mnemonic_generator |
| version | 0.1.1 |
| created_at | 2025-01-02 09:21:03.659543+00 |
| updated_at | 2025-01-02 09:32:03.604156+00 |
| description | A simple mnemonic generator |
| homepage | https://github.com/vkpatva/mnemonic-generator |
| repository | https://github.com/vkpatva/mnemonic-generator |
| max_upload_size | |
| id | 1501378 |
| size | 27,986 |
This crate provides a function to generate a BIP-39 mnemonic, which is a sequence of words used for securely storing cryptocurrency keys or other sensitive information. The mnemonic is derived from entropy and can be used for purposes like key generation or recovery.
To use this crate, include it in your Cargo.toml:
[dependencies]
bip39-mnemonic = "0.1"
or use the command :
cargo add bip39_mnemonic_generator
Ensure you add the appropriate version of the crate based on your requirements.
Add the crate to your project:
In your Rust project, include the crate in Cargo.toml as mentioned above.
Use the mnemonic generator:
The crate exposes a function generate_mnemonic that takes the number of words you want in your mnemonic. The function returns a Result with either a space-separated string of words or an error message if the input length is invalid.
use bip39_mnemonic::generate_mnemonic;
fn main() {
let mnemonic_result = generate_mnemonic(12); // 12 words mnemonic
match mnemonic_result {
Ok(mnemonic) => println!("Generated Mnemonic: {}", mnemonic),
Err(err) => eprintln!("Error: {}", err),
}
}
generate_mnemonic(length: usize) -> Result<String, String>:
length: The desired number of words in the mnemonic. This must be a multiple of 3 (12, 15, 18, 21, or 24).Result:
Ok(String) containing the mnemonic, if successful.Err(String) containing an error message if the length is invalid.Mnemonic Generation Process:
The length parameter must be one of the following values:
length parameter must be a multiple of 3 and between 12 and 24 (inclusive).This crate depends on the following libraries:
rand: For generating cryptographically secure random numbers.sha2: For performing SHA-256 hashing to calculate the checksum.You can include these dependencies automatically by using the crate, but make sure you have them in your Cargo.toml if you're using them directly.