How can I get password input without showing user input?
fn main() {
println!("Type Your Password");
// I want to hide input here, and do not know how
let input = std::old_io::stdin().read_line().ok().expect("Failed to read line");
println!("{}", input);
}
<input type = "password" > Password can also be the part of the data entered in HTML forms. Instead of displaying original characters, either asterisk (*) or dots (.)
There are various Python modules that are used to hide the user's inputted password, among them one is maskpass() module. In Python with the help of maskpass() module and base64() module we can hide the password of users with asterisk(*) during input time and then with the help of base64() module it can be encrypted.
To take password input in HTML form, use the <input> tag with type attribute as a password. This is also a single-line text input but it masks the character as soon as a user enters it.
Update: you can use the rpassword crate. Quoting from the README:
Add the rpassword
crate to your Cargo.toml
:
[dependencies]
rpassword = "0.0.4"
Then use the read_password()
function:
extern crate rpassword;
use rpassword::read_password;
use std::io::Write;
fn main() {
print!("Type a password: ");
std::io::stdout().flush().unwrap();
let password = read_password().unwrap();
println!("The password is: '{}'", password);
}
I suspect your best bet is calling some C functions from rust: either getpass (3) or its recommended alternatives (see Getting a password in C without using getpass). The tricky thing is that it differs by platform, of course (if you get it working, it'd be handy as a crate).
Depending on your requirements, you could also try using ncurses-rs (crate "ncurses"); I haven't tested it but it looks like example 2 might demo turning off echoing input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With