]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/verifier.go
Use ssh/terminal package for reading passwords directly from terminal
[govpn.git] / src / govpn / verifier.go
index ba51846ce23eb17c42b4a9b9fcc5d0333f848bbc..0dc9388188ad0e1f0c29eec611e7218cae32b687 100644 (file)
@@ -29,6 +29,7 @@ import (
 
        "github.com/agl/ed25519"
        "github.com/magical/argon2"
+       "golang.org/x/crypto/ssh/terminal"
 )
 
 const (
@@ -117,11 +118,26 @@ func (v *Verifier) LongForm() string {
        )
 }
 
-// Read string from the file, trimming newline.
-func StringFromFile(path string) string {
-       s, err := ioutil.ReadFile(path)
+// Read the key either from text file (if path is specified), or
+// from the terminal.
+func KeyRead(path string) (string, error) {
+       var p []byte
+       var err error
+       var pass string
+       if path == "" {
+               fmt.Print("Passphrase:")
+               p, err = terminal.ReadPassword(0)
+               fmt.Print("\n")
+               pass = string(p)
+       } else {
+               p, err = ioutil.ReadFile(path)
+               pass = strings.TrimRight(string(p), "\n")
+       }
        if err != nil {
-               log.Fatalln("Can not read string from", path, err)
+               return "", err
+       }
+       if len(pass) == 0 {
+               return "", errors.New("Empty passphrase submitted")
        }
-       return strings.TrimRight(string(s), "\n")
+       return pass, err
 }