-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
72 lines (63 loc) · 1.52 KB
/
main.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
68
69
70
71
72
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"time"
)
func main() {
cmd := &cobra.Command{
Short: "签名生成工具。",
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
Run: func(cmd *cobra.Command, args []string) {
bits := sha256.Size * viper.GetInt("sha256_len")
priKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
panic(err)
}
nowStr := time.Now().Format("2006-01-02T15_04_05")
priKeyFile, err := os.Create(fmt.Sprintf("rsa-%d-%s.pem", bits, nowStr))
if err != nil {
panic(err)
}
defer priKeyFile.Close()
err = pem.Encode(priKeyFile, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priKey),
})
if err != nil {
panic(err)
}
pubKeyFile, err := os.Create(fmt.Sprintf("ras-%d-%s.pub", bits, nowStr))
if err != nil {
panic(err)
}
defer pubKeyFile.Close()
err = pem.Encode(pubKeyFile, &pem.Block{
Type: "PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(&priKey.PublicKey),
})
if err != nil {
panic(err)
}
fmt.Printf("saved to %s, %s\n", priKeyFile.Name(), pubKeyFile.Name())
},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
DisableNoDescFlag: true,
DisableDescriptions: true,
},
}
cmd.Flags().Int("sha256_len", 64, "sha256 hash length")
if err := cmd.Execute(); err != nil {
panic(err)
}
}