-
Notifications
You must be signed in to change notification settings - Fork 0
/
payloadSizeImpact.go
61 lines (51 loc) · 2.11 KB
/
payloadSizeImpact.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
// File: payloadSizeImpact.go
// Berikut adalah program untuk menguji pengaruh ukuran payload pada pembuatan dan verifikasi token JWT dengan algoritma RSA dan HMAC SHA-3.
package main
import (
"crypto/rsa"
"fmt"
"time"
"github.com/golang-jwt/jwt/v4"
)
func payloadSizeImpact(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, hmacSecret []byte) {
sizes := []int{100, 1000, 10000}
for _, size := range sizes {
payload := generatePayload(size)
// RSA
start := time.Now()
createCustomRSAToken(privateKey, payload)
fmt.Printf("RSA token creation time with payload size %d: %s\n", size, time.Since(start))
tokenString, _ := createCustomRSAToken(privateKey, payload)
start = time.Now()
verifyRSAToken(tokenString, publicKey)
fmt.Printf("RSA token verification time with payload size %d: %s\n", size, time.Since(start))
// HMAC SHA-3
start = time.Now()
createCustomHMACSHA3Token(hmacSecret, payload)
fmt.Printf("HMAC SHA-3 token creation time with payload size %d: %s\n", size, time.Since(start))
tokenString, _ = createCustomHMACSHA3Token(hmacSecret, payload)
start = time.Now()
verifyHMACSHA3Token(tokenString, hmacSecret)
fmt.Printf("HMAC SHA-3 token verification time with payload size %d: %s\n", size, time.Since(start))
}
}
// Function to generate a payload of specified size
func generatePayload(size int) map[string]interface{} {
payload := make(map[string]interface{})
for i := 0; i < size; i++ {
payload[fmt.Sprintf("key%d", i)] = fmt.Sprintf("value%d", i)
}
return payload
}
// Function to create RSA token with custom payload
func createCustomRSAToken(privateKey *rsa.PrivateKey, payload map[string]interface{}) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims(payload))
tokenString, err := token.SignedString(privateKey)
return tokenString, err
}
// Function to create HMAC SHA-3 token with custom payload
func createCustomHMACSHA3Token(secret []byte, payload map[string]interface{}) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(payload))
tokenString, err := token.SignedString(secret)
return tokenString, err
}