Skip to content

Commit 2da40a3

Browse files
burdgesvks
authored andcommitted
Remarks on passwrod generation
I suppose the code should go elsewhere.
1 parent 321ae39 commit 2da40a3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/distributions/other.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,37 @@ use serde::{Serialize, Deserialize};
3636
/// .collect();
3737
/// println!("Random chars: {}", chars);
3838
/// ```
39+
///
40+
/// # Passwords
41+
///
42+
/// We caution that strings produced by sampling `Alphanumeric` tend not
43+
/// to be particularly memorable when used as passwords by humans.
44+
/// Instead, we suggest that human memorable passwords be created by
45+
/// drawing words independently and uniformly at random from a large wordlist.
46+
///
47+
/// Each random word contributes `log2(wordlist_length)` bits of entropy.
48+
///
49+
/// Among the widely reviewed wordlists, there are [Diceware](https://en.wikipedia.org/wiki/Diceware)
50+
/// wordlists for many major langauges, including some from security
51+
/// organizations like the E.F.F., and many of which further facilitate
52+
/// memorability by avoiding homophones and words with tricky spelling.
53+
///
54+
/// There exists [several crates](https://crates.io/search?q=diceware) for
55+
/// this but `rand::seq::SliceRandom::choose` works too:
56+
/// ```
57+
/// # use rand::Rng;
58+
/// #[allow(dead_code)]
59+
/// pub fn make_password<R: Rng>(wordlist: &[impl ::core::borrow::Borrow<str>], entropy: u32, rng: &mut R) -> String {
60+
/// use rand::seq::SliceRandom;
61+
/// use core::convert::TryInto;
62+
/// let entropy: f64 = entropy.into();
63+
/// let l: u32 = wordlist.len().try_into().unwrap();
64+
/// assert!( l > 0 );
65+
/// let l: f64 = l.into();
66+
/// let l = (entropy / l.log2()).ceil() as usize;
67+
/// (0..l).map(|_| wordlist.choose(rng).unwrap().borrow() ).collect::<String>()
68+
/// }
69+
/// ```
3970
#[derive(Debug)]
4071
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
4172
pub struct Alphanumeric;

0 commit comments

Comments
 (0)