Skip to content

Commit

Permalink
Add -bin flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Dec 10, 2023
1 parent a76c39b commit 10a441b
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 21 deletions.
9 changes: 3 additions & 6 deletions internal/aes/cbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions internal/aes/cbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions internal/aes/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions internal/aes/ecb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions internal/aes/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions internal/aes/gcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/rsa/pem2der.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions internal/kdf/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -69,5 +70,5 @@ Options:

output := pbkdf2.Key(key, salt, *fIter, *fLen, hashFunc)

return sf.Write(output, true)
return sf.Write(output, *fBin)
}
1 change: 1 addition & 0 deletions internal/kdf/pbkdf2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions internal/kem/rsa/cmd/kem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -117,5 +118,5 @@ Options:
return err
}

return sf.Write(output, true)
return sf.Write(output, *fBin)
}
5 changes: 3 additions & 2 deletions internal/keywrap/aes/cmd/keywrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -65,5 +66,5 @@ Options:
return err
}

return sf.Write(output, true)
return sf.Write(output, *fBin)
}
1 change: 1 addition & 0 deletions internal/keywrap/aes/cmd/keywrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions todo.org
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 10a441b

Please sign in to comment.