add base32 IDs

main
_ 2025-02-17 14:48:52 -06:00
parent cb68bc8f3f
commit 726dcba58b
1 changed files with 21 additions and 4 deletions

View File

@ -1,15 +1,32 @@
use rand::{seq::IndexedRandom as _, Rng as _};
pub fn main() {
let passphrase = create_passphrase(" ", 8);
println!("{passphrase}");
let pin = create_pin(8);
println!("{pin}");
println!("{}", create_passphrase(" ", 8));
println!("{}", create_pin(8));
println!("{}", create_base32(8));
println!("Press Enter");
let mut input = String::new();
std::io::stdin().read_line(&mut input).ok();
}
/// Generates a Base32 string per RFC 4648, Section 6.
///
/// Wikipedia says this is the most popular encoding, and it's what
/// `base32` in GNU coreutils uses.
/// <https://en.wikipedia.org/wiki/Base32#Base_32_Encoding_per_%C2%A76>
///
/// This is useful for short IDs, e.g. bugs that aren't living in an issue
/// tracker. 8 characters of Base32 is 40 bits of entropy, which is
/// not enough for a password
pub fn create_base32(len: usize) -> String {
let encoding: Vec<_> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".chars().collect();
assert_eq!(encoding.len(), 32);
let chars = (0..len).map(|_| *encoding.choose(&mut rand::rng()).unwrap());
String::from_iter(chars)
}
/// Generates a diceware passphrase
pub fn create_passphrase(separator: &str, len: usize) -> String {
let dice = Dice::default();
let words: Vec<_> = (0..len).map(|_| dice.pick_word()).collect();