motus 0.2.0

Access the most common information about your system using a single command
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#![feature(iter_intersperse)]

use std::collections::HashMap;
use std::fmt::{Display, Formatter};

use arboard::Clipboard;
use clap::{Parser, Subcommand, ValueEnum};
use colored::{ColoredString, Colorize};
use human_panic::setup_panic;
use rand::prelude::*;
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use term_table::row::Row;
use term_table::table_cell::{Alignment, TableCell};
use term_table::{Table, TableStyle};
use zxcvbn::zxcvbn;

/// Args is a struct representing the command line arguments
#[derive(Parser, Debug)]
#[command(name = "motus")]
#[command(version = "0.2.0")]
#[command(about = "A command-line tool to generate secure passwords")]
#[command(
    long_about = "Motus is a command-line tool for generating secure, random, and memorable passwords as well as PIN codes."
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Disable automatic copying of generated password to clipboard
    #[arg(long)]
    no_clipboard: bool,

    /// Output the generated password in a specified format
    #[arg(short, long, default_value = "text", value_enum)]
    output: OutputFormat,

    /// Display a safety analysis along the generated password
    #[arg(long)]
    analyze: bool,

    /// Seed value for deterministic password generation (for testing purposes)
    #[arg(long)]
    seed: Option<u64>, // Set the randomness source with an unsigned 64-bit integer for reproducible passwords
}

#[derive(Debug, Subcommand)]
enum Commands {
    #[command(name = "memorable")]
    #[command(about = "Generate a human-friendly memorable password")]
    #[command(
        long_about = "Generate a memorable password using a combination of words and configurable separators, with optional capitalization and the choice to use unrecognizable words."
    )]
    Memorable {
        /// Specify the number of words in the generated password
        #[arg(short, long, default_value = "5", value_parser = validate_word_count)]
        words: u32,

        /// Choose the separator for words in the generated password
        #[arg(short, long, default_value = "space", value_enum)]
        separator: motus::Separator,

        /// Enable capitalization of each word in the generated password
        #[arg(short, long)]
        capitalize: bool,

        /// Enable the use of unrecognizable words in the generated password
        #[arg(long)]
        no_full_words: bool,
    },

    #[command(name = "random")]
    #[command(about = "Generate a random password with specified complexity")]
    #[command(
        long_about = "Generate a random password with a configurable number of characters, including optional numbers and symbols for increased complexity."
    )]
    Random {
        /// Specify the number of characters in the generated password
        #[arg(short, long, default_value = "20", value_parser = validate_character_count)]
        characters: u32,

        /// Enable the inclusion of numbers in the generated password
        #[arg(short, long)]
        numbers: bool,

        /// Enable the inclusion of symbols in the generated password
        #[arg(short, long)]
        symbols: bool,
    },

    #[command(name = "pin")]
    #[command(about = "Generate a random numeric PIN code")]
    #[command(
        long_about = "Generate a random numeric Personal Identification Number (PIN) code with a configurable length."
    )]
    Pin {
        /// Specify the number of digits in the generated PIN code
        #[arg(short, long, default_value = "7", value_parser = validate_pin_length)]
        numbers: u32,
    },
}

fn main() {
    // Enable human-readable panic messages
    setup_panic!();

    // Parse command line arguments
    let opts: Cli = Cli::parse();

    // Initialize the randomness source
    // If a seed is provided, use it to seed the randomness source
    // Otherwise, use the main thread's randomness source
    let mut rng: Box<dyn RngCore> = match opts.seed {
        Some(seed) => Box::new(StdRng::seed_from_u64(seed)),
        None => Box::new(rand::thread_rng()),
    };

    let password = match opts.command {
        Commands::Memorable {
            words,
            separator,
            capitalize,
            no_full_words,
        } => motus::memorable_password(
            &mut rng,
            words as usize,
            separator,
            capitalize,
            no_full_words,
        ),
        Commands::Random {
            characters,
            numbers,
            symbols,
        } => motus::random_password(&mut rng, characters, numbers, symbols),
        Commands::Pin { numbers } => motus::pin_password(&mut rng, numbers),
    };

    // Copy the password to the clipboard
    if !opts.no_clipboard {
        let mut clipboard =
            Clipboard::new().expect("unable to interact with your system's clipboard");
        clipboard
            .set_text(&password)
            .expect("unable to set clipboard contents");
    }

    match opts.output {
        OutputFormat::Text => {
            if opts.analyze {
                let analysis = SecurityAnalysis::new(&password);
                analysis.display_report(TableStyle::extended(), 80)
            } else {
                println!("{}", password);
            }
        }
        OutputFormat::Json => {
            let output = PasswordOutput {
                kind: match opts.command {
                    Commands::Memorable { .. } => PasswordKind::Memorable,
                    Commands::Random { .. } => PasswordKind::Random,
                    Commands::Pin { .. } => PasswordKind::Pin,
                },
                password: &password,
                analysis: if opts.analyze {
                    Some(SecurityAnalysis::new(&password))
                } else {
                    None
                },
            };
            println!("{}", serde_json::to_string(&output).unwrap());
        }
    }
}

#[derive(ValueEnum, Clone, Debug)]
enum OutputFormat {
    Text,
    Json,
}

#[derive(Serialize)]
struct PasswordOutput<'a> {
    kind: PasswordKind,
    password: &'a str,

    #[serde(skip_serializing_if = "Option::is_none")]
    analysis: Option<SecurityAnalysis<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
enum PasswordKind {
    Memorable,
    Random,
    Pin,
}

impl Display for PasswordKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            PasswordKind::Memorable => write!(f, "memorable"),
            PasswordKind::Random => write!(f, "random"),
            PasswordKind::Pin => write!(f, "pin"),
        }
    }
}

struct SecurityAnalysis<'a> {
    password: &'a str,
    entropy: zxcvbn::Entropy,
}

impl Serialize for SecurityAnalysis<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut crack_times = HashMap::new();
        crack_times.insert(
            "100/h",
            self.entropy
                .crack_times()
                .online_throttling_100_per_hour()
                .to_string(),
        );

        crack_times.insert(
            "10/s",
            self.entropy
                .crack_times()
                .online_no_throttling_10_per_second()
                .to_string(),
        );

        crack_times.insert(
            "10^4/s",
            self.entropy
                .crack_times()
                .offline_slow_hashing_1e4_per_second()
                .to_string(),
        );

        crack_times.insert(
            "10^10/s",
            self.entropy
                .crack_times()
                .offline_fast_hashing_1e10_per_second()
                .to_string(),
        );

        let mut struct_serializer = serializer.serialize_struct("SecurityAnalysis", 3)?;
        struct_serializer.serialize_field(
            "strength",
            &PasswordStrength::from(self.entropy.score()).to_string(),
        )?;
        struct_serializer.serialize_field(
            "guesses",
            format!("10^{:.0}", &self.entropy.guesses_log10()).as_str(),
        )?;
        struct_serializer.serialize_field("crack_times", &crack_times)?;
        struct_serializer.end()
    }
}

impl<'a> SecurityAnalysis<'a> {
    fn new(password: &'a str) -> Self {
        let entropy = zxcvbn(password, &[]).expect("unable to analyze password's safety");
        Self { password, entropy }
    }

    fn display_report(&self, table_style: TableStyle, max_width: usize) {
        self.display_password_table(table_style, max_width);
        self.display_analysis_table(table_style, max_width);
        self.display_crack_times_table(table_style, max_width);
    }

    fn display_password_table(&self, table_style: TableStyle, max_width: usize) {
        let mut table = Table::new();
        table.max_column_width = max_width;
        table.style = table_style;

        table.add_row(Row::new(vec![TableCell::new_with_alignment(
            "Generated Password".bold(),
            1,
            Alignment::Left,
        )]));

        table.add_row(Row::new(vec![TableCell::new(self.password)]));

        println!("{}", table.render());
    }

    fn display_analysis_table(&self, table_style: TableStyle, max_width: usize) {
        let mut table = Table::new();
        table.max_column_width = max_width;
        table.style = table_style;

        table.add_row(Row::new(vec![TableCell::new_with_alignment(
            "Security Analysis",
            2,
            Alignment::Left,
        )]));

        table.add_row(Row::new(vec![
            TableCell::new("Strength".bold()),
            TableCell::new_with_alignment(
                PasswordStrength::from(self.entropy.score()).to_colored_string(),
                1,
                Alignment::Left,
            ),
        ]));

        table.add_row(Row::new(vec![
            TableCell::new("Guesses".bold()),
            TableCell::new_with_alignment(
                format!("10^{:.0}", self.entropy.guesses_log10()),
                1,
                Alignment::Left,
            ),
        ]));

        println!("{}", table.render());
    }

    fn display_crack_times_table(&self, table_style: TableStyle, max_width: usize) {
        let mut table = Table::new();
        table.max_column_width = max_width;
        table.style = table_style;

        table.add_row(Row::new(vec![TableCell::new_with_alignment(
            "Crack time estimations",
            2,
            Alignment::Left,
        )]));

        table.add_row(Row::new(vec![
            TableCell::new("100 attempts/hour".bold()),
            TableCell::new_with_alignment(
                format!(
                    "{}",
                    self.entropy.crack_times().online_throttling_100_per_hour()
                ),
                1,
                Alignment::Left,
            ),
        ]));

        table.add_row(Row::new(vec![
            TableCell::new("10 attempts/second".bold()),
            TableCell::new_with_alignment(
                format!(
                    "{}",
                    self.entropy
                        .crack_times()
                        .online_no_throttling_10_per_second()
                ),
                1,
                Alignment::Left,
            ),
        ]));

        table.add_row(Row::new(vec![
            TableCell::new("10^4 attempts/second".bold()),
            TableCell::new_with_alignment(
                format!(
                    "{}",
                    self.entropy
                        .crack_times()
                        .offline_slow_hashing_1e4_per_second()
                ),
                1,
                Alignment::Left,
            ),
        ]));

        table.add_row(Row::new(vec![
            TableCell::new("10^10 attempts/second".bold()),
            TableCell::new_with_alignment(
                format!(
                    "{}",
                    self.entropy
                        .crack_times()
                        .offline_fast_hashing_1e10_per_second()
                ),
                1,
                Alignment::Left,
            ),
        ]));

        println!("{}", table.render());
    }
}

enum PasswordStrength {
    VeryWeak,
    Weak,
    Reasonable,
    Strong,
    VeryStrong,
}

impl From<u8> for PasswordStrength {
    fn from(score: u8) -> Self {
        match score {
            0 => PasswordStrength::VeryWeak,
            1 => PasswordStrength::Weak,
            2 => PasswordStrength::Reasonable,
            3 => PasswordStrength::Strong,
            4 => PasswordStrength::VeryStrong,
            _ => panic!("invalid score"),
        }
    }
}

impl PasswordStrength {
    fn to_colored_string(&self) -> ColoredString {
        match self {
            PasswordStrength::VeryWeak => self.to_string().red(),
            PasswordStrength::Weak => self.to_string().bright_red(),
            PasswordStrength::Reasonable => self.to_string().yellow(),
            PasswordStrength::Strong => self.to_string().bright_green(),
            PasswordStrength::VeryStrong => self.to_string().green(),
        }
    }
}

impl Display for PasswordStrength {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let strength = match self {
            PasswordStrength::VeryWeak => "very weak",
            PasswordStrength::Weak => "weak",
            PasswordStrength::Reasonable => "reasonable",
            PasswordStrength::Strong => "strong",
            PasswordStrength::VeryStrong => "very strong",
        };

        write!(f, "{}", strength)
    }
}

/// validate_word_count parses the given string as a u32 and returns an error if it is not between
/// 3 and 15.
fn validate_word_count(s: &str) -> Result<u32, String> {
    match s.parse::<u32>() {
        Ok(n) if (3..16).contains(&n) => Ok(n),
        Ok(_) => Err("The number of words must be between 4 and 15".to_string()),
        Err(_) => Err("The number of words must be an integer".to_string()),
    }
}

/// validate_character_count parses the given string as a u32 and returns an error if it is not between
/// 8 and 100.
fn validate_character_count(s: &str) -> Result<u32, String> {
    match s.parse::<u32>() {
        Ok(n) if (8..101).contains(&n) => Ok(n),
        Ok(_) => Err("The number of words must be between 8 and 100".to_string()),
        Err(_) => Err("The number of words must be an integer".to_string()),
    }
}

/// validate_ping_length parses the given string as a u32 and returns an error if it is not between
/// 3 and 12.
fn validate_pin_length(s: &str) -> Result<u32, String> {
    match s.parse::<u32>() {
        Ok(n) if (3..13).contains(&n) => Ok(n),
        Ok(_) => Err("The number of words must be between 3 and 12".to_string()),
        Err(_) => Err("The number of words must be an integer".to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_word_count() {
        assert!(validate_word_count("2").is_err());
        assert!(validate_word_count("3").is_ok());
        assert!(validate_word_count("15").is_ok());
        assert!(validate_word_count("16").is_err());
    }

    #[test]
    fn test_validate_character_count() {
        assert!(validate_character_count("7").is_err());
        assert!(validate_character_count("8").is_ok());
        assert!(validate_character_count("100").is_ok());
        assert!(validate_character_count("101").is_err());
    }

    #[test]
    fn test_validate_pin_length() {
        assert!(validate_pin_length("2").is_err());
        assert!(validate_pin_length("3").is_ok());
        assert!(validate_pin_length("12").is_ok());
        assert!(validate_pin_length("13").is_err());
    }
}