github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/cli/input/readpass_unix.go (about)

     1  //go:build !windows
     2  
     3  package input
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  
     9  	"golang.org/x/term"
    10  )
    11  
    12  // readSecurePassword reads the user's password with prompt directly from /dev/tty.
    13  func readSecurePassword(prompt string) (string, error) {
    14  	f, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
    15  	if err != nil {
    16  		return "", err
    17  	}
    18  	defer f.Close()
    19  	_, err = f.WriteString(prompt)
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  	pass, err := term.ReadPassword(int(f.Fd()))
    24  	if err != nil {
    25  		return "", fmt.Errorf("failed to read password: %w", err)
    26  	}
    27  	_, err = f.WriteString("\n")
    28  	return string(pass), err
    29  }