forked from rt12/node-jwt-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (58 loc) · 2.06 KB
/
index.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
'use strict';
const Benchmark = require('benchmark');
const encode = new Benchmark.Suite;
const decode = new Benchmark.Suite;
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const jsonwebtoken = require('jsonwebtoken');
let token = {};
let payload = {
foo: 'bar',
baz: 'qux',
quux: 'corge',
user_id: 100
};
function loadKey(filename) {
return fs.readFileSync(path.join(__dirname, filename));
}
var algorithms = {
HS256: {
pub_key: 'iOiJiYXIiLCJiYXoiOiJ',
priv_key: 'iOiJiYXIiLCJiYXoiOiJ'
},
RS256: {
// openssl genrsa -out private.pem
pub_key: loadKey('rsa-public.pem'),
// openssl rsa -in rsa-private.pem -outform PEM -pubout -out rsa-public.pem
priv_key: loadKey('rsa-private.pem')
},
ES256: {
// openssl ecparam -genkey -name prime256v1 -noout -out ecdsa-private.pem
priv_key: loadKey('ecdsa-private.pem'),
// openssl ec -in ecdsa-private.pem -pubout -out ecdsa-public.pem
pub_key: loadKey('ecdsa-public.pem')
}
};
for (const [algo, keys] of Object.entries(algorithms)) {
const enc = jsonwebtoken.sign(payload, keys.priv_key, {algorithm: algo});
const dec = jsonwebtoken.verify(enc, keys.pub_key, { algorithm: algo });
assert.equal(dec.user_id, payload.user_id);
console.log(`${algo} token: ${enc}`)
encode.add(algo, () => {
token[algo] = jsonwebtoken.sign(payload, keys.priv_key, { algorithm: algo });
});
decode.add(algo, () => {
jsonwebtoken.verify(token[algo], keys.pub_key, { algorithm: algo });
})
}
console.log("Encoding");
encode
.on('cycle', (event) => console.log(String(event.target)))
.on('complete', function () { console.log('Fastest encoder is ' + this.filter('fastest').map('name') + "\n") })
.run({ 'async': false });
console.log("Decoding");
decode
.on('cycle', (event) => console.log(String(event.target)))
.on('complete', function () { console.log('Fastest decoder is ' + this.filter('fastest').map('name') + "\n"); })
.run({ 'async': false });