Skip to content

Commit

Permalink
Use *os.File instead of io.Reader/io.Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Dec 10, 2023
1 parent 1490ecb commit 6b2a1d5
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 164 deletions.
20 changes: 8 additions & 12 deletions internal/aes/cbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
32 changes: 13 additions & 19 deletions internal/aes/cmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
20 changes: 8 additions & 12 deletions internal/aes/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
20 changes: 8 additions & 12 deletions internal/aes/gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
20 changes: 8 additions & 12 deletions internal/encoding/rsa/der2pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
22 changes: 9 additions & 13 deletions internal/encoding/rsa/pem2der.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
20 changes: 8 additions & 12 deletions internal/encoding/rsa/priv2pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
Loading

0 comments on commit 6b2a1d5

Please sign in to comment.