The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/internal/utils/passphrase.go

80 lines
2.0 KiB

package utils
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
// AskForPassphrase return passphrase using password input
func AskForPassphrase(prompt string) string {
fmt.Printf(prompt)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
panic("read password error")
}
password := string(bytePassword)
fmt.Println()
return password
}
// readAllAsString reads the entire file contents as a string.
func readAllAsString(r io.Reader) (data string, err error) {
bytes, err := ioutil.ReadAll(r)
return string(bytes), err
}
// GetPassphraseFromSource reads a passphrase such as a key-encrypting one
// non-interactively from the given source.
//
// The source can be "pass:password", "env:var", "file:pathname", "fd:number",
// or "stdin". See “PASS PHRASE ARGUMENTS” section of openssl(1) for details.
func GetPassphraseFromSource(src string) (pass string, err error) {
switch src {
case "stdin":
return readAllAsString(os.Stdin)
}
methodArg := strings.SplitN(src, ":", 2)
if len(methodArg) < 2 {
return "", errors.Errorf("invalid passphrase reading method %#v", src)
}
method := methodArg[0]
arg := methodArg[1]
switch method {
case "pass":
return arg, nil
case "env":
pass, ok := os.LookupEnv(arg)
if !ok {
return "", errors.Errorf("environment variable %#v undefined", arg)
}
return pass, nil
case "file":
f, err := os.Open(arg)
if err != nil {
return "", errors.Wrapf(err, "cannot open file %#v", arg)
}
defer func() { _ = f.Close() }()
return readAllAsString(f)
case "fd":
fd, err := strconv.ParseUint(arg, 10, 0)
if err != nil {
return "", errors.Wrapf(err, "invalid fd literal %#v", arg)
}
f := os.NewFile(uintptr(fd), "(passphrase-source)")
if f == nil {
return "", errors.Errorf("cannot open fd %#v", fd)
}
defer func() { _ = f.Close() }()
return readAllAsString(f)
}
return "", errors.Errorf("invalid passphrase reading method %#v", method)
}