From f72b88c632c4dc407097b59a72e1ed872bac4882 Mon Sep 17 00:00:00 2001 From: Rick Arnold Date: Sat, 19 Mar 2022 17:45:57 -0400 Subject: [PATCH] run gofmt in cmd --- cmd/decrypt.go | 80 +++++++++--------- cmd/decryptFile.go | 172 +++++++++++++++++++------------------- cmd/encrypt.go | 108 ++++++++++++------------ cmd/encryptFile.go | 182 ++++++++++++++++++++-------------------- cmd/main.go | 118 +++++++++++++------------- cmd/recrypt.go | 134 +++++++++++++++--------------- cmd/recryptFile.go | 202 ++++++++++++++++++++++----------------------- 7 files changed, 498 insertions(+), 498 deletions(-) diff --git a/cmd/decrypt.go b/cmd/decrypt.go index 1dd5e82..b478399 100644 --- a/cmd/decrypt.go +++ b/cmd/decrypt.go @@ -1,40 +1,40 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/rickar/props" -) - -var ( - decryptFlags = flag.NewFlagSet("decrypt", flag.ExitOnError) - decryptValue = decryptFlags.String("value", "", "`encrypted` value to decrypt (including algorithm prefix)") - decryptPass = decryptFlags.String("password", "", "`password` to decrypt the value") -) - -func init() { - decryptFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "decrypt: decrypt an encrypted property value\n") - decryptFlags.PrintDefaults() - } -} - -func decrypt() { - if *decryptValue == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") - decryptFlags.Usage() - os.Exit(100) - } - if *decryptPass == "" { - *decryptPass = readPassword("Password:", decryptFlags, 100) - } - - dec, err := props.Decrypt(*decryptPass, *decryptValue) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err) - os.Exit(101) - } - fmt.Println(dec) -} +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/rickar/props" +) + +var ( + decryptFlags = flag.NewFlagSet("decrypt", flag.ExitOnError) + decryptValue = decryptFlags.String("value", "", "`encrypted` value to decrypt (including algorithm prefix)") + decryptPass = decryptFlags.String("password", "", "`password` to decrypt the value") +) + +func init() { + decryptFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "decrypt: decrypt an encrypted property value\n") + decryptFlags.PrintDefaults() + } +} + +func decrypt() { + if *decryptValue == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") + decryptFlags.Usage() + os.Exit(100) + } + if *decryptPass == "" { + *decryptPass = readPassword("Password:", decryptFlags, 100) + } + + dec, err := props.Decrypt(*decryptPass, *decryptValue) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err) + os.Exit(101) + } + fmt.Println(dec) +} diff --git a/cmd/decryptFile.go b/cmd/decryptFile.go index 1824056..bd00caa 100644 --- a/cmd/decryptFile.go +++ b/cmd/decryptFile.go @@ -1,86 +1,86 @@ -package main - -import ( - "bufio" - "bytes" - "flag" - "fmt" - "os" - "strings" - - "github.com/rickar/props" -) - -var ( - decryptFileFlags = flag.NewFlagSet("decryptFile", flag.ExitOnError) - decryptFilePath = decryptFileFlags.String("path", "", "properties `file` to decrypt") - decryptFilePass = decryptFileFlags.String("password", "", "`password` to decrypt the values") - decryptFileOutput = decryptFileFlags.String("output", "", "output `file` to write results (default is input file)") -) - -func init() { - decryptFileFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "decryptFile: decrypt encrypted values in a property file\n") - decryptFileFlags.PrintDefaults() - } -} - -func decryptFile() { - if *decryptFilePath == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") - decryptFileFlags.Usage() - os.Exit(200) - } else { - stat, err := os.Stat(*decryptFilePath) - if err != nil || stat.IsDir() { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") - decryptFileFlags.Usage() - os.Exit(201) - } - } - if *decryptFilePass == "" { - *decryptFilePass = readPassword("Password:", decryptFileFlags, 202) - } - if *decryptFileOutput == "" { - *decryptFileOutput = *decryptFilePath - } - - f, err := os.Open(*decryptFilePath) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) - os.Exit(203) - } - defer f.Close() - - found := 0 - var result bytes.Buffer - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - if strings.Contains(line, props.EncryptAESGCM) { - found++ - i := strings.Index(line, props.EncryptAESGCM) - val := line[i:] - line := line[:i] - enc, err := props.Decrypt(*decryptFilePass, val) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err) - os.Exit(204) - } - result.WriteString(line) - result.WriteString(props.EncryptNone) - result.WriteString(enc) - result.WriteRune('\n') - } else { - result.WriteString(line) - result.WriteRune('\n') - } - } - f.Close() - err = os.WriteFile(*decryptFileOutput, result.Bytes(), 0o644) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) - os.Exit(205) - } - fmt.Printf("%d properties decrypted\n", found) -} +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "os" + "strings" + + "github.com/rickar/props" +) + +var ( + decryptFileFlags = flag.NewFlagSet("decryptFile", flag.ExitOnError) + decryptFilePath = decryptFileFlags.String("path", "", "properties `file` to decrypt") + decryptFilePass = decryptFileFlags.String("password", "", "`password` to decrypt the values") + decryptFileOutput = decryptFileFlags.String("output", "", "output `file` to write results (default is input file)") +) + +func init() { + decryptFileFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "decryptFile: decrypt encrypted values in a property file\n") + decryptFileFlags.PrintDefaults() + } +} + +func decryptFile() { + if *decryptFilePath == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") + decryptFileFlags.Usage() + os.Exit(200) + } else { + stat, err := os.Stat(*decryptFilePath) + if err != nil || stat.IsDir() { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") + decryptFileFlags.Usage() + os.Exit(201) + } + } + if *decryptFilePass == "" { + *decryptFilePass = readPassword("Password:", decryptFileFlags, 202) + } + if *decryptFileOutput == "" { + *decryptFileOutput = *decryptFilePath + } + + f, err := os.Open(*decryptFilePath) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) + os.Exit(203) + } + defer f.Close() + + found := 0 + var result bytes.Buffer + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, props.EncryptAESGCM) { + found++ + i := strings.Index(line, props.EncryptAESGCM) + val := line[i:] + line := line[:i] + enc, err := props.Decrypt(*decryptFilePass, val) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err) + os.Exit(204) + } + result.WriteString(line) + result.WriteString(props.EncryptNone) + result.WriteString(enc) + result.WriteRune('\n') + } else { + result.WriteString(line) + result.WriteRune('\n') + } + } + f.Close() + err = os.WriteFile(*decryptFileOutput, result.Bytes(), 0o644) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) + os.Exit(205) + } + fmt.Printf("%d properties decrypted\n", found) +} diff --git a/cmd/encrypt.go b/cmd/encrypt.go index b1281aa..e1f82a9 100644 --- a/cmd/encrypt.go +++ b/cmd/encrypt.go @@ -1,54 +1,54 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/rickar/props" -) - -var ( - encryptFlags = flag.NewFlagSet("encrypt", flag.ExitOnError) - encryptValue = encryptFlags.String("value", "", "`plaintext` value to encrypt") - encryptPass = encryptFlags.String("password", "", "`password` to encrypt the value") - encryptAlg = encryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") -) - -func init() { - encryptFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "encrypt: encrypt a value for use in a property file\n") - encryptFlags.PrintDefaults() - } -} - -func encrypt() { - if *encryptValue == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") - encryptFlags.Usage() - os.Exit(300) - } - if *encryptPass == "" { - *encryptPass = readPassword("Password:", encryptFlags, 301) - } - if *encryptAlg != props.EncryptAESGCM { - fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") - encryptFlags.Usage() - os.Exit(302) - } - - switch len(*encryptPass) { - case 16, 24, 32: - default: - fmt.Fprintf(flag.CommandLine.Output(), "the password parameter must be 16, 24, or 32 bytes\n") - encryptFlags.Usage() - os.Exit(303) - } - - enc, err := props.Encrypt(*encryptAlg, *encryptPass, *encryptValue) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err) - os.Exit(304) - } - fmt.Println(enc) -} +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/rickar/props" +) + +var ( + encryptFlags = flag.NewFlagSet("encrypt", flag.ExitOnError) + encryptValue = encryptFlags.String("value", "", "`plaintext` value to encrypt") + encryptPass = encryptFlags.String("password", "", "`password` to encrypt the value") + encryptAlg = encryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") +) + +func init() { + encryptFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "encrypt: encrypt a value for use in a property file\n") + encryptFlags.PrintDefaults() + } +} + +func encrypt() { + if *encryptValue == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") + encryptFlags.Usage() + os.Exit(300) + } + if *encryptPass == "" { + *encryptPass = readPassword("Password:", encryptFlags, 301) + } + if *encryptAlg != props.EncryptAESGCM { + fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") + encryptFlags.Usage() + os.Exit(302) + } + + switch len(*encryptPass) { + case 16, 24, 32: + default: + fmt.Fprintf(flag.CommandLine.Output(), "the password parameter must be 16, 24, or 32 bytes\n") + encryptFlags.Usage() + os.Exit(303) + } + + enc, err := props.Encrypt(*encryptAlg, *encryptPass, *encryptValue) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err) + os.Exit(304) + } + fmt.Println(enc) +} diff --git a/cmd/encryptFile.go b/cmd/encryptFile.go index a94b5d9..6b3d7d7 100644 --- a/cmd/encryptFile.go +++ b/cmd/encryptFile.go @@ -1,91 +1,91 @@ -package main - -import ( - "bufio" - "bytes" - "flag" - "fmt" - "os" - "strings" - - "github.com/rickar/props" -) - -var ( - encryptFileFlags = flag.NewFlagSet("encryptFile", flag.ExitOnError) - encryptFilePath = encryptFileFlags.String("path", "", "properties `file` to encrypt") - encryptFilePass = encryptFileFlags.String("password", "", "`password` to encrypt the values") - encryptFileAlg = encryptFileFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") - encryptFileOutput = encryptFileFlags.String("output", "", "output `file` to write results (default is input file)") -) - -func init() { - encryptFileFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "encryptFile: encrypt plaintext values in a property file\n") - encryptFileFlags.PrintDefaults() - } -} - -func encryptFile() { - if *encryptFilePath == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") - encryptFileFlags.Usage() - os.Exit(400) - } else { - stat, err := os.Stat(*encryptFilePath) - if err != nil || stat.IsDir() { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") - encryptFileFlags.Usage() - os.Exit(401) - } - } - if *encryptFilePass == "" { - *encryptFilePass = readPassword("Password:", encryptFileFlags, 402) - } - if *encryptFileAlg != props.EncryptAESGCM { - fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") - encryptFileFlags.Usage() - os.Exit(403) - } - if *encryptFileOutput == "" { - *encryptFileOutput = *encryptFilePath - } - - f, err := os.Open(*encryptFilePath) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) - os.Exit(404) - } - defer f.Close() - - found := 0 - var result bytes.Buffer - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - if strings.Contains(line, props.EncryptNone) { - found++ - i := strings.Index(line, props.EncryptNone) - val := line[i+len(props.EncryptNone):] - line := line[:i] - enc, err := props.Encrypt(*encryptFileAlg, *encryptFilePass, val) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to encrypt property: %v\n", err) - os.Exit(405) - } - result.WriteString(line) - result.WriteString(enc) - result.WriteRune('\n') - } else { - result.WriteString(line) - result.WriteRune('\n') - } - } - f.Close() - err = os.WriteFile(*encryptFileOutput, result.Bytes(), 0o644) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) - os.Exit(406) - } - fmt.Printf("%d properties encrypted\n", found) -} +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "os" + "strings" + + "github.com/rickar/props" +) + +var ( + encryptFileFlags = flag.NewFlagSet("encryptFile", flag.ExitOnError) + encryptFilePath = encryptFileFlags.String("path", "", "properties `file` to encrypt") + encryptFilePass = encryptFileFlags.String("password", "", "`password` to encrypt the values") + encryptFileAlg = encryptFileFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") + encryptFileOutput = encryptFileFlags.String("output", "", "output `file` to write results (default is input file)") +) + +func init() { + encryptFileFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "encryptFile: encrypt plaintext values in a property file\n") + encryptFileFlags.PrintDefaults() + } +} + +func encryptFile() { + if *encryptFilePath == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") + encryptFileFlags.Usage() + os.Exit(400) + } else { + stat, err := os.Stat(*encryptFilePath) + if err != nil || stat.IsDir() { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") + encryptFileFlags.Usage() + os.Exit(401) + } + } + if *encryptFilePass == "" { + *encryptFilePass = readPassword("Password:", encryptFileFlags, 402) + } + if *encryptFileAlg != props.EncryptAESGCM { + fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") + encryptFileFlags.Usage() + os.Exit(403) + } + if *encryptFileOutput == "" { + *encryptFileOutput = *encryptFilePath + } + + f, err := os.Open(*encryptFilePath) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) + os.Exit(404) + } + defer f.Close() + + found := 0 + var result bytes.Buffer + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, props.EncryptNone) { + found++ + i := strings.Index(line, props.EncryptNone) + val := line[i+len(props.EncryptNone):] + line := line[:i] + enc, err := props.Encrypt(*encryptFileAlg, *encryptFilePass, val) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to encrypt property: %v\n", err) + os.Exit(405) + } + result.WriteString(line) + result.WriteString(enc) + result.WriteRune('\n') + } else { + result.WriteString(line) + result.WriteRune('\n') + } + } + f.Close() + err = os.WriteFile(*encryptFileOutput, result.Bytes(), 0o644) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) + os.Exit(406) + } + fmt.Printf("%d properties encrypted\n", found) +} diff --git a/cmd/main.go b/cmd/main.go index 0467ad6..03b8bc1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,59 +1,59 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "golang.org/x/term" -) - -func main() { - if len(os.Args) < 2 { - fmt.Fprintf(os.Stderr, "a command is required (decrypt, decryptFile, encrypt, encryptFile, recrypt, recryptFile)\n") - os.Exit(1) - } - - switch os.Args[1] { - case "decrypt": - decryptFlags.Parse(os.Args[2:]) - decrypt() - case "decryptFile": - decryptFileFlags.Parse(os.Args[2:]) - decryptFile() - case "encrypt": - encryptFlags.Parse(os.Args[2:]) - encrypt() - case "encryptFile": - encryptFileFlags.Parse(os.Args[2:]) - encryptFile() - case "recrypt": - recryptFlags.Parse(os.Args[2:]) - recrypt() - case "recryptFile": - recryptFileFlags.Parse(os.Args[2:]) - recryptFile() - default: - fmt.Fprintf(os.Stderr, "a command is required (decrypt, decryptFile, encrypt, encryptFile, recrypt, recryptFile)\n") - os.Exit(2) - } -} - -func readPassword(prompt string, flags *flag.FlagSet, exitCode int) string { - fmt.Print(prompt) - bytes, err := term.ReadPassword(int(os.Stdin.Fd())) - fmt.Println() - - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to read password\n") - flags.Usage() - os.Exit(exitCode) - } - pass := string(bytes) - if pass == "" { - fmt.Fprintf(flag.CommandLine.Output(), "no password was provided\n") - flags.Usage() - os.Exit(exitCode) - } - return pass -} +package main + +import ( + "flag" + "fmt" + "os" + + "golang.org/x/term" +) + +func main() { + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "a command is required (decrypt, decryptFile, encrypt, encryptFile, recrypt, recryptFile)\n") + os.Exit(1) + } + + switch os.Args[1] { + case "decrypt": + decryptFlags.Parse(os.Args[2:]) + decrypt() + case "decryptFile": + decryptFileFlags.Parse(os.Args[2:]) + decryptFile() + case "encrypt": + encryptFlags.Parse(os.Args[2:]) + encrypt() + case "encryptFile": + encryptFileFlags.Parse(os.Args[2:]) + encryptFile() + case "recrypt": + recryptFlags.Parse(os.Args[2:]) + recrypt() + case "recryptFile": + recryptFileFlags.Parse(os.Args[2:]) + recryptFile() + default: + fmt.Fprintf(os.Stderr, "a command is required (decrypt, decryptFile, encrypt, encryptFile, recrypt, recryptFile)\n") + os.Exit(2) + } +} + +func readPassword(prompt string, flags *flag.FlagSet, exitCode int) string { + fmt.Print(prompt) + bytes, err := term.ReadPassword(int(os.Stdin.Fd())) + fmt.Println() + + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to read password\n") + flags.Usage() + os.Exit(exitCode) + } + pass := string(bytes) + if pass == "" { + fmt.Fprintf(flag.CommandLine.Output(), "no password was provided\n") + flags.Usage() + os.Exit(exitCode) + } + return pass +} diff --git a/cmd/recrypt.go b/cmd/recrypt.go index 0fe0641..5e71b6b 100644 --- a/cmd/recrypt.go +++ b/cmd/recrypt.go @@ -1,67 +1,67 @@ -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/rickar/props" -) - -var ( - recryptFlags = flag.NewFlagSet("recrypt", flag.ExitOnError) - recryptValue = recryptFlags.String("value", "", "`encrypted` value to re-encrypt") - recryptPass = recryptFlags.String("newpass", "", "new `password` to re-encrypt the value") - recryptOldPass = recryptFlags.String("oldpass", "", "old `password` to decrypt the value") - recryptAlg = recryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") -) - -func init() { - recryptFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "recrypt: re-encrypt a property value with a new password\n") - recryptFlags.PrintDefaults() - } -} - -func recrypt() { - if *recryptValue == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") - recryptFlags.Usage() - os.Exit(500) - } - if *recryptOldPass == "" { - *recryptOldPass = readPassword("Old Password:", recryptFlags, 501) - } - if *recryptPass == "" { - *recryptPass = readPassword("New Password:", recryptFlags, 502) - } - if *recryptAlg != props.EncryptAESGCM { - fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") - recryptFlags.Usage() - os.Exit(503) - } - - switch len(*recryptPass) { - case 16, 24, 32: - default: - fmt.Fprintf(flag.CommandLine.Output(), "the newpass parameter must be 16, 24, or 32 bytes\n") - recryptFlags.Usage() - os.Exit(504) - } - - p := props.NewProperties() - p.Set("val", *recryptValue) - c := props.Configuration{Props: p} - dec, err := c.Decrypt(*recryptOldPass, "val", "") - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err) - os.Exit(505) - } - - enc, err := props.Encrypt(*recryptAlg, *recryptPass, dec) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err) - os.Exit(506) - } - fmt.Println(enc) -} +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/rickar/props" +) + +var ( + recryptFlags = flag.NewFlagSet("recrypt", flag.ExitOnError) + recryptValue = recryptFlags.String("value", "", "`encrypted` value to re-encrypt") + recryptPass = recryptFlags.String("newpass", "", "new `password` to re-encrypt the value") + recryptOldPass = recryptFlags.String("oldpass", "", "old `password` to decrypt the value") + recryptAlg = recryptFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") +) + +func init() { + recryptFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "recrypt: re-encrypt a property value with a new password\n") + recryptFlags.PrintDefaults() + } +} + +func recrypt() { + if *recryptValue == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the value parameter is required\n") + recryptFlags.Usage() + os.Exit(500) + } + if *recryptOldPass == "" { + *recryptOldPass = readPassword("Old Password:", recryptFlags, 501) + } + if *recryptPass == "" { + *recryptPass = readPassword("New Password:", recryptFlags, 502) + } + if *recryptAlg != props.EncryptAESGCM { + fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") + recryptFlags.Usage() + os.Exit(503) + } + + switch len(*recryptPass) { + case 16, 24, 32: + default: + fmt.Fprintf(flag.CommandLine.Output(), "the newpass parameter must be 16, 24, or 32 bytes\n") + recryptFlags.Usage() + os.Exit(504) + } + + p := props.NewProperties() + p.Set("val", *recryptValue) + c := props.Configuration{Props: p} + dec, err := c.Decrypt(*recryptOldPass, "val", "") + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "decrypt error: %v\n", err) + os.Exit(505) + } + + enc, err := props.Encrypt(*recryptAlg, *recryptPass, dec) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "encrypt error: %v\n", err) + os.Exit(506) + } + fmt.Println(enc) +} diff --git a/cmd/recryptFile.go b/cmd/recryptFile.go index 1a9cac4..55886da 100644 --- a/cmd/recryptFile.go +++ b/cmd/recryptFile.go @@ -1,101 +1,101 @@ -package main - -import ( - "bufio" - "bytes" - "flag" - "fmt" - "os" - "strings" - - "github.com/rickar/props" -) - -var ( - recryptFileFlags = flag.NewFlagSet("recryptFile", flag.ExitOnError) - recryptFilePath = recryptFileFlags.String("path", "", "properties `file` to re-encrypt") - recryptFilePass = recryptFileFlags.String("newpass", "", "new `password` to re-encrypt the values") - recryptFileOldPass = recryptFileFlags.String("oldpass", "", "old `password` to decrypt the values") - recryptFileAlg = recryptFileFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") - recryptFileOutput = recryptFileFlags.String("output", "", "output `file` to write results (default is input file)") -) - -func init() { - recryptFileFlags.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "recryptFile: re-encrypt values in a property file\n") - recryptFileFlags.PrintDefaults() - } -} - -func recryptFile() { - if *recryptFilePath == "" { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") - recryptFileFlags.Usage() - os.Exit(600) - } else { - stat, err := os.Stat(*recryptFilePath) - if err != nil || stat.IsDir() { - fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") - recryptFileFlags.Usage() - os.Exit(601) - } - } - if *recryptFileAlg != props.EncryptAESGCM { - fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") - recryptFileFlags.Usage() - os.Exit(602) - } - if *recryptFileOldPass == "" { - *recryptFileOldPass = readPassword("Old Password:", recryptFileFlags, 603) - } - if *recryptFilePass == "" { - *recryptFilePass = readPassword("New Password:", recryptFileFlags, 604) - } - if *recryptFileOutput == "" { - *recryptFileOutput = *recryptFilePath - } - - f, err := os.Open(*recryptFilePath) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) - os.Exit(605) - } - defer f.Close() - - found := 0 - var result bytes.Buffer - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - if strings.Contains(line, props.EncryptAESGCM) { - found++ - i := strings.Index(line, props.EncryptAESGCM) - val := line[i:] - line := line[:i] - dec, err := props.Decrypt(*recryptFileOldPass, val) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err) - os.Exit(100) - } - - enc, err := props.Encrypt(*recryptFileAlg, *recryptFilePass, dec) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to encrypt property: %v\n", err) - os.Exit(606) - } - result.WriteString(line) - result.WriteString(enc) - result.WriteRune('\n') - } else { - result.WriteString(line) - result.WriteRune('\n') - } - } - f.Close() - err = os.WriteFile(*recryptFileOutput, result.Bytes(), 0o644) - if err != nil { - fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) - os.Exit(607) - } - fmt.Printf("%d properties re-encrypted\n", found) -} +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "os" + "strings" + + "github.com/rickar/props" +) + +var ( + recryptFileFlags = flag.NewFlagSet("recryptFile", flag.ExitOnError) + recryptFilePath = recryptFileFlags.String("path", "", "properties `file` to re-encrypt") + recryptFilePass = recryptFileFlags.String("newpass", "", "new `password` to re-encrypt the values") + recryptFileOldPass = recryptFileFlags.String("oldpass", "", "old `password` to decrypt the values") + recryptFileAlg = recryptFileFlags.String("alg", props.EncryptDefault, "encryption `algorithm` to use (see props.Encrypt*)") + recryptFileOutput = recryptFileFlags.String("output", "", "output `file` to write results (default is input file)") +) + +func init() { + recryptFileFlags.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "recryptFile: re-encrypt values in a property file\n") + recryptFileFlags.PrintDefaults() + } +} + +func recryptFile() { + if *recryptFilePath == "" { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter is required\n") + recryptFileFlags.Usage() + os.Exit(600) + } else { + stat, err := os.Stat(*recryptFilePath) + if err != nil || stat.IsDir() { + fmt.Fprintf(flag.CommandLine.Output(), "the path parameter must be an existing, readable file\n") + recryptFileFlags.Usage() + os.Exit(601) + } + } + if *recryptFileAlg != props.EncryptAESGCM { + fmt.Fprintf(flag.CommandLine.Output(), "the alg parameter must be an encryption algorithm id from props\n") + recryptFileFlags.Usage() + os.Exit(602) + } + if *recryptFileOldPass == "" { + *recryptFileOldPass = readPassword("Old Password:", recryptFileFlags, 603) + } + if *recryptFilePass == "" { + *recryptFilePass = readPassword("New Password:", recryptFileFlags, 604) + } + if *recryptFileOutput == "" { + *recryptFileOutput = *recryptFilePath + } + + f, err := os.Open(*recryptFilePath) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to read property file: %v\n", err) + os.Exit(605) + } + defer f.Close() + + found := 0 + var result bytes.Buffer + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, props.EncryptAESGCM) { + found++ + i := strings.Index(line, props.EncryptAESGCM) + val := line[i:] + line := line[:i] + dec, err := props.Decrypt(*recryptFileOldPass, val) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to decrypt property: %v\n", err) + os.Exit(100) + } + + enc, err := props.Encrypt(*recryptFileAlg, *recryptFilePass, dec) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to encrypt property: %v\n", err) + os.Exit(606) + } + result.WriteString(line) + result.WriteString(enc) + result.WriteRune('\n') + } else { + result.WriteString(line) + result.WriteRune('\n') + } + } + f.Close() + err = os.WriteFile(*recryptFileOutput, result.Bytes(), 0o644) + if err != nil { + fmt.Fprintf(flag.CommandLine.Output(), "unable to write output: %v\n", err) + os.Exit(607) + } + fmt.Printf("%d properties re-encrypted\n", found) +}