mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
31 lines
598 B
Go
31 lines
598 B
Go
|
//go:build !windows
|
||
|
// +build !windows
|
||
|
|
||
|
package input
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
|
||
|
"golang.org/x/term"
|
||
|
)
|
||
|
|
||
|
// readSecurePassword reads the user's password with prompt directly from /dev/tty.
|
||
|
func readSecurePassword(prompt string) (string, error) {
|
||
|
f, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
_, err = f.WriteString(prompt)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
pass, err := term.ReadPassword(int(f.Fd()))
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("failed to read password: %w", err)
|
||
|
}
|
||
|
_, err = f.WriteString("\n")
|
||
|
return string(pass), err
|
||
|
}
|