-
Notifications
You must be signed in to change notification settings - Fork 0
/
der2raw.go
67 lines (56 loc) · 1.44 KB
/
der2raw.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
55
56
57
58
59
60
61
62
63
64
65
66
67
package rsa
import (
"crypto/x509"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"
)
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
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.")
if err := fset.Parse(args); err != nil {
return err
}
if fset.NArg() != 1 {
fset.Usage()
return errors.New("DER hex string not specified")
}
input, err := hex.DecodeString(fset.Arg(0))
if err != nil {
return err
}
switch {
case *fPriv:
key, err := x509.ParsePKCS1PrivateKey(input)
if err != nil {
return err
}
fmt.Printf("n=%s\n", hex.EncodeToString(key.N.Bytes()))
fmt.Printf("e=%x\n", key.E)
fmt.Printf("d=%s\n", hex.EncodeToString(key.D.Bytes()))
fmt.Printf("p=%s\n", hex.EncodeToString(key.Primes[0].Bytes()))
fmt.Printf("q=%s\n", hex.EncodeToString(key.Primes[1].Bytes()))
case *fPub:
key, err := x509.ParsePKCS1PublicKey(input)
if err != nil {
return err
}
fmt.Printf("n=%s\n", hex.EncodeToString(key.N.Bytes()))
fmt.Printf("e=%x\n", key.E)
default:
fset.Usage()
return errors.New("need to specify one of -priv or -pub")
}
return nil
}