@@ -19,12 +19,14 @@ var (
19
19
keyType string
20
20
nBits int
21
21
filename string
22
+ readPriv bool
22
23
)
23
24
24
25
func init () {
25
26
flag .StringVar (& keyType , "t" , "rsa" , "key type (rsa, ed25519)" )
26
27
flag .IntVar (& nBits , "b" , 3072 , "number of bits in the key (only for RSA)" )
27
28
flag .StringVar (& filename , "f" , "dkim.priv" , "private key filename" )
29
+ flag .BoolVar (& readPriv , "y" , false , "read private key and print public key" )
28
30
flag .Parse ()
29
31
}
30
32
@@ -33,8 +35,13 @@ type privateKey interface {
33
35
}
34
36
35
37
func main () {
36
- privKey := genPrivKey ()
37
- writePrivKey (privKey )
38
+ var privKey privateKey
39
+ if readPriv {
40
+ privKey = readPrivKey ()
41
+ } else {
42
+ privKey = genPrivKey ()
43
+ writePrivKey (privKey )
44
+ }
38
45
printPubKey (privKey .Public ())
39
46
}
40
47
@@ -59,6 +66,28 @@ func genPrivKey() privateKey {
59
66
return privKey
60
67
}
61
68
69
+ func readPrivKey () privateKey {
70
+ b , err := os .ReadFile (filename )
71
+ if err != nil {
72
+ log .Fatalf ("Failed to read public key file: %v" , err )
73
+ }
74
+
75
+ block , _ := pem .Decode (b )
76
+ if block == nil {
77
+ log .Fatalf ("Failed to decode PEM block" )
78
+ } else if block .Type != "PRIVATE KEY" {
79
+ log .Fatalf ("Not a private key" )
80
+ }
81
+
82
+ privKey , err := x509 .ParsePKCS8PrivateKey (block .Bytes )
83
+ if err != nil {
84
+ log .Fatalf ("Failed to parse private key: %v" , err )
85
+ }
86
+
87
+ log .Printf ("Private key read from %q" , filename )
88
+ return privKey .(privateKey )
89
+ }
90
+
62
91
func writePrivKey (privKey privateKey ) {
63
92
privBytes , err := x509 .MarshalPKCS8PrivateKey (privKey )
64
93
if err != nil {
0 commit comments