-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
144 lines (131 loc) · 6.04 KB
/
benchmark.js
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
'use strict'
const Benchmark = require('benchmark')
const jwtUtils = require('./src/index')
const crypto = require('crypto')
const rsaPrivateKeyEncrypted =
'-----BEGIN RSA PRIVATE KEY-----\n' +
'Proc-Type: 4,ENCRYPTED\n' +
'DEK-Info: AES-256-CBC,F8DFFBFC89ACC93CC604E18608B412C4\n' +
'\n' +
'UUGCEuSDg8xiy504WKQ4Fg9Xeen1JYxhhO/3vEjfjSgaSf7e3LJWv6jp5KQTfRD5\n' +
'5lfTI1mHt7NqEziUc5ERq3ueO5x0qtoLNaIsXwXu0/0ex98pjVz6KHnwCB3UqbhZ\n' +
'PJsyfjdA0dyTUjGZZApDRqbK3FedRHL980VknX+r2LMskYEtY0YZiHurRLt8U0Ph\n' +
'q9G2LaNwddZzTTCRVSISl1zYLeMrNk7Tdfkwctlhd1a4RKAe8ymXHinE00nMXbio\n' +
'fJvvCMzPFGvvaRJVKWLyxerZ0Weh0oGXftAgICvOz7AiajQbs7qLBV92I+VPlvQw\n' +
'515VuZYqupTkGoc5SQhBSzMGBS+kJa5/+IrNx/pjEUc9m4sb2Pr9IXbPFrZhtyAR\n' +
'JeYxx2keodFeg/sM0GQWUM0yt807DoQflnLa/hVFI9/OzpfHhe4ToaoLNVCAez2L\n' +
'pAB4HDgu0AbTyt+ffx3yQwfLZHsa90V/+cr1/A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d/+B05Q5wGx8zdBtTJdjQCcWC2pSXFt9wOa6uB1kc/eaebu4DVEV\n' +
'nKPGUgT+uajJh+bznmzDpQilZ9cmYnWa61Ws8jaD+qW8TDXIUCOlRjsCgYEAwQN4\n' +
'+gs5DoFJIUZvVYuHkbkTHgA4Cg1y3F52FZ0QxFYnsPWb2/K4XqPKFsObu6pD/UG6\n' +
'UDA6r1Z6sPRLC+J9lD3UVHIcZY6/cCng0ffjAu9L/MwMybB5bBsKecjiimvezf4+\n' +
'nvOn1MYJlXIxSufViURNi3WF5VTnM3WgSTX7kAsCgYEA0KKVeeDFddBgxdcwROgh\n' +
'hMeLgK1nf5Ra+d4yRQYCpqFfnkaUAaZsH7ymR7XZxlr6WCUp9zbqEskKfv578PEN\n' +
'sdYTOc04cMSnJYIu+XG09slk0tK67+5PxBAdb2rtWSjES69U9jPnRJ5kdTZHzfzW\n' +
'J4XgVlrrS1hpK8eghogXlxsCgYEAr379z7TGo0awfL3zWz/r+3bhSAUA7zr9Nhv5\n' +
'mHtglHSV0FPx7FJMi1i31mzzO/Z7rVnHE0RXB7hFBIKLffnpHxH87u92nGzn6g9Z\n' +
'6PBQjAmS+P97nSOFs+L+am9adGvSgVBsFdELx+5tJmJXKXmUciQVsfkgzr2dHvAT\n' +
'1p1r57MCgYA6W5pXzpzT2plb6XDyOq5A4Mf0P52VEg6gIcRARhvujSrgkzsys78l\n' +
'YCUTCLtRFKPH9Y6XFyO6NE70KI0GQSOm04YyTjBsWRfhziEeny+QMbWiQk3X+nU+\n' +
'1qEwFwH2Z6Z4CsUf6h4rUbUopt0SmryGOhu5cseLVBTTH4XiC3dipA==\n' +
'-----END RSA PRIVATE KEY-----\n'
const rsaPrivateKeyObject = crypto.createPrivateKey(rsaPrivateKey)
const rsaPublicKey =
'-----BEGIN PUBLIC KEY-----\n' +
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqFvlHi5dBWLyDNsspY3c\n' +
'OHdK8UNhuyFJHQJ3qjVQqpc2W7LDNKbsC3YUxShH9j8HyRYz3kbEPvEHT2dV2jqG\n' +
'0jvDRFldqri963FFk+0K8quOAezngbp51/x64w7DQjDp8SIYWkvYFpaAS7AU4AM5\n' +
'AzUsftbsxDo4QpNtadY9Xh1p6vWK4k2dLwQ2Ara8AjuI6C+APkFp3wle/U21YO11\n' +
'FTKgBPGWLLJ/oG7+GrT/UJLgtiPJtNVPciBDDa6XxTeeeVp4op10MZVjDQK8szPA\n' +
'x0tWXhqnAe5EL7ZljQT/BF2+dxT8ZQbp+tMXC0YVMwqjV6DJwt+cDQ7U7cDnknQ0\n' +
'iQIDAQAB\n' +
'-----END PUBLIC KEY-----\n'
const audiences = ['https://host/oauth/token']
const pubKeys = {
'[email protected]': {
'1@RS256': rsaPublicKey,
'1@RS384': rsaPublicKey,
'1@RS512': rsaPublicKey
}
}
const pubKeys2 = {
'[email protected]': {
'1@RS256': rsaPrivateKeyObject,
'1@RS384': rsaPrivateKeyObject,
'1@RS512': rsaPrivateKeyObject
}
}
const unixNow = Math.floor(Date.now() / 1000)
const jwtHeader = {
typ: 'JWT',
alg: 'RS256',
kid: '1'
}
const jwtBody = {
aud: 'https://host/oauth/token',
iss: '[email protected]',
iat: unixNow,
exp: unixNow + 600,
scope: ['http://stuff', 'http://stuff2']
}
let suite = new Benchmark.Suite()
let testToken = jwtUtils.encode(
rsaPrivateKeyEncrypted,
jwtHeader,
jwtBody,
'Qwerty1234'
)
suite
.add('Encode with encrypted RSA key at RS256', () => {
jwtUtils.encode(rsaPrivateKeyEncrypted, jwtHeader, jwtBody, 'Qwerty1234')
})
.add('Encode with unencrypted RSA key at RS256', () => {
jwtUtils.encode(rsaPrivateKey, jwtHeader, jwtBody)
})
.add('Decode encrypted RSA key at RS256', () => {
jwtUtils.decode(testToken, pubKeys, audiences)
})
.add('Encode with unencrypted RSA KeyObject at RS256', () => {
jwtUtils.encode(rsaPrivateKeyObject, jwtHeader, jwtBody)
})
.add('Decode encrypted RSA KeyObject at RS256', () => {
jwtUtils.decode(testToken, pubKeys2, audiences)
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target))
})
// run async
.run({ async: true })