Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix benchmarks with regards to jsonwebtoken performance #535

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/sign.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function runSuites() {
compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm])
return
} else {
for (const algorightm of ['HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) {
for (const algorightm of ['HS256', 'HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) {
Gobd marked this conversation as resolved.
Show resolved Hide resolved
process.env.CURRENT_ALGORITHM = algorightm
await compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm])
}
Expand Down
27 changes: 20 additions & 7 deletions benchmarks/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import nodeRsJwt, { Algorithm } from '@node-rs/jsonwebtoken'
import cronometro from 'cronometro'
import { createSecretKey } from 'crypto'
import { readFileSync } from 'fs'
import { mkdir, writeFile } from 'fs/promises'
import { dirname, resolve } from 'path'
Expand Down Expand Up @@ -129,6 +130,16 @@ export function compareDecoding(token, algorithm) {
)
}

function jsonwebtokenKeyFromString(publicKey) {
const jsonwebtokenBuffer = Buffer.from(publicKey)
const jwtSecretDataview = new DataView(
jsonwebtokenBuffer.buffer,
jsonwebtokenBuffer.byteOffset,
jsonwebtokenBuffer.byteLength
)
return createSecretKey(jwtSecretDataview)
}

export async function compareSigning(payload, algorithm, privateKey, publicKey) {
const isEdDSA = algorithm.slice(0, 2) === 'Ed'

Expand All @@ -137,6 +148,7 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
const fastjwtVerify = createVerifier({ key: publicKey })

const josePrivateKey = asKey(privateKey)
const jsonwebtokenKey = jsonwebtokenKeyFromString(publicKey)
const joseOptions = {
algorithm,
iat: false,
Expand All @@ -153,7 +165,7 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
})
const jsonwebtokenGenerated = isEdDSA
? null
: jsonwebtokenSign(payload, privateKey, { algorithm, noTimestamp: true })
: jsonwebtokenSign(payload, jsonwebtokenKey, { algorithm, noTimestamp: true })

log('-------')
log(`Generated ${algorithm} tokens:`)
Expand All @@ -165,7 +177,7 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
log(`@node-rs/jsonwebtoken: ${JSON.stringify(nodeRsGenerated)}`)
log('Generated tokens verification:')
if (!isEdDSA) {
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(jsonwebtokenGenerated, publicKey))}`)
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(jsonwebtokenGenerated, jsonwebtokenKey))}`)
}
log(` jose: ${JSON.stringify(joseVerify(joseGenerated, asKey(publicKey)))}`)
log(` fastjwt: ${JSON.stringify(fastjwtVerify(fastjwtGenerated))}`)
Expand All @@ -184,10 +196,10 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey)
if (!isEdDSA) {
Object.assign(tests, {
[`${algorithm} - jsonwebtoken (sync)`]: function () {
jsonwebtokenSign(payload, privateKey, { algorithm, noTimestamp: true })
jsonwebtokenSign(payload, jsonwebtokenKey, { algorithm, noTimestamp: true })
},
[`${algorithm} - jsonwebtoken (async)`]: function (done) {
jsonwebtokenSign(payload, privateKey, { algorithm, noTimestamp: true }, done)
jsonwebtokenSign(payload, jsonwebtokenKey, { algorithm, noTimestamp: true }, done)
}
})
}
Expand Down Expand Up @@ -222,12 +234,13 @@ export function compareVerifying(token, algorithm, publicKey) {
const fastjwtCachedVerifyAsync = createVerifier({ key: async () => publicKey, cache: true })

const josePublicKey = asKey(publicKey)
const jsonwebtokenKey = jsonwebtokenKeyFromString(publicKey)

if ((process.env.NODE_DEBUG || '').includes('fast-jwt')) {
log('-------')
log(`Verified ${algorithm} tokens:`)
if (!isEdDSA) {
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(token, publicKey))}`)
log(` jsonwebtoken: ${JSON.stringify(jsonwebtokenVerify(token, jsonwebtokenKey))}`)
}
log(` jose: ${JSON.stringify(joseVerify(token, josePublicKey))}`)
log(` fastjwt: ${JSON.stringify(fastjwtVerify(token))}`)
Expand Down Expand Up @@ -256,10 +269,10 @@ export function compareVerifying(token, algorithm, publicKey) {

if (!isEdDSA) {
tests[`${algorithm} - jsonwebtoken (sync)`] = function () {
jsonwebtokenVerify(token, publicKey)
jsonwebtokenVerify(token, jsonwebtokenKey)
}
tests[`${algorithm} - jsonwebtoken (async)`] = function (done) {
jsonwebtokenVerify(token, publicKey, done)
jsonwebtokenVerify(token, jsonwebtokenKey, done)
}
}

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/verify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function runSuites() {
compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm])
return
} else {
for (const algorightm of ['HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) {
for (const algorightm of ['HS256', 'HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) {
Gobd marked this conversation as resolved.
Show resolved Hide resolved
process.env.CURRENT_ALGORITHM = algorightm
await compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm])
}
Expand Down
Loading