-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (114 loc) · 4.26 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"certutil/codec"
"certutil/keypairgen"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"fmt"
"path/filepath"
"time"
)
type KeyPair struct {
Type string
Key interface{}
}
func main() {
startTime := time.Now()
fmt.Printf("Main function started at: %s\n\n", startTime.Format(time.RFC3339))
iterations := 5
// Generate key pairs
keyPairs := generateKeys(iterations)
// Write key pairs to files
writeKeys(keyPairs)
// Read keys from files
readKeys(keyPairs)
fmt.Printf("\nTotal execution time: %d ms\n", time.Since(startTime).Milliseconds())
}
func generateKeys(iterations int) []KeyPair {
fmt.Println("Generating key pairs:")
rsaStart := time.Now()
rsaPool := keypairgen.NewKeyPairPool(iterations, iterations, keypairgen.GenerateRSAKeyPair(2048))
fmt.Printf("RSA KeyPairPool creation took: %d ms\n", time.Since(rsaStart).Milliseconds())
defer rsaPool.Close()
ecStart := time.Now()
ecPool := keypairgen.NewKeyPairPool(iterations, 1, keypairgen.GenerateECKeyPair(elliptic.P256()))
fmt.Printf("EC KeyPairPool creation took: %d ms\n", time.Since(ecStart).Milliseconds())
defer ecPool.Close()
edStart := time.Now()
edPool := keypairgen.NewKeyPairPool(iterations, 1, keypairgen.GenerateEDKeyPair("Ed25519"))
fmt.Printf("ED KeyPairPool creation took: %d ms\n\n", time.Since(edStart).Milliseconds())
defer edPool.Close()
keyPairs := make([]KeyPair, 0, iterations*3)
for i := 0; i < iterations; i++ {
fmt.Printf("Iteration %d:\n", i+1)
start := time.Now()
rsaKey := rsaPool.Get().(*rsa.PrivateKey)
rsaDuration := time.Since(start).Milliseconds()
fmt.Printf("Retrieved RSA Key in %d ms\n", rsaDuration)
keyPairs = append(keyPairs, KeyPair{"rsa2048", rsaKey})
start = time.Now()
ecKey := ecPool.Get().(*ecdsa.PrivateKey)
ecDuration := time.Since(start).Milliseconds()
fmt.Printf("Retrieved EC Key in %d ms\n", ecDuration)
keyPairs = append(keyPairs, KeyPair{"ecp256", ecKey})
start = time.Now()
edKey := edPool.Get().(ed25519.PrivateKey)
edDuration := time.Since(start).Milliseconds()
fmt.Printf("Retrieved ED Key in %d ms\n", edDuration)
keyPairs = append(keyPairs, KeyPair{"ed25519", edKey})
fmt.Println()
}
return keyPairs
}
func writeKeys(keyPairs []KeyPair) {
fmt.Println("Writing key pairs to files:")
start := time.Now()
for i, kp := range keyPairs {
baseFilename := filepath.Join("output", fmt.Sprintf("keypair_%d_%s", i+1, kp.Type))
err := codec.WriteKeyToPEMFile(kp.Key, baseFilename+"_pri.pem", false)
logFileOperation("write", baseFilename+"_pri.pem", err)
err = codec.WriteKeyToDERFile(kp.Key, baseFilename+"_pri.der", false)
logFileOperation("write", baseFilename+"_pri.der", err)
var pubKey interface{}
switch k := kp.Key.(type) {
case *rsa.PrivateKey:
pubKey = &k.PublicKey
case *ecdsa.PrivateKey:
pubKey = &k.PublicKey
case ed25519.PrivateKey:
pubKey = k.Public()
}
err = codec.WriteKeyToPEMFile(pubKey, baseFilename+"_pub.pem", true)
logFileOperation("write", baseFilename+"_pub.pem", err)
err = codec.WriteKeyToDERFile(pubKey, baseFilename+"_pub.der", true)
logFileOperation("write", baseFilename+"_pub.der", err)
}
duration := time.Since(start).Milliseconds()
fmt.Printf("Total time to write all key pairs: %d ms\n\n", duration)
}
func readKeys(keyPairs []KeyPair) {
fmt.Println("Reading key pairs from files:")
start := time.Now()
for i, kp := range keyPairs {
baseFilename := filepath.Join("output", fmt.Sprintf("keypair_%d_%s", i+1, kp.Type))
_, err := codec.ReadKeyFromPEMFile(baseFilename + "_pri.pem")
logFileOperation("read", baseFilename+"_pri.pem", err)
_, err = codec.ReadKeyFromDERFile(baseFilename + "_pri.der")
logFileOperation("read", baseFilename+"_pri.der", err)
_, err = codec.ReadKeyFromPEMFile(baseFilename + "_pub.pem")
logFileOperation("read", baseFilename+"_pub.pem", err)
_, err = codec.ReadKeyFromDERFile(baseFilename + "_pub.der")
logFileOperation("read", baseFilename+"_pub.der", err)
}
duration := time.Since(start).Milliseconds()
fmt.Printf("Total time to read all key pairs: %d ms\n", duration)
}
func logFileOperation(operation, filename string, err error) {
if err != nil {
fmt.Printf("Error %sing %s: %v\n", operation, filename, err)
} else {
fmt.Printf("Successfully %s %s\n", operation, filename)
}
}