From 99fa6cb2210328c8a7ba96e56971788aac5bce27 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Mar 2018 17:33:23 +0100 Subject: When `-i`, replace password with '*'s Previously, when the "invisible" flag was activated, we wouldn't print anything except the "p: " prefix on a password line. This made it difficult to verify the password, and if you for some reason had multiple passwords, you wouldn't know which one to pick unless you knew the order. This at least gives you a visual cue about your password. --- src/main.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 7411c1a..28b0259 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,6 +89,20 @@ fn is_password_line(line: &str) -> bool { line.starts_with("p: ") } +/// Replaces the password on a password line with "*"s. +/// +/// # Examples +/// +/// ``` +/// assert_eq!(hide_password_line("p: secret"), "p: ******); +/// ``` +fn hide_password_line(line: &str) -> String { + const KEY_LENGTH: usize = 3; + let password_length = line.len() - KEY_LENGTH; + + format!("p: {}", "*".repeat(password_length)) +} + fn main() { let args: Vec = env::args().collect(); @@ -132,7 +146,7 @@ fn main() { for (i, s) in options.iter().enumerate() { if hide_password && is_password_line(s) { - term.printline(5, i + 2, "p: ") + term.printline(5, i + 2, &hide_password_line(s)) } else { term.printline(5, i + 2, s) } -- cgit v1.2.3