aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: b89a38111fabb1721a5bbca0d1bdcc943ccf09ae (plain) (blame)
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
// checkpw - Check passwords against pwnedpasswords.com
// Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use checkpw::{check, Password};
use std::{env, error::Error, thread, time, process};

fn print_usage() {
    println!("Usage: checkpw <pw>...");
}

fn main() {
    let (args, passwords) : (Vec<String>, Vec<String>) = env::args()
        .skip(1)
        .partition(|arg| arg.starts_with('-'));

    if args.is_empty() && passwords.is_empty() {
        print_usage();
        return;
    }

    let mut first_round = true;
    let mut pwn_count = 0;

    for pw in passwords {
        if first_round {
            first_round = false;
        }
        else {
            thread::sleep(time::Duration::new(1, 0));
        }

        match check(Password::new(&pw)) {
            Err(e) => println!("{}", e.description()),
            Ok(num) => {
                if num > 0 {
                    println!("Password is PWNED! It was found in {} breaches.", num);
                    pwn_count += 1;
                }
                else {
                    println!("Password was not found in any breaches");
                }
            }
        }
    }

    process::exit(pwn_count);
}