-
-
Save satazor/1746e7f2ce2f5363b1f21d403b790480 to your computer and use it in GitHub Desktop.
Node.js - JWT sign & verify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt from "jsonwebtoken"; | |
import { readFileSync } from "node:fs"; | |
import { createSecretKey } from 'node:crypto'; | |
const emails = JSON.parse( | |
readFileSync( | |
"/Users/mayankc/Work/source/perfComparisons/testdata/emails.json", | |
), | |
); | |
let i = 1, idx = 0; | |
const jwtSecret = createSecretKey(process.env.JWT_SECRET); | |
const numIterations = parseInt(process.argv[2]); | |
let startTS; | |
while (true) { | |
if (i === 10000) { | |
startTS = Date.now(); | |
} | |
const email = emails[idx]; | |
const currTS = Date.now(); | |
const token = jwt.sign({ | |
sub: email, | |
iat: currTS, | |
exp: currTS + 2 * 60 * 60 * 1000, | |
}, jwtSecret); | |
const claims = jwt.verify(token, jwtSecret); | |
if (claims.sub !== email) { | |
process.exit(1); | |
} | |
if (idx++ >= emails.length) { | |
idx = 0; | |
} | |
if (i++ > numIterations) { | |
break; | |
} | |
} | |
const endTS = Date.now(); | |
const diff = endTS - startTS; | |
console.log(diff); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment