diff --git a/internal/aes/cbc.go b/internal/aes/cbc.go index b5b4615..0429234 100644 --- a/internal/aes/cbc.go +++ b/internal/aes/cbc.go @@ -16,7 +16,7 @@ import ( func CbcCmd(args ...string) error { fset := flag.NewFlagSet("aes-cbc", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp aes-cbc [-e/-d] -key/-key-file -iv [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp aes-cbc [-bin] [-e/-d] -key/-key-file -iv [-in INPUT] [-out OUTPUT] Encrypt/Decrypt INPUT to OUTPUT using AES-CBC. @@ -35,6 +35,7 @@ Options: fKey := fset.String("key", "", "Key as hex.") fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.") fIV := fset.String("iv", "", "IV as hex.") + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -81,11 +82,7 @@ Options: output := cbcProcessBlocks(c, input) - if err := sf.Write(output, true); err != nil { - return err - } - - return nil + return sf.Write(output, *fBin) } func newCBCEncrypter(key, iv []byte) (cipher.BlockMode, error) { diff --git a/internal/aes/cbc_test.go b/internal/aes/cbc_test.go index a0805c7..88e7e85 100644 --- a/internal/aes/cbc_test.go +++ b/internal/aes/cbc_test.go @@ -57,6 +57,7 @@ func testCbcCmd(t *testing.T, tmp string, direction string, key, iv, input, expe args = append(args, direction) } args = append(args, + "-bin", "-key", hex.EncodeToString(key), "-iv", hex.EncodeToString(iv), "-in", in, diff --git a/internal/aes/ecb.go b/internal/aes/ecb.go index 044c8ee..a1ca7ce 100644 --- a/internal/aes/ecb.go +++ b/internal/aes/ecb.go @@ -14,7 +14,7 @@ import ( func EcbCmd(args ...string) error { fset := flag.NewFlagSet("aes-ecb", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp aes-ecb [-e/-d] -key|-key-file [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp aes-ecb [-bin] [-e/-d] -key|-key-file [-in INPUT] [-out OUTPUT] Encrypt/Decrypt INPUT to OUTPUT using AES-ECB. @@ -32,6 +32,7 @@ Options: fInput := fset.String("in", "", "Read data from the file at path INPUT.") fKey := fset.String("key", "", "Key as hex.") fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.") + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -66,7 +67,7 @@ Options: return err } - return sf.Write(output, true) + return sf.Write(output, *fBin) } func ecb(key, in []byte, direction bool) ([]byte, error) { diff --git a/internal/aes/ecb_test.go b/internal/aes/ecb_test.go index e2a63d2..3dba698 100644 --- a/internal/aes/ecb_test.go +++ b/internal/aes/ecb_test.go @@ -50,6 +50,7 @@ func testEcbCmd(t *testing.T, tmp string, direction string, key, input, expected args = append(args, direction) } args = append(args, + "-bin", "-key", hex.EncodeToString(key), "-in", in, "-out", out, diff --git a/internal/aes/gcm.go b/internal/aes/gcm.go index f09b2bc..898b163 100644 --- a/internal/aes/gcm.go +++ b/internal/aes/gcm.go @@ -16,7 +16,7 @@ import ( func GcmCmd(args ...string) error { fset := flag.NewFlagSet("aes-gcm", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp aes-gcm [-e/-d] -key|-key-file -iv -aad [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp aes-gcm [-bin] [-e/-d] -key|-key-file -iv -aad [-in INPUT] [-out OUTPUT] Encrypt/Decrypt INPUT to OUTPUT using AES-GCM. @@ -36,6 +36,7 @@ Options: fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.") fIV := fset.String("iv", "", "IV as hex.") fAAD := fset.String("aad", "", "File which contains additional associated data as binary/text.") + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -89,7 +90,7 @@ Options: return err } - return sf.Write(output, true) + return sf.Write(output, *fBin) } func gcm(key, nonce, in, additionalData []byte, direction bool) ([]byte, error) { diff --git a/internal/aes/gcm_test.go b/internal/aes/gcm_test.go index 363041d..551f7b2 100644 --- a/internal/aes/gcm_test.go +++ b/internal/aes/gcm_test.go @@ -58,6 +58,7 @@ func testGcm(t *testing.T, tmp string, direction string, key, nonce, aad, input, args = append(args, direction) } args = append(args, + "-bin", "-key", hex.EncodeToString(key), "-iv", hex.EncodeToString(nonce), "-in", in, diff --git a/internal/encoding/rsa/pem2der.go b/internal/encoding/rsa/pem2der.go index d6a9f57..d76a6d3 100644 --- a/internal/encoding/rsa/pem2der.go +++ b/internal/encoding/rsa/pem2der.go @@ -13,7 +13,7 @@ import ( func Pem2DerCmd(args ...string) error { fset := flag.NewFlagSet("rsa-pem2der", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp rsa-pem2der [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp rsa-pem2der [-bin] [-in INPUT] [-out OUTPUT] Convert RSA key from PEM to PKCS#1 ASN.1 DER. @@ -27,7 +27,7 @@ Options: fOutput := fset.String("out", "", "Write the result to the file at path OUTPUT.") fInput := fset.String("in", "", "Read data from the file at path INPUT.") - fPrintBin := fset.Bool("bin", false, "Print output in binary form.") + fBin := fset.Bool("bin", false, "Write output as binary not hex.") if err := fset.Parse(args); err != nil { return err @@ -49,5 +49,5 @@ Options: return errors.New("failed to parse PEM block") } - return sf.Write(block.Bytes, *fPrintBin) + return sf.Write(block.Bytes, *fBin) } diff --git a/internal/kdf/pbkdf2.go b/internal/kdf/pbkdf2.go index ca24b4b..e85055f 100644 --- a/internal/kdf/pbkdf2.go +++ b/internal/kdf/pbkdf2.go @@ -15,7 +15,7 @@ import ( func Pbkdf2Cmd(args ...string) error { fset := flag.NewFlagSet("kdf-pbkdf2", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp kdf-pbkdf2 -key|-key-file -salt|-salt-file -iter -len -hash [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp kdf-pbkdf2 [-bin] -key|-key-file -salt|-salt-file -iter -len -hash [-out OUTPUT] Derive a new key from the given key using PBKDF2. @@ -38,6 +38,7 @@ Options: common.AlgSHA256, fmt.Sprintf("Hash function(valid options: %s).", common.SHAAlgs), ) + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -69,5 +70,5 @@ Options: output := pbkdf2.Key(key, salt, *fIter, *fLen, hashFunc) - return sf.Write(output, true) + return sf.Write(output, *fBin) } diff --git a/internal/kdf/pbkdf2_test.go b/internal/kdf/pbkdf2_test.go index a10fb18..0430dde 100644 --- a/internal/kdf/pbkdf2_test.go +++ b/internal/kdf/pbkdf2_test.go @@ -59,6 +59,7 @@ func TestPbkdf2Cmd(t *testing.T) { } defer f.Close() args := []string{ + "-bin", "-key", hex.EncodeToString(tv.p), "-salt", hex.EncodeToString(tv.s), "-iter", fmt.Sprintf("%d", tv.c), diff --git a/internal/kem/rsa/cmd/kem.go b/internal/kem/rsa/cmd/kem.go index 0cb3535..02684a2 100644 --- a/internal/kem/rsa/cmd/kem.go +++ b/internal/kem/rsa/cmd/kem.go @@ -18,7 +18,7 @@ import ( func Run(args ...string) error { fset := flag.NewFlagSet("kem-rsa", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp kem-rsa [-e/-d] -key [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp kem-rsa [-bin] [-e/-d] -key [-in INPUT] [-out OUTPUT] Encapsulate/Decapsulate INPUT to OUTPUT using RSA-KEM. @@ -43,6 +43,7 @@ Options: common.AlgSHA256, fmt.Sprintf("KDF hash function(valid options: %s).", common.SHAAlgs), ) + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -117,5 +118,5 @@ Options: return err } - return sf.Write(output, true) + return sf.Write(output, *fBin) } diff --git a/internal/keywrap/aes/cmd/keywrap.go b/internal/keywrap/aes/cmd/keywrap.go index 0ebb10b..741bda3 100644 --- a/internal/keywrap/aes/cmd/keywrap.go +++ b/internal/keywrap/aes/cmd/keywrap.go @@ -13,7 +13,7 @@ import ( func Run(args ...string) error { fset := flag.NewFlagSet("aes-keywrap", flag.ContinueOnError) fset.Usage = func() { - fmt.Fprint(os.Stderr, `Usage: pocryp aes-keywrap [-w/-u] -key/-key-file [-in INPUT] [-out OUTPUT] + fmt.Fprint(os.Stderr, `Usage: pocryp aes-keywrap [-bin] [-w/-u] -key/-key-file [-in INPUT] [-out OUTPUT] Wrap/Unwrap INPUT to OUTPUT using AES-KEYWRAP. @@ -31,6 +31,7 @@ Options: fInput := fset.String("in", "", "Read data from the file at path INPUT.") fKey := fset.String("key", "", "Key as hex.") fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.") + fBin := fset.Bool("bin", false, "Print output in binary form not hex.") if err := fset.Parse(args); err != nil { return err @@ -65,5 +66,5 @@ Options: return err } - return sf.Write(output, true) + return sf.Write(output, *fBin) } diff --git a/internal/keywrap/aes/cmd/keywrap_test.go b/internal/keywrap/aes/cmd/keywrap_test.go index 07d0b74..026da40 100644 --- a/internal/keywrap/aes/cmd/keywrap_test.go +++ b/internal/keywrap/aes/cmd/keywrap_test.go @@ -43,6 +43,7 @@ func testCmd(t *testing.T, tmp string, direction string, key, input, expected [] args = append(args, direction) } args = append(args, + "-bin", "-key", hex.EncodeToString(key), "-in", in, "-out", out, diff --git a/todo.org b/todo.org index 0116940..a95ffe0 100644 --- a/todo.org +++ b/todo.org @@ -1,6 +1,6 @@ -* todo [3/5] -** TODO add -bin wherever needed +* todo [4/5] ** TODO add some sort of abstraction for adding subcommands +** DONE add -bin wherever needed ** DONE use FileOrHex ** DONE use stdfile everywhere ** DONE split RSA-KEM and AES-KEYWRAP into actual pkg and cmd pkg