-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem2der.go
54 lines (42 loc) · 1.14 KB
/
pem2der.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package rsa
import (
"encoding/pem"
"errors"
"bandr.me/p/pocryp/internal/cli/cmd"
"bandr.me/p/pocryp/internal/util/stdfile"
)
var Pem2DerCmd = &cmd.Command{
Name: "rsa-pem2der",
Run: runPem2Der,
Brief: "Convert RSA key from PEM to PKCS#1 ASN.1 DER",
Usage: `Usage: pocryp rsa-pem2der [-bin] [-in INPUT] [-out OUTPUT]
Convert RSA key from PEM to PKCS#1 ASN.1 DER.
If -in is not specified, stdin will be read.
If -out is not specified, the output will be printed to stdout.
`,
}
func runPem2Der(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.")
fBin := cmd.Flags.Bool("bin", false, "Write output as binary not hex.")
if isHelp, err := cmd.Parse(); err != nil {
if isHelp {
return nil
}
return err
}
sf, err := stdfile.New(*fInput, *fOutput)
if err != nil {
return err
}
defer sf.Close()
input, err := sf.Read()
if err != nil {
return err
}
block, _ := pem.Decode(input)
if block == nil {
return errors.New("failed to parse PEM block")
}
return sf.Write(block.Bytes, *fBin)
}