From 44c5d6ad7d5804d43efc80c5aae01990aed8f3d9 Mon Sep 17 00:00:00 2001 From: David Hill Date: Fri, 31 Mar 2017 14:00:18 -0400 Subject: [PATCH] Add new tool, promptsecret (#649) promptsecret is used to read a secret from stdin without echoing and write it to stdout. --- .gitignore | 5 +++-- cmd/promptsecret/promptsecret.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 cmd/promptsecret/promptsecret.go diff --git a/.gitignore b/.gitignore index d13c42d20c..0aa250bbfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -cmd/dcrd/dcrd -cmd/dcrd/dcrctl +dcrd +cmd/dcrctl/dcrctl +cmd/promptsecret/promptsecret *~ *.pyc vendor/ diff --git a/cmd/promptsecret/promptsecret.go b/cmd/promptsecret/promptsecret.go new file mode 100644 index 0000000000..4bf91de75e --- /dev/null +++ b/cmd/promptsecret/promptsecret.go @@ -0,0 +1,36 @@ +// Copyright (c) 2017 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package main + +import ( + "fmt" + "os" + + "golang.org/x/crypto/ssh/terminal" +) + +func zero(b []byte) { + for i := 0; i < len(b); i++ { + b[i] = 0x00 + } +} + +func main() { + fmt.Fprint(os.Stderr, "Secret: ") + + secret, err := terminal.ReadPassword(int(os.Stdin.Fd())) + fmt.Fprint(os.Stderr, "\n") + if err != nil { + fmt.Fprintf(os.Stderr, "unable to read secret: %v\n", err) + os.Exit(1) + } + + _, err = os.Stdout.Write(secret) + zero(secret) + if err != nil { + fmt.Fprintf(os.Stderr, "unable to write to stdout: %v\n", err) + os.Exit(1) + } +}