From 6b2a1d5242503d6d4ad89d40c752368190817f84 Mon Sep 17 00:00:00 2001 From: Andrei Burdulescu Date: Sun, 10 Dec 2023 13:13:30 +0200 Subject: [PATCH] Use *os.File instead of io.Reader/io.Writer --- internal/aes/cbc.go | 20 +++++++---------- internal/aes/cmac.go | 32 +++++++++++---------------- internal/aes/ecb.go | 20 +++++++---------- internal/aes/gcm.go | 20 +++++++---------- internal/encoding/rsa/der2pem.go | 20 +++++++---------- internal/encoding/rsa/pem2der.go | 22 ++++++++----------- internal/encoding/rsa/priv2pub.go | 20 +++++++---------- internal/hash/sha.go | 34 +++++++++++++---------------- internal/kdf/pbkdf2.go | 10 ++++----- internal/kem/rsa/cmd/kem.go | 20 +++++++---------- internal/keygen/aes.go | 13 +++++------ internal/keygen/ed25519.go | 13 +++++------ internal/keygen/rsa.go | 11 ++++------ internal/keywrap/aes/cmd/keywrap.go | 20 +++++++---------- 14 files changed, 111 insertions(+), 164 deletions(-) diff --git a/internal/aes/cbc.go b/internal/aes/cbc.go index 34911ac..4811204 100644 --- a/internal/aes/cbc.go +++ b/internal/aes/cbc.go @@ -74,32 +74,28 @@ Options: return err } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -118,7 +114,7 @@ Options: output := cbcProcessBlocks(c, input.Bytes()) - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } diff --git a/internal/aes/cmac.go b/internal/aes/cmac.go index 7807112..8382363 100644 --- a/internal/aes/cmac.go +++ b/internal/aes/cmac.go @@ -64,32 +64,28 @@ Options: key = b } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -104,11 +100,11 @@ Options: } if *fBin { - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } } else { - fmt.Fprintln(w, hex.EncodeToString(output)) + fmt.Fprintln(out, hex.EncodeToString(output)) } return nil @@ -171,20 +167,18 @@ Options: return err } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } diff --git a/internal/aes/ecb.go b/internal/aes/ecb.go index 1e55365..850f6aa 100644 --- a/internal/aes/ecb.go +++ b/internal/aes/ecb.go @@ -63,32 +63,28 @@ Options: key = b } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -106,7 +102,7 @@ Options: return err } - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } diff --git a/internal/aes/gcm.go b/internal/aes/gcm.go index 20365df..c10bd0c 100644 --- a/internal/aes/gcm.go +++ b/internal/aes/gcm.go @@ -85,32 +85,28 @@ Options: aad = b } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -127,7 +123,7 @@ Options: return err } - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } diff --git a/internal/encoding/rsa/der2pem.go b/internal/encoding/rsa/der2pem.go index f4b1179..831eb93 100644 --- a/internal/encoding/rsa/der2pem.go +++ b/internal/encoding/rsa/der2pem.go @@ -36,32 +36,28 @@ Options: return err } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -80,7 +76,7 @@ Options: Type: blockType, Bytes: input.Bytes(), } - if err := pem.Encode(w, block); err != nil { + if err := pem.Encode(out, block); err != nil { return err } diff --git a/internal/encoding/rsa/pem2der.go b/internal/encoding/rsa/pem2der.go index 8a06d54..0000feb 100644 --- a/internal/encoding/rsa/pem2der.go +++ b/internal/encoding/rsa/pem2der.go @@ -34,32 +34,28 @@ Options: return err } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -69,11 +65,11 @@ Options: } if *fPrintBin { - if _, err := io.Copy(w, bytes.NewBuffer(block.Bytes)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(block.Bytes)); err != nil { return err } } else { - fmt.Fprintln(w, hex.EncodeToString(block.Bytes)) + fmt.Fprintln(out, hex.EncodeToString(block.Bytes)) } return nil diff --git a/internal/encoding/rsa/priv2pub.go b/internal/encoding/rsa/priv2pub.go index a26ee8d..5b24566 100644 --- a/internal/encoding/rsa/priv2pub.go +++ b/internal/encoding/rsa/priv2pub.go @@ -34,32 +34,28 @@ Options: return err } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -75,7 +71,7 @@ Options: Bytes: x509.MarshalPKCS1PublicKey(&pubKey), } - if err := pem.Encode(w, pubKeyBlock); err != nil { + if err := pem.Encode(out, pubKeyBlock); err != nil { return err } diff --git a/internal/hash/sha.go b/internal/hash/sha.go index 1996441..a4b50c4 100644 --- a/internal/hash/sha.go +++ b/internal/hash/sha.go @@ -40,50 +40,46 @@ Options: return errors.New("hash alg not specified, use -alg") } - hashFunc, err := common.HashFuncFrom(*fAlg) - if err != nil { - fset.Usage() - return err - } - - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f + } + + hashFunc, err := common.HashFuncFrom(*fAlg) + if err != nil { + fset.Usage() + return err } h := hashFunc() - if _, err := io.Copy(h, r); err != nil { + if _, err := io.Copy(h, in); err != nil { return err } digest := h.Sum(nil) if *fBin { - if _, err := w.Write(digest); err != nil { + if _, err := out.Write(digest); err != nil { return err } } else { - fmt.Fprintln(w, hex.EncodeToString(digest)) + fmt.Fprintln(out, hex.EncodeToString(digest)) } return nil diff --git a/internal/kdf/pbkdf2.go b/internal/kdf/pbkdf2.go index ebd9630..26bf8b9 100644 --- a/internal/kdf/pbkdf2.go +++ b/internal/kdf/pbkdf2.go @@ -102,21 +102,19 @@ Options: salt = b } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } output := pbkdf2.Key(key, salt, *fIter, *fLen, hashFunc) - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } diff --git a/internal/kem/rsa/cmd/kem.go b/internal/kem/rsa/cmd/kem.go index ce2e43b..48cdee0 100644 --- a/internal/kem/rsa/cmd/kem.go +++ b/internal/kem/rsa/cmd/kem.go @@ -93,32 +93,28 @@ Options: HashFunc: kdfHashFunc, } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -135,7 +131,7 @@ Options: return err } - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err } diff --git a/internal/keygen/aes.go b/internal/keygen/aes.go index 966ed24..9b81aa8 100644 --- a/internal/keygen/aes.go +++ b/internal/keygen/aes.go @@ -6,7 +6,6 @@ import ( "errors" "flag" "fmt" - "io" "os" "strconv" ) @@ -49,16 +48,14 @@ Options: } numBits /= 8 - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } output := make([]byte, numBits) @@ -67,11 +64,11 @@ Options: } if *fBin { - if _, err := w.Write(output); err != nil { + if _, err := out.Write(output); err != nil { return err } } else { - fmt.Fprintln(w, hex.EncodeToString(output)) + fmt.Fprintln(out, hex.EncodeToString(output)) } return nil diff --git a/internal/keygen/ed25519.go b/internal/keygen/ed25519.go index cc55636..965a943 100644 --- a/internal/keygen/ed25519.go +++ b/internal/keygen/ed25519.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "flag" "fmt" - "io" "os" ) @@ -30,16 +29,14 @@ Options: return err } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } key, _, err := ed25519.GenerateKey(nil) @@ -48,11 +45,11 @@ Options: } if *fBin { - if _, err := w.Write(key); err != nil { + if _, err := out.Write(key); err != nil { return err } } else { - fmt.Fprintln(w, hex.EncodeToString(key)) + fmt.Fprintln(out, hex.EncodeToString(key)) } return nil diff --git a/internal/keygen/rsa.go b/internal/keygen/rsa.go index dd58837..391defc 100644 --- a/internal/keygen/rsa.go +++ b/internal/keygen/rsa.go @@ -8,7 +8,6 @@ import ( "errors" "flag" "fmt" - "io" "os" "strconv" ) @@ -49,16 +48,14 @@ Options: return errors.New("invalid num bits requested") } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } key, err := rsa.GenerateKey(rand.Reader, numBits) @@ -70,7 +67,7 @@ Options: Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key), } - if err := pem.Encode(w, block); err != nil { + if err := pem.Encode(out, block); err != nil { return err } diff --git a/internal/keywrap/aes/cmd/keywrap.go b/internal/keywrap/aes/cmd/keywrap.go index 394524c..93634a4 100644 --- a/internal/keywrap/aes/cmd/keywrap.go +++ b/internal/keywrap/aes/cmd/keywrap.go @@ -63,32 +63,28 @@ Options: key = b } - var r io.Reader - if *fInput == "" { - r = os.Stdin - } else { + in := os.Stdin + if *fInput != "" { f, err := os.Open(*fInput) if err != nil { return err } defer f.Close() - r = f + in = f } - var w io.Writer - if *fOutput == "" { - w = os.Stdout - } else { + out := os.Stdout + if *fOutput != "" { f, err := os.Create(*fOutput) if err != nil { return err } defer f.Close() - w = f + out = f } var input bytes.Buffer - if _, err := io.Copy(&input, r); err != nil { + if _, err := io.Copy(&input, in); err != nil { return err } @@ -106,7 +102,7 @@ Options: return err } - if _, err := io.Copy(w, bytes.NewBuffer(output)); err != nil { + if _, err := io.Copy(out, bytes.NewBuffer(output)); err != nil { return err }