Situation:
I want to get a password entry from the stdin
console - without echoing what the user types. Is there something comparable to getpasswd
functionality in Go?
What I tried:
I tried using syscall.Read
, but it echoes what is typed.
Situation:
I want to get a password entry from the stdin
console - without echoing what the user types. Is there something comparable to getpasswd
functionality in Go?
What I tried:
I tried using syscall.Read
, but it echoes what is typed.
Here is a solution that I developed using Go1.6.2 that you might find useful.
It only uses the following standard packages:
bufio
,fmt
,os
,strings
andsyscall
. More specifically, it usessyscall.ForkExec()
andsyscall.Wait4()
to invoke stty to disable/enable terminal echo.I have tested it on Linux and BSD (Mac). It will not work on windows.
You could also use
PasswordPrompt
function of https://github.com/peterh/liner package.Just saw a mail in #go-nuts maillist. There is someone who wrote quite a simple go package to be used. You can find it here: https://github.com/howeyc/gopass
It something like that:
you can do this by execing
stty -echo
to turn off echo and thenstty echo
after reading in the password to turn it back onHere is a version specific to Linux:
So to use it:
It's the TCGETS syscall that is linux specific. There are different syscall values for OSX and Windows.
I had a similar usecase and the following code snippet works well for me. Feel free to try this if you are still stuck here.
Of course, you need to install terminal package using
go get
beforehand.