Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get password input without showing user input?

Tags:

rust

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);
}
like image 752
fengsp Avatar asked Mar 08 '15 07:03

fengsp


People also ask

How do I make my HTML password not visible?

<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 (.)

How do you hide user input in python?

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.

How do you input a password in HTML?

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.


1 Answers

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);
}


Old answer

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.

like image 157
Caspar Avatar answered Oct 30 '22 09:10

Caspar