From 3663f604b66782825d07ba8eae0773b5865bb017 Mon Sep 17 00:00:00 2001 From: Matthieu Moquet Date: Fri, 2 Feb 2024 17:07:32 +0100 Subject: [PATCH] feat: add option to change password for generated keystore --- main.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index d7f5864..17835de 100644 --- a/main.go +++ b/main.go @@ -23,13 +23,6 @@ type Keystore struct { Version uint `json:"version"` } -type Options struct { - Cipher string - InputFile string - OutputFile string - Password string -} - var Flags = []cli.Flag{ &cli.StringFlag{ Name: "cipher", @@ -51,7 +44,12 @@ var Flags = []cli.Flag{ Name: "password", Value: "", Aliases: []string{"p"}, - Usage: "Keystore password", + Usage: "Keystore decrypting password", + }, + &cli.StringFlag{ + Name: "new-password", + Value: "", + Usage: "Set another password for generated keystore", }, &cli.BoolFlag{ Name: "raw", @@ -78,11 +76,12 @@ func Run(cCtx *cli.Context) error { var ( err error - inputFile = cCtx.String("input") - outputFile = cCtx.String("output") - password = cCtx.String("password") - cipher = cCtx.String("cipher") - raw = cCtx.Bool("raw") + inputFile = cCtx.String("input") + outputFile = cCtx.String("output") + password = cCtx.String("password") + newPassword = cCtx.String("new-password") + cipher = cCtx.String("cipher") + raw = cCtx.Bool("raw") ) encryptor := keystorev4.New(keystorev4.WithCipher(cipher)) @@ -110,7 +109,7 @@ func Run(cCtx *cli.Context) error { err = json.NewDecoder(input).Decode(&keystore) if err != nil { - return err + return errors.New("could not decode keystore json") } if keystore.Pubkey == "" { @@ -119,7 +118,7 @@ func Run(cCtx *cli.Context) error { secret, err := encryptor.Decrypt(keystore.Crypto, password) if err != nil { - return err + return fmt.Errorf("could not decrypt keystore: %w", err) } if raw { @@ -127,16 +126,20 @@ func Run(cCtx *cli.Context) error { return nil } + if newPassword != "" { + password = newPassword + } + crypto, err := encryptor.Encrypt(secret, password) if err != nil { - return err + return fmt.Errorf("could not encrypt keystore: %w", err) } keystore.Crypto = crypto keystore2, err := json.Marshal(keystore) if err != nil { - return err + return fmt.Errorf("could not encode keystore to json: %w", err) } fmt.Fprint(output, string(keystore2))