Skip to content

Commit

Permalink
Finally found a way to not echo stdin when inputing password
Browse files Browse the repository at this point in the history
  • Loading branch information
antonin-lebrard authored Aug 19, 2021
1 parent 2c1d3b0 commit 8ac520b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/term"
"io/ioutil"
"log"
"math/big"
Expand Down Expand Up @@ -90,14 +91,20 @@ func getFiles() []KeyFileContent {
}

func getPassword() []byte {
reader := bufio.NewReader(os.Stdin)
fmt.Print("password: ")
password, err := reader.ReadBytes(byte('\n'))
PanicIfErrorMsg(err, "cannot read password")
if bytes.HasSuffix(password, []byte("\n")) {
previousState, err := term.MakeRaw(int(os.Stdin.Fd()))
PanicIfErrorMsg(err, "cannot configure terminal to not echo characters")
t := term.NewTerminal(os.Stdin, "")
password, err := t.ReadPassword("password: ")
if err != nil {
log.Println("cannot read password")
PanicIfErrorMsg(term.Restore(int(os.Stdin.Fd()), previousState), "cannot restore terminal to non-raw mode")
panic(err)
}
PanicIfErrorMsg(term.Restore(int(os.Stdin.Fd()), previousState), "cannot restore terminal to non-raw mode")
if strings.HasSuffix(password, "\n") {
password = password[:len(password)-1]
}
return password
return []byte(password)
}

func decipherKeys(pass []byte, keyFiles []KeyFileContent, onDecipheredKey func(string, string)) {
Expand Down

0 comments on commit 8ac520b

Please sign in to comment.