From 09f5b7f0eb6117a6cfc915a359042faff01f2dee Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 22 Mar 2018 20:18:14 +0100 Subject: Move reporting of pwned or not into main. --- src/main.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 3f82135..4e7cbb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ extern crate reqwest; extern crate ring; use std::env; +use std::error::Error; use ring::digest; // @@ -75,17 +76,20 @@ fn test_creating_new_password() { assert_eq!(&pw.rest, "910077770C8340F63CD2DCA2AC1F120444F"); } -fn check(pw: Password) { +fn check(pw: Password) -> Result{ let uri = &format!("https://api.pwnedpasswords.com/range/{}", pw.range); - let hashes = reqwest::get(uri).unwrap().text().unwrap(); + let hashes = reqwest::get(uri)?.text()?; if let Some(pos) = hashes.find(&pw.rest) { - println!("Password is PWNED!"); if let Some(res) = hashes[pos..].lines().take(1).collect::>().pop() { let count = res.split(':').skip(1).collect::>().pop().unwrap(); println!("The password {} was found in {} breaches", pw.pw, count); } + + return Ok(true); } + + Ok(false) } fn print_usage() { @@ -103,6 +107,10 @@ fn main() { } for pw in passwords { - check(Password::new(&pw)); + match check(Password::new(&pw)) { + Err(e) => println!("{}", e.description()), + Ok(true) => println!("Password is PWNED!"), + Ok(false) => println!("Password was not found in any breaches.") + } } } -- cgit v1.2.3