-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
58 lines (48 loc) · 1.25 KB
/
benchmark_test.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
package testpackage
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"testing"
)
func BenchmarkEncryptOAEP(b *testing.B) {
// Generate an RSA key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
b.Fatal("Error generating RSA key:", err)
}
privateKey.Precompute()
// Generate a random nonce
nonce := make([]byte, 32)
for n := 0; n < b.N; n++ {
if _, err := rand.Read(nonce); err != nil {
b.Fatal("Error generating nonce:", err)
}
// Encrypt the nonce
_, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, &privateKey.PublicKey, nonce, nil)
if err != nil {
b.Fatal("Error encrypting nonce:", err)
}
}
}
func BenchmarkSignPSS(b *testing.B) {
var err error
var hash [32]byte
jsonStr := `{"PubKey": "encryptedPub", "Share": "encryptedShare", "OtherData": "dummyData"}`
jsonBytes := []byte(jsonStr)
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
b.Fatal("Error generating RSA key:", err)
}
privateKey.Precompute()
jsonBytesCopy := make([]byte, len(jsonBytes))
copy(jsonBytesCopy, jsonBytes)
for n := 0; n < b.N; n++ {
hash = sha256.Sum256(jsonBytesCopy)
_, err = rsa.SignPSS(rand.Reader, privateKey, crypto.SHA256, hash[:], nil)
if err != nil {
b.Fatal(err)
}
}
}