From 13af27641829f61d1e6b383e37aab6caae22f2c1 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Fri, 8 Jun 2018 15:07:07 +0200 Subject: cmd/ethkey: add command to change key passphrase (#16516) This change introduces ethkey changepassphrase to change the passphrase of a key file. --- cmd/ethkey/utils.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) (limited to 'cmd/ethkey/utils.go') diff --git a/cmd/ethkey/utils.go b/cmd/ethkey/utils.go index 0e563bf92..6f60ebaf1 100644 --- a/cmd/ethkey/utils.go +++ b/cmd/ethkey/utils.go @@ -28,26 +28,14 @@ import ( "gopkg.in/urfave/cli.v1" ) -// getPassPhrase obtains a passphrase given by the user. It first checks the -// --passphrase command line flag and ultimately prompts the user for a -// passphrase. -func getPassPhrase(ctx *cli.Context, confirmation bool) string { - // Look for the --passphrase flag. - passphraseFile := ctx.String(passphraseFlag.Name) - if passphraseFile != "" { - content, err := ioutil.ReadFile(passphraseFile) - if err != nil { - utils.Fatalf("Failed to read passphrase file '%s': %v", - passphraseFile, err) - } - return strings.TrimRight(string(content), "\r\n") - } - - // Otherwise prompt the user for the passphrase. +// promptPassphrase prompts the user for a passphrase. Set confirmation to true +// to require the user to confirm the passphrase. +func promptPassphrase(confirmation bool) string { passphrase, err := console.Stdin.PromptPassword("Passphrase: ") if err != nil { utils.Fatalf("Failed to read passphrase: %v", err) } + if confirmation { confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ") if err != nil { @@ -57,9 +45,29 @@ func getPassPhrase(ctx *cli.Context, confirmation bool) string { utils.Fatalf("Passphrases do not match") } } + return passphrase } +// getPassphrase obtains a passphrase given by the user. It first checks the +// --passfile command line flag and ultimately prompts the user for a +// passphrase. +func getPassphrase(ctx *cli.Context) string { + // Look for the --passwordfile flag. + passphraseFile := ctx.String(passphraseFlag.Name) + if passphraseFile != "" { + content, err := ioutil.ReadFile(passphraseFile) + if err != nil { + utils.Fatalf("Failed to read passphrase file '%s': %v", + passphraseFile, err) + } + return strings.TrimRight(string(content), "\r\n") + } + + // Otherwise prompt the user for the passphrase. + return promptPassphrase(false) +} + // signHash is a helper function that calculates a hash for the given message // that can be safely used to calculate a signature from. // -- cgit v1.2.3