codephrases/
lib.rs

1//! `codephrases`
2//!
3//! Generate easy to memorize codephrases for sharing authentication. The word lists are based off
4//! the room name generator for [Jitsi
5//! Meet](https://github.com/jitsi/js-utils/blob/2639462719185599bc54e15233f9154e2d016a82/random/roomNameGenerator.js).
6//!
7//! Note that this library has insufficient entropy for generating passphrases. You probably want
8//! diceware for that.
9//!
10//! ## Example
11//!
12//! ```
13//! use codephrases::random_codephrase;
14//!
15//! let phrase = random_codephrase();
16//! // "PinkContradictionsYellFast"
17//! ```
18use rand::seq::SliceRandom;
19use rand::Rng;
20
21mod adjectives;
22mod adverbs;
23mod nouns;
24mod verbs;
25
26/// Generate a random codephrase.
27pub fn random_codephrase() -> String {
28    let mut rng = rand::thread_rng();
29    random_codephrase_from_rng(&mut rng)
30}
31
32/// Generate a random codephrase using the specified RNG.
33pub fn random_codephrase_from_rng<R: ?Sized>(rng: &mut R) -> String
34where
35    R: Rng,
36{
37    let mut out = String::new();
38    out.push_str(adjectives::ADJECTIVES.choose(rng).unwrap());
39    out.push_str(nouns::NOUNS.choose(rng).unwrap());
40    out.push_str(verbs::VERBS.choose(rng).unwrap());
41    out.push_str(adverbs::ADVERBS.choose(rng).unwrap());
42    out
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn check_random_codephrase() {
51        let out = random_codephrase();
52        assert!(out.len() > 10);
53    }
54
55    #[test]
56    fn check_random_codephrase_from_rng() {
57        let mut rng = rand::thread_rng();
58        for _ in 0..10000 {
59            let out = random_codephrase_from_rng(&mut rng);
60            assert!(out.len() > 10);
61        }
62    }
63}