Skip to content

Commit

Permalink
Fix cli
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Dec 12, 2023
1 parent 38f0002 commit defe9d7
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 201 deletions.
43 changes: 21 additions & 22 deletions internal/aes/cbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,38 @@ import (
"crypto/cipher"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/util"
"bandr.me/p/pocryp/internal/util/stdfile"
)

func CbcCmd(args ...string) error {
fset := flag.NewFlagSet("aes-cbc", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-cbc [-bin] [-e/-d] -key/-key-file -iv [-in INPUT] [-out OUTPUT]
var CbcCmd = &cmd.Command{
Name: "aes-cbc",
Run: runCbc,
Brief: "Encrypt/Decrypt using AES-CBC",

Usage: `Usage: pocryp aes-cbc [-bin] [-e/-d] -key/-key-file -iv [-in INPUT] [-out OUTPUT]
Encrypt/Decrypt INPUT to OUTPUT using AES-CBC.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
`,
}

Options:
`)
fset.PrintDefaults()
}

fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
fDecrypt := fset.Bool("d", false, "Decrypt the input to the output.")
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.")
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 {
func runCbc(cmd *cmd.Command) error {
fEncrypt := cmd.Flags.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
fDecrypt := cmd.Flags.Bool("d", false, "Decrypt the input to the output.")
fOutput := cmd.Flags.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := cmd.Flags.String("in", "", "Read data from the file at path INPUT.")
fKey := cmd.Flags.String("key", "", "Key as hex.")
fKeyFile := cmd.Flags.String("key-file", "", "File which contains the key as binary/text.")
fIV := cmd.Flags.String("iv", "", "IV as hex.")
fBin := cmd.Flags.Bool("bin", false, "Print output in binary form not hex.")

if err := cmd.Parse(); err != nil {
return err
}

Expand All @@ -47,7 +46,7 @@ Options:
}

if *fIV == "" {
fset.Usage()
cmd.Flags.Usage()
return errors.New("no IV specified, use -iv to specify it")
}

Expand Down
61 changes: 30 additions & 31 deletions internal/aes/cmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@ package aes
import (
"crypto/aes"
"encoding/hex"
"flag"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/util"
"bandr.me/p/pocryp/internal/util/stdfile"

"github.com/aead/cmac"
)

func CmacGenerateCmd(args ...string) error {
fset := flag.NewFlagSet("aes-cmac-generate", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-cmac-generate [-bin] -key|-key-file [-in INPUT] [-out OUTPUT]
var CmacGenerateCmd = &cmd.Command{
Name: "aes-cmac-generate",
Run: runCmacGenerate,
Brief: "Generate MAC using AES-CMAC",

Usage: `Usage: pocryp aes-cmac-generate [-bin] -key|-key-file [-in INPUT] [-out OUTPUT]
Generate MAC using AES-CMAC.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
`,
}

Options:
`)
fset.PrintDefaults()
}

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.")
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.")
func runCmacGenerate(cmd *cmd.Command) error {
fOutput := cmd.Flags.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := cmd.Flags.String("in", "", "Read data from the file at path INPUT.")
fKey := cmd.Flags.String("key", "", "Key as hex.")
fKeyFile := cmd.Flags.String("key-file", "", "File which contains the key as binary/text.")
fBin := cmd.Flags.Bool("bin", false, "Print output in binary form not hex.")

if err := fset.Parse(args); err != nil {
if err := cmd.Parse(); err != nil {
return err
}

Expand Down Expand Up @@ -67,26 +66,26 @@ Options:
return sf.Write(output, *fBin)
}

func CmacVerifyCmd(args ...string) error {
fset := flag.NewFlagSet("aes-cmac-verify", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-cmac-verify -key|-key-file -in MESSAGE -mac MAC
var CmacVerifyCmd = &cmd.Command{
Name: "aes-cmac-verify",
Run: runCmacVerify,
Brief: "Verify MAC using AES-CMAC",

Usage: `Usage: pocryp aes-cmac-verify -key|-key-file -in MESSAGE -mac MAC
Verify MAC using AES-CMAC.
If -in is not specified, stdin will be read.
`,
}

Options:
`)
fset.PrintDefaults()
}

fInput := fset.String("in", "", "Read message from the file at path INPUT.")
fMac := fset.String("mac", "", "Expected MAC as hex string.")
fKey := fset.String("key", "", "Key as hex.")
fKeyFile := fset.String("key-file", "", "File which contains the key as binary/text.")
func runCmacVerify(cmd *cmd.Command) error {
fInput := cmd.Flags.String("in", "", "Read message from the file at path INPUT.")
fMac := cmd.Flags.String("mac", "", "Expected MAC as hex string.")
fKey := cmd.Flags.String("key", "", "Key as hex.")
fKeyFile := cmd.Flags.String("key-file", "", "File which contains the key as binary/text.")

if err := fset.Parse(args); err != nil {
if err := cmd.Parse(); err != nil {
return err
}

Expand Down
44 changes: 22 additions & 22 deletions internal/aes/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ import (
"crypto/cipher"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/util"
"bandr.me/p/pocryp/internal/util/stdfile"
)

func GcmCmd(args ...string) error {
fset := flag.NewFlagSet("aes-gcm", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp aes-gcm [-bin] [-e/-d] -key|-key-file -iv -aad [-in INPUT] [-out OUTPUT]
var GcmCmd = &cmd.Command{
Name: "aes-gcm",
Run: runGcm,
Brief: "Encrypt/Decrypt using AES-GCM",

Usage: `Usage: pocryp aes-gcm [-bin] [-e/-d] -key|-key-file -iv -aad [-in INPUT] [-out OUTPUT]
Encrypt/Decrypt INPUT to OUTPUT using AES-GCM.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
`,
}

Options:
`)
fset.PrintDefaults()
}

fEncrypt := fset.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
fDecrypt := fset.Bool("d", false, "Decrypt the input to the output.")
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.")
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.")
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 {
func runGcm(cmd *cmd.Command) error {
fEncrypt := cmd.Flags.Bool("e", false, "Encrypt the input to the output. Default if omitted.")
fDecrypt := cmd.Flags.Bool("d", false, "Decrypt the input to the output.")
fOutput := cmd.Flags.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := cmd.Flags.String("in", "", "Read data from the file at path INPUT.")
fKey := cmd.Flags.String("key", "", "Key as hex.")
fKeyFile := cmd.Flags.String("key-file", "", "File which contains the key as binary/text.")
fIV := cmd.Flags.String("iv", "", "IV as hex.")
fAAD := cmd.Flags.String("aad", "", "File which contains additional associated data as binary/text.")
fBin := cmd.Flags.Bool("bin", false, "Print output in binary form not hex.")

if err := cmd.Parse(); err != nil {
return err
}

Expand All @@ -48,7 +48,7 @@ Options:
}

if *fIV == "" {
fset.Usage()
cmd.Flags.Usage()
return errors.New("no IV specified, use -iv to specify it")
}

Expand Down
34 changes: 16 additions & 18 deletions internal/encoding/rsa/der2pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,34 @@ package rsa
import (
"encoding/pem"
"errors"
"flag"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/util/stdfile"
)

func Der2PemCmd(args ...string) error {
fset := flag.NewFlagSet("rsa-der2pem", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp rsa-der2pem -priv/-pub [-in INPUT] [-out OUTPUT]
var Der2PemCmd = &cmd.Command{
Name: "rsa-der2pem",
Run: runDer2Pem,
Brief: "Convert RSA key from PKCS#1 ASN.1 DER to PEM",

Usage: `Usage: pocryp rsa-der2pem -priv/-pub [-in INPUT] [-out OUTPUT]
Convert RSA key from PKCS#1 ASN.1 DER to PEM.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
DER input must be specified in binary form.
`,
}

Options:
`)
fset.PrintDefaults()
}

fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.")
fPub := fset.Bool("pub", false, "Encode PublicKey from given input.")
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.")
func runDer2Pem(cmd *cmd.Command) error {
fPriv := cmd.Flags.Bool("priv", false, "Encode PrivateKey from given input.")
fPub := cmd.Flags.Bool("pub", false, "Encode PublicKey from given input.")
fOutput := cmd.Flags.String("out", "", "Write the result to the file at path OUTPUT.")
fInput := cmd.Flags.String("in", "", "Read data from the file at path INPUT.")

if err := fset.Parse(args); err != nil {
if err := cmd.Parse(); err != nil {
return err
}

Expand All @@ -54,7 +52,7 @@ Options:
case *fPub:
blockType = "RSA PUBLIC KEY"
default:
fset.Usage()
cmd.Flags.Usage()
return errors.New("need to specify one of -priv or -pub")
}

Expand Down
36 changes: 18 additions & 18 deletions internal/encoding/rsa/der2raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import (
"crypto/x509"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"

"bandr.me/p/pocryp/internal/cli/cmd"
)

func Der2RawCmd(args ...string) error {
fset := flag.NewFlagSet("rsa-der2raw", flag.ContinueOnError)
fset.Usage = func() {
fmt.Fprint(os.Stderr, `Usage: pocryp rsa-der2raw -priv/-pub DER
var Der2RawCmd = &cmd.Command{
Name: "rsa-der2raw",
Run: runDer2Raw,
Brief: "Convert RSA key from PKCS#1 ASN.1 DER to raw values(n, e, d, p, q)",

Usage: `pocryp rsa-der2raw -priv/-pub DER
Convert RSA key from PKCS#1 ASN.1 DER to raw values(n, e, d, p, q).
DER must be specified in hex form.
`,
}

Options:
`)
fset.PrintDefaults()
}

fPriv := fset.Bool("priv", false, "Encode PrivateKey from given input.")
fPub := fset.Bool("pub", false, "Encode PublicKey from given input.")
func runDer2Raw(cmd *cmd.Command) error {
fPriv := cmd.Flags.Bool("priv", false, "Encode PrivateKey from given input.")
fPub := cmd.Flags.Bool("pub", false, "Encode PublicKey from given input.")

if err := fset.Parse(args); err != nil {
if err := cmd.Parse(); err != nil {
return err
}

if fset.NArg() != 1 {
fset.Usage()
if cmd.Flags.NArg() != 1 {
cmd.Flags.Usage()
return errors.New("DER hex string not specified")
}

input, err := hex.DecodeString(fset.Arg(0))
input, err := hex.DecodeString(cmd.Flags.Arg(0))
if err != nil {
return err
}
Expand All @@ -59,7 +59,7 @@ Options:
fmt.Printf("n=%s\n", hex.EncodeToString(key.N.Bytes()))
fmt.Printf("e=%x\n", key.E)
default:
fset.Usage()
cmd.Flags.Usage()
return errors.New("need to specify one of -priv or -pub")
}

Expand Down
Loading

0 comments on commit defe9d7

Please sign in to comment.