How do I hide user input (password field) in terminal similar to the -s
command in read -s p "password " password
in bash. ?
var password string
fmt.Println("password: ")
fmt.Scan(&password)
http://play.golang.org/p/xAPDDPjKb4
The best way would be to use ReadPassword()
from the terminal package. Else you can also look in this question for more ways to do so.
Code example:
package main
import "golang.org/x/crypto/ssh/terminal"
import "fmt"
func main() {
fmt.Println("Enter password: ")
password, err := terminal.ReadPassword(0)
if err == nil {
fmt.Println("Password typed: " + string(password))
}
}
I have no idea about go, so I'm not sure if you can call other programs. If so, just call stty -echo to hide input and stty echo to show it again.
Otherwise you could try the following:
fmt.Println("password: ")
fmt.Println("\033[8m") // Hide input
fmt.Scan(&password)
fmt.Println("\033[28m") // Show input
To support windows, as mentioned here, please use:
import "syscall"
import "golang.org/x/crypto/ssh/terminal"
passwd, err := terminal.ReadPassword(int(syscall.Stdin))
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