diff --git a/README.md b/README.md index b4034e2..055ab48 100644 --- a/README.md +++ b/README.md @@ -295,86 +295,126 @@ JWKS is supported via [get-jwks](https://github.com/nearform/get-jwks). Check ou ### Signing ``` -╔══════════════════════════════╤═════════╤═════════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ HS512 - fast-jwt (async) │ 10000 │ 55766.29 op/sec │ ± 2.85 % │ ║ -║ HS512 - jsonwebtoken (async) │ 10000 │ 68764.89 op/sec │ ± 1.25 % │ + 23.31 % ║ -║ HS512 - jsonwebtoken (sync) │ 10000 │ 70191.14 op/sec │ ± 1.84 % │ + 25.87 % ║ -║ HS512 - jose (sync) │ 10000 │ 72844.84 op/sec │ ± 1.72 % │ + 30.63 % ║ -╟──────────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ HS512 - fast-jwt (sync) │ 10000 │ 97602.16 op/sec │ ± 1.83 % │ + 75.02 % ║ -╚══════════════════════════════╧═════════╧═════════════════╧═══════════╧═════════════════════════╝ - -╔══════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ ES512 - fast-jwt (async) │ 1000 │ 419.29 op/sec │ ± 0.34 % │ ║ -║ ES512 - jsonwebtoken (async) │ 1000 │ 440.53 op/sec │ ± 0.26 % │ + 5.07 % ║ -║ ES512 - jsonwebtoken (sync) │ 1000 │ 445.91 op/sec │ ± 0.16 % │ + 6.35 % ║ -║ ES512 - jose (sync) │ 1000 │ 452.01 op/sec │ ± 0.20 % │ + 7.80 % ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ ES512 - fast-jwt (sync) │ 1000 │ 467.54 op/sec │ ± 0.15 % │ + 11.51 % ║ -╚══════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ - -╔══════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ RS512 - fast-jwt (async) │ 1000 │ 196.13 op/sec │ ± 0.28 % │ ║ -║ RS512 - jsonwebtoken (async) │ 1000 │ 200.15 op/sec │ ± 0.23 % │ + 2.05 % ║ -║ RS512 - jsonwebtoken (sync) │ 1000 │ 203.72 op/sec │ ± 0.18 % │ + 3.87 % ║ -║ RS512 - jose (sync) │ 1000 │ 245.89 op/sec │ ± 0.39 % │ + 25.37 % ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ RS512 - fast-jwt (sync) │ 1000 │ 273.31 op/sec │ ± 0.27 % │ + 39.36 % ║ -╚══════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ - -╔══════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ PS512 - jsonwebtoken (sync) │ 1000 │ 194.00 op/sec │ ± 0.27 % │ ║ -║ PS512 - jsonwebtoken (async) │ 1000 │ 202.08 op/sec │ ± 0.21 % │ + 4.17 % ║ -║ PS512 - fast-jwt (async) │ 1000 │ 203.36 op/sec │ ± 0.19 % │ + 4.82 % ║ -║ PS512 - jose (sync) │ 1000 │ 266.54 op/sec │ ± 0.29 % │ + 37.39 % ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ -║ PS512 - fast-jwt (sync) │ 1000 │ 272.11 op/sec │ ± 0.24 % │ + 40.26 % ║ -╚══════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ - -╔══════════════════════════╤═════════╤═════════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ EdDSA - fast-jwt (async) │ 1000 │ 8301.50 op/sec │ ± 0.70 % │ ║ -║ EdDSA - jose (sync) │ 1500 │ 16561.83 op/sec │ ± 0.88 % │ + 99.50 % ║ -╟──────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟──────────────────────────┼─────────┼─────────────────┼───────────┼─────────────────────────╢ -║ EdDSA - fast-jwt (sync) │ 3000 │ 17514.99 op/sec │ ± 0.94 % │ + 110.99 % ║ -╚══════════════════════════╧═════════╧═════════════════╧═══════════╧═════════════════════════╝ +╔═══════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ HS256 - @node-rs/jsonwebtoken (async) │ 1000 │ 74045.03 op/sec │ ± 0.85 % │ ║ +║ HS256 - fast-jwt (async) │ 10000 │ 251884.32 op/sec │ ± 3.64 % │ + 240.18 % ║ +║ HS256 - jsonwebtoken (async) │ 10000 │ 282072.80 op/sec │ ± 1.43 % │ + 280.95 % ║ +║ HS256 - jsonwebtoken (sync) │ 10000 │ 323356.89 op/sec │ ± 1.64 % │ + 336.70 % ║ +║ HS256 - jose (sync) │ 10000 │ 329714.63 op/sec │ ± 2.45 % │ + 345.29 % ║ +║ HS256 - @node-rs/jsonwebtoken (sync) │ 1000 │ 360375.55 op/sec │ ± 0.53 % │ + 386.70 % ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ HS256 - fast-jwt (sync) │ 10000 │ 431801.82 op/sec │ ± 2.52 % │ + 483.16 % ║ +╚═══════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ RS256 - fast-jwt (async) │ 1000 │ 227.29 op/sec │ ± 0.39 % │ ║ +║ RS256 - jsonwebtoken (async) │ 1000 │ 300.52 op/sec │ ± 0.13 % │ + 32.22 % ║ +║ RS256 - fast-jwt (sync) │ 1000 │ 302.37 op/sec │ ± 0.13 % │ + 33.04 % ║ +║ RS256 - jsonwebtoken (sync) │ 1000 │ 302.46 op/sec │ ± 0.13 % │ + 33.07 % ║ +║ RS256 - jose (sync) │ 1000 │ 302.49 op/sec │ ± 0.13 % │ + 33.09 % ║ +║ RS256 - @node-rs/jsonwebtoken (sync) │ 1000 │ 309.88 op/sec │ ± 0.49 % │ + 36.34 % ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ RS256 - @node-rs/jsonwebtoken (async) │ 1000 │ 314.77 op/sec │ ± 0.03 % │ + 38.49 % ║ +╚═══════════════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ HS512 - @node-rs/jsonwebtoken (async) │ 1500 │ 82025.83 op/sec │ ± 0.87 % │ ║ +║ HS512 - fast-jwt (async) │ 10000 │ 247946.30 op/sec │ ± 3.34 % │ + 202.28 % ║ +║ HS512 - jsonwebtoken (async) │ 10000 │ 257520.13 op/sec │ ± 1.61 % │ + 213.95 % ║ +║ HS512 - jose (sync) │ 1500 │ 285727.62 op/sec │ ± 0.93 % │ + 248.34 % ║ +║ HS512 - @node-rs/jsonwebtoken (sync) │ 8000 │ 291709.90 op/sec │ ± 0.97 % │ + 255.63 % ║ +║ HS512 - jsonwebtoken (sync) │ 1000 │ 294312.24 op/sec │ ± 0.72 % │ + 258.80 % ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ HS512 - fast-jwt (sync) │ 10000 │ 369219.80 op/sec │ ± 3.20 % │ + 350.13 % ║ +╚═══════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ ES512 - fast-jwt (async) │ 1500 │ 612.25 op/sec │ ± 0.72 % │ ║ +║ ES512 - jsonwebtoken (async) │ 1000 │ 738.52 op/sec │ ± 0.06 % │ + 20.62 % ║ +║ ES512 - jsonwebtoken (sync) │ 1000 │ 738.90 op/sec │ ± 0.06 % │ + 20.69 % ║ +║ ES512 - jose (sync) │ 1000 │ 740.12 op/sec │ ± 0.04 % │ + 20.89 % ║ +║ ES512 - fast-jwt (sync) │ 1000 │ 743.13 op/sec │ ± 0.05 % │ + 21.38 % ║ +║ ES512 - @node-rs/jsonwebtoken (async) │ 1500 │ 76321.13 op/sec │ ± 0.72 % │ + 12365.75 % ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ ES512 - @node-rs/jsonwebtoken (sync) │ 7000 │ 335482.59 op/sec │ ± 0.94 % │ + 54695.32 % ║ +╚═══════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ RS512 - fast-jwt (async) │ 1000 │ 232.20 op/sec │ ± 0.06 % │ ║ +║ RS512 - jsonwebtoken (async) │ 1000 │ 301.91 op/sec │ ± 0.13 % │ + 30.02 % ║ +║ RS512 - fast-jwt (sync) │ 1000 │ 302.16 op/sec │ ± 0.13 % │ + 30.13 % ║ +║ RS512 - jose (sync) │ 1000 │ 302.18 op/sec │ ± 0.13 % │ + 30.14 % ║ +║ RS512 - jsonwebtoken (sync) │ 1000 │ 302.24 op/sec │ ± 0.13 % │ + 30.16 % ║ +║ RS512 - @node-rs/jsonwebtoken (async) │ 1000 │ 314.80 op/sec │ ± 0.03 % │ + 35.57 % ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ RS512 - @node-rs/jsonwebtoken (sync) │ 1000 │ 316.38 op/sec │ ± 0.04 % │ + 36.25 % ║ +╚═══════════════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤═══════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ PS512 - fast-jwt (async) │ 1000 │ 232.20 op/sec │ ± 0.05 % │ ║ +║ PS512 - fast-jwt (sync) │ 1000 │ 301.36 op/sec │ ± 0.13 % │ + 29.78 % ║ +║ PS512 - jsonwebtoken (sync) │ 1000 │ 301.69 op/sec │ ± 0.13 % │ + 29.92 % ║ +║ PS512 - jsonwebtoken (async) │ 1000 │ 301.94 op/sec │ ± 0.13 % │ + 30.03 % ║ +║ PS512 - jose (sync) │ 1000 │ 302.37 op/sec │ ± 0.13 % │ + 30.22 % ║ +║ PS512 - @node-rs/jsonwebtoken (async) │ 1000 │ 314.08 op/sec │ ± 0.03 % │ + 35.26 % ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼───────────────┼───────────┼─────────────────────────╢ +║ PS512 - @node-rs/jsonwebtoken (sync) │ 1000 │ 315.26 op/sec │ ± 0.04 % │ + 35.77 % ║ +╚═══════════════════════════════════════╧═════════╧═══════════════╧═══════════╧═════════════════════════╝ + +╔═══════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ EdDSA - fast-jwt (async) │ 1000 │ 3693.63 op/sec │ ± 0.20 % │ ║ +║ EdDSA - jose (sync) │ 1500 │ 31921.02 op/sec │ ± 0.74 % │ + 764.22 % ║ +║ EdDSA - fast-jwt (sync) │ 1000 │ 33336.06 op/sec │ ± 0.94 % │ + 802.53 % ║ +║ EdDSA - @node-rs/jsonwebtoken (async) │ 1000 │ 74783.37 op/sec │ ± 0.90 % │ + 1924.66 % ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟───────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ EdDSA - @node-rs/jsonwebtoken (sync) │ 8500 │ 349524.57 op/sec │ ± 0.95 % │ + 9362.91 % ║ +╚═══════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ``` ### Decoding ``` -╔═════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ -║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟─────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ RS512 - jsonwebtoken - complete │ 10000 │ 126201.23 op/sec │ ± 2.84 % │ ║ -║ RS512 - jsonwebtoken │ 10000 │ 143571.03 op/sec │ ± 1.82 % │ + 13.76 % ║ -║ RS512 - jose - complete │ 10000 │ 252738.76 op/sec │ ± 5.62 % │ + 100.27 % ║ -║ RS512 - fast-jwt │ 10000 │ 254921.59 op/sec │ ± 3.39 % │ + 102.00 % ║ -║ RS512 - jose │ 10000 │ 266197.51 op/sec │ ± 4.02 % │ + 110.93 % ║ -╟─────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ -╟─────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ RS512 - fast-jwt - complete │ 10000 │ 284719.82 op/sec │ ± 3.39 % │ + 125.61 % ║ -╚═════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ +╔═════════════════════════════════╤═════════╤═══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────┼─────────┼───────────────────┼───────────┼─────────────────────────╢ +║ RS512 - jsonwebtoken │ 10000 │ 498316.98 op/sec │ ± 1.48 % │ ║ +║ RS512 - jsonwebtoken (complete) │ 10000 │ 507533.27 op/sec │ ± 1.52 % │ + 1.85 % ║ +║ RS512 - fast-jwt (complete) │ 10000 │ 786208.40 op/sec │ ± 2.33 % │ + 57.77 % ║ +║ RS512 - fast-jwt │ 10000 │ 838610.85 op/sec │ ± 1.42 % │ + 68.29 % ║ +║ RS512 - jose │ 1000 │ 957739.73 op/sec │ ± 0.88 % │ + 92.19 % ║ +╟─────────────────────────────────┼─────────┼───────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────┼─────────┼───────────────────┼───────────┼─────────────────────────╢ +║ RS512 - jose (complete) │ 10000 │ 1034359.78 op/sec │ ± 2.44 % │ + 107.57 % ║ +╚═════════════════════════════════╧═════════╧═══════════════════╧═══════════╧═════════════════════════╝ ``` Note that for decoding the algorithm is irrelevant, so only one was measured. @@ -385,74 +425,104 @@ Note that for decoding the algorithm is irrelevant, so only one was measured. ╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ ║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ HS512 - jsonwebtoken (sync) │ 10000 │ 49275.12 op/sec │ ± 1.41 % │ ║ -║ HS512 - fast-jwt (async) │ 10000 │ 51353.81 op/sec │ ± 2.98 % │ + 4.22 % ║ -║ HS512 - jsonwebtoken (async) │ 10000 │ 51610.98 op/sec │ ± 1.51 % │ + 4.74 % ║ -║ HS512 - jose (sync) │ 10000 │ 64280.92 op/sec │ ± 1.73 % │ + 30.45 % ║ -║ HS512 - fast-jwt (sync) │ 10000 │ 75067.57 op/sec │ ± 2.40 % │ + 52.34 % ║ -║ HS512 - fast-jwt (async with cache) │ 10000 │ 175013.21 op/sec │ ± 4.42 % │ + 255.18 % ║ +║ HS256 - fast-jwt (async) │ 1000 │ 220491.56 op/sec │ ± 0.78 % │ ║ +║ HS256 - jsonwebtoken (async) │ 10000 │ 235808.20 op/sec │ ± 1.17 % │ + 6.95 % ║ +║ HS256 - jsonwebtoken (sync) │ 10000 │ 252426.21 op/sec │ ± 1.62 % │ + 14.48 % ║ +║ HS256 - jose (sync) │ 10000 │ 278758.24 op/sec │ ± 2.26 % │ + 26.43 % ║ +║ HS256 - fast-jwt (sync) │ 10000 │ 339960.06 op/sec │ ± 2.86 % │ + 54.18 % ║ +║ HS256 - fast-jwt (async with cache) │ 10000 │ 733415.12 op/sec │ ± 6.67 % │ + 232.63 % ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ ║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ HS512 - fast-jwt (sync with cache) │ 10000 │ 207199.64 op/sec │ ± 3.15 % │ + 320.50 % ║ +║ HS256 - fast-jwt (sync with cache) │ 7000 │ 986486.40 op/sec │ ± 0.96 % │ + 347.40 % ║ ╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ ║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ ES512 - fast-jwt (async) │ 1000 │ 561.01 op/sec │ ± 0.44 % │ ║ -║ ES512 - jsonwebtoken (sync) │ 1000 │ 573.52 op/sec │ ± 0.27 % │ + 2.23 % ║ -║ ES512 - jsonwebtoken (async) │ 1000 │ 573.74 op/sec │ ± 0.26 % │ + 2.27 % ║ -║ ES512 - fast-jwt (sync) │ 1000 │ 597.68 op/sec │ ± 0.30 % │ + 6.54 % ║ -║ ES512 - jose (sync) │ 1000 │ 604.42 op/sec │ ± 0.27 % │ + 7.74 % ║ -║ ES512 - fast-jwt (async with cache) │ 10000 │ 189999.48 op/sec │ ± 4.49 % │ + 33767.60 % ║ +║ RS256 - fast-jwt (async) │ 1000 │ 7060.98 op/sec │ ± 0.38 % │ ║ +║ RS256 - jsonwebtoken (async) │ 1000 │ 16636.99 op/sec │ ± 0.50 % │ + 135.62 % ║ +║ RS256 - jsonwebtoken (sync) │ 1000 │ 16915.78 op/sec │ ± 0.39 % │ + 139.57 % ║ +║ RS256 - jose (sync) │ 1000 │ 17781.70 op/sec │ ± 0.85 % │ + 151.83 % ║ +║ RS256 - fast-jwt (sync) │ 1000 │ 17884.95 op/sec │ ± 0.39 % │ + 153.29 % ║ +║ RS256 - fast-jwt (async with cache) │ 10000 │ 561798.73 op/sec │ ± 5.82 % │ + 7856.39 % ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ ║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ ES512 - fast-jwt (sync with cache) │ 10000 │ 192353.61 op/sec │ ± 4.79 % │ + 34187.22 % ║ +║ RS256 - fast-jwt (sync with cache) │ 10000 │ 730630.87 op/sec │ ± 5.23 % │ + 10247.44 % ║ ╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ ║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ RS512 - jsonwebtoken (async) │ 1500 │ 7551.10 op/sec │ ± 0.92 % │ ║ -║ RS512 - jsonwebtoken (sync) │ 4500 │ 7750.46 op/sec │ ± 0.96 % │ + 2.64 % ║ -║ RS512 - fast-jwt (async) │ 1000 │ 8413.41 op/sec │ ± 0.99 % │ + 11.42 % ║ -║ RS512 - jose (sync) │ 4500 │ 12382.58 op/sec │ ± 0.94 % │ + 63.98 % ║ -║ RS512 - fast-jwt (sync) │ 4500 │ 12665.45 op/sec │ ± 0.90 % │ + 67.73 % ║ -║ RS512 - fast-jwt (sync with cache) │ 10000 │ 145107.65 op/sec │ ± 7.54 % │ + 1821.68 % ║ +║ HS512 - jsonwebtoken (async) │ 1000 │ 220177.36 op/sec │ ± 0.69 % │ ║ +║ HS512 - fast-jwt (async) │ 1000 │ 221759.63 op/sec │ ± 0.47 % │ + 0.72 % ║ +║ HS512 - jsonwebtoken (sync) │ 10000 │ 239279.34 op/sec │ ± 1.26 % │ + 8.68 % ║ +║ HS512 - jose (sync) │ 10000 │ 270725.62 op/sec │ ± 1.69 % │ + 22.96 % ║ +║ HS512 - fast-jwt (sync) │ 10000 │ 321378.46 op/sec │ ± 1.96 % │ + 45.96 % ║ +║ HS512 - fast-jwt (async with cache) │ 10000 │ 661096.40 op/sec │ ± 9.09 % │ + 200.26 % ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ ║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ RS512 - fast-jwt (async with cache) │ 10000 │ 158780.83 op/sec │ ± 3.90 % │ + 2002.75 % ║ +║ HS512 - fast-jwt (sync with cache) │ 10000 │ 781793.65 op/sec │ ± 5.63 % │ + 255.07 % ║ ╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ ║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ PS512 - jsonwebtoken (async) │ 2500 │ 7240.21 op/sec │ ± 0.89 % │ ║ -║ PS512 - jsonwebtoken (sync) │ 2000 │ 7449.38 op/sec │ ± 0.91 % │ + 2.89 % ║ -║ PS512 - fast-jwt (async) │ 1500 │ 8301.99 op/sec │ ± 0.81 % │ + 14.67 % ║ -║ PS512 - jose (sync) │ 4000 │ 11944.57 op/sec │ ± 0.99 % │ + 64.98 % ║ -║ PS512 - fast-jwt (sync) │ 1000 │ 12881.96 op/sec │ ± 0.76 % │ + 77.92 % ║ -║ PS512 - fast-jwt (async with cache) │ 10000 │ 155603.59 op/sec │ ± 4.27 % │ + 2049.16 % ║ +║ ES512 - fast-jwt (async) │ 1000 │ 920.81 op/sec │ ± 0.09 % │ ║ +║ ES512 - jsonwebtoken (async) │ 1000 │ 944.60 op/sec │ ± 0.91 % │ + 2.58 % ║ +║ ES512 - jose (sync) │ 1000 │ 977.66 op/sec │ ± 0.09 % │ + 6.17 % ║ +║ ES512 - jsonwebtoken (sync) │ 1000 │ 980.18 op/sec │ ± 0.08 % │ + 6.45 % ║ +║ ES512 - fast-jwt (sync) │ 1000 │ 983.04 op/sec │ ± 0.08 % │ + 6.76 % ║ +║ ES512 - fast-jwt (async with cache) │ 4000 │ 611845.04 op/sec │ ± 0.96 % │ + 66346.49 % ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ ║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ PS512 - fast-jwt (sync with cache) │ 10000 │ 172097.91 op/sec │ ± 4.58 % │ + 2276.97 % ║ +║ ES512 - fast-jwt (sync with cache) │ 10000 │ 630467.45 op/sec │ ± 11.79 % │ + 68368.89 % ║ ╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ ║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ EdDSA - fast-jwt (async) │ 1000 │ 6370.58 op/sec │ ± 0.59 % │ ║ -║ EdDSA - jose (sync) │ 1000 │ 6538.90 op/sec │ ± 0.83 % │ + 2.64 % ║ -║ EdDSA - fast-jwt (sync) │ 1000 │ 7078.93 op/sec │ ± 0.87 % │ + 11.12 % ║ -║ EdDSA - fast-jwt (async with cache) │ 10000 │ 177457.09 op/sec │ ± 5.36 % │ + 2685.57 % ║ +║ RS512 - fast-jwt (async) │ 1000 │ 6964.24 op/sec │ ± 0.19 % │ ║ +║ RS512 - jsonwebtoken (async) │ 1000 │ 15757.62 op/sec │ ± 0.59 % │ + 126.26 % ║ +║ RS512 - jsonwebtoken (sync) │ 1000 │ 16967.83 op/sec │ ± 0.40 % │ + 143.64 % ║ +║ RS512 - fast-jwt (sync) │ 1000 │ 17691.16 op/sec │ ± 0.20 % │ + 154.03 % ║ +║ RS512 - jose (sync) │ 1000 │ 18512.16 op/sec │ ± 0.17 % │ + 165.82 % ║ +║ RS512 - fast-jwt (async with cache) │ 4000 │ 542514.19 op/sec │ ± 0.93 % │ + 7689.99 % ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ ║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ ╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ -║ EdDSA - fast-jwt (sync with cache) │ 10000 │ 202628.41 op/sec │ ± 3.12 % │ + 3080.69 % ║ +║ RS512 - fast-jwt (sync with cache) │ 10000 │ 551957.22 op/sec │ ± 8.69 % │ + 7825.59 % ║ +╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ + +╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ PS512 - fast-jwt (async) │ 1000 │ 11236.72 op/sec │ ± 0.90 % │ ║ +║ PS512 - jsonwebtoken (sync) │ 1000 │ 16220.82 op/sec │ ± 0.42 % │ + 44.36 % ║ +║ PS512 - jsonwebtoken (async) │ 1000 │ 16952.27 op/sec │ ± 0.31 % │ + 50.86 % ║ +║ PS512 - fast-jwt (sync) │ 1000 │ 17099.44 op/sec │ ± 0.57 % │ + 52.17 % ║ +║ PS512 - jose (sync) │ 1000 │ 17201.23 op/sec │ ± 0.68 % │ + 53.08 % ║ +║ PS512 - fast-jwt (async with cache) │ 10000 │ 509886.18 op/sec │ ± 5.19 % │ + 4437.68 % ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ PS512 - fast-jwt (sync with cache) │ 2500 │ 619182.78 op/sec │ ± 0.99 % │ + 5410.35 % ║ +╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ + +╔═════════════════════════════════════╤═════════╤══════════════════╤═══════════╤═════════════════════════╗ +║ Slower tests │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ EdDSA - fast-jwt (async) │ 1000 │ 7788.29 op/sec │ ± 0.52 % │ ║ +║ EdDSA - jose (sync) │ 1000 │ 12210.20 op/sec │ ± 0.33 % │ + 56.78 % ║ +║ EdDSA - fast-jwt (sync) │ 1000 │ 12262.06 op/sec │ ± 0.70 % │ + 57.44 % ║ +║ EdDSA - fast-jwt (async with cache) │ 4000 │ 611780.66 op/sec │ ± 0.97 % │ + 7755.13 % ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ Fastest test │ Samples │ Result │ Tolerance │ Difference with slowest ║ +╟─────────────────────────────────────┼─────────┼──────────────────┼───────────┼─────────────────────────╢ +║ EdDSA - fast-jwt (sync with cache) │ 10000 │ 643679.66 op/sec │ ± 7.56 % │ + 8164.71 % ║ ╚═════════════════════════════════════╧═════════╧══════════════════╧═══════════╧═════════════════════════╝ ``` diff --git a/benchmarks/auth0.mjs b/benchmarks/auth0.mjs index 163c7a9..54b12b2 100644 --- a/benchmarks/auth0.mjs +++ b/benchmarks/auth0.mjs @@ -6,23 +6,23 @@ import { tokens, privateKeys, publicKeys, compareSigning, compareVerifying, save async function runSuites() { if (!isMainThread) { - const algorightm = process.env.CURRENT_ALGORITHM + const algorithm = process.env.CURRENT_ALGORITHM if (process.env.CURRENT_PHASE === 'sign') { - compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm]) + compareSigning({ a: 1, b: 2, c: 3 }, algorithm, privateKeys[algorithm], publicKeys[algorithm]) } else { - compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm]) + compareVerifying(tokens[algorithm], algorithm, publicKeys[algorithm]) } return } else { - for (const algorightm of ['HS256', 'RS256']) { - process.env.CURRENT_ALGORITHM = algorightm + for (const algorithm of ['HS256', 'RS256']) { + process.env.CURRENT_ALGORITHM = algorithm process.env.CURRENT_PHASE = 'sign' - await compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm]) + await compareSigning({ a: 1, b: 2, c: 3 }, algorithm, privateKeys[algorithm], publicKeys[algorithm]) process.env.CURRENT_PHASE = 'verify' - await compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm]) + await compareVerifying(tokens[algorithm], algorithm, publicKeys[algorithm]) } } diff --git a/benchmarks/sign.mjs b/benchmarks/sign.mjs index 2587803..fa5974e 100644 --- a/benchmarks/sign.mjs +++ b/benchmarks/sign.mjs @@ -1,17 +1,17 @@ 'use strict' import { isMainThread } from 'worker_threads' -import { privateKeys, publicKeys, compareSigning, saveLogs } from './utils.mjs' +import { privateKeys, publicKeys, compareSigning, saveLogs, algorithms } from './utils.mjs' async function runSuites() { if (!isMainThread) { - const algorightm = process.env.CURRENT_ALGORITHM - compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm]) + const algorithm = process.env.CURRENT_ALGORITHM + compareSigning({ a: 1, b: 2, c: 3 }, algorithm, privateKeys[algorithm], publicKeys[algorithm]) return } else { - for (const algorightm of ['HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) { - process.env.CURRENT_ALGORITHM = algorightm - await compareSigning({ a: 1, b: 2, c: 3 }, algorightm, privateKeys[algorightm], publicKeys[algorightm]) + for (const algorithm of algorithms) { + process.env.CURRENT_ALGORITHM = algorithm + await compareSigning({ a: 1, b: 2, c: 3 }, algorithm, privateKeys[algorithm], publicKeys[algorithm]) } } diff --git a/benchmarks/utils.mjs b/benchmarks/utils.mjs index 37cd8ea..d07d276 100644 --- a/benchmarks/utils.mjs +++ b/benchmarks/utils.mjs @@ -2,6 +2,7 @@ import nodeRsJwt, { Algorithm } from '@node-rs/jsonwebtoken' import cronometro from 'cronometro' +import { createSecretKey, createPublicKey, createPrivateKey } from 'crypto' import { readFileSync } from 'fs' import { mkdir, writeFile } from 'fs/promises' import { dirname, resolve } from 'path' @@ -31,6 +32,8 @@ const cronometroOptions = { print: { compare: true, compareMode: 'base' } } +export const algorithms = ['HS256', 'RS256', 'HS512', 'ES512', 'RS512', 'PS512', 'EdDSA'] + export const tokens = { /* Regenerate these tokens after regenerating the keys @@ -47,7 +50,7 @@ export const tokens = { RS512: 'eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJhIjoxLCJiIjoyLCJjIjozLCJpYXQiOjE1ODA5MTUyOTZ9.JEHZDpA99hww-5-PKcCvwialNy1QcyDSJXJ-qvzV0fU56NXPxZRfn2rEwdX4g-8N-oXLsjCjNZzr0Hl39FXSK0ke_vnzwPW6D4r5mVL6Ak0K-jMgNFidxElM7PRg2XE7N72dI5ClQJCJMqex7NYmIN4OUj9psx1NDv8bM_Oj44kXyI6ozYrkV-6tvowLXUX9BOZH55jF3aAA1DLI4rVBKc_JYqiHf376xu6zvFxzZ8XP3-S-dTR7OBRZLe5_Y6YJweWiL2n0lRkEjYrpK3Ht9MlfaCmW2_KMH0DpUKVS6nnKmzqGjdutnzP6PYXZsJikCQOrIcPW97LdQWLLRIptSpn7YHH1xbNbq__kryaggwpKuNd6qhdXqREEhpaYl3Xc4yjGnBR0zMq7J-GxDo7mSujMMFmb4ZQLQWwANCEHSfqYIJYp7Upc1Rd__lo56Mr1Bd9claZPBNgKqAvhlmjZT9lELA-eyEzhH_yrcVzMcVpCC_oVIzvpiDQ0jgOLcDIz0q8uzoSCks3M0IK3flefopY8g1e-OExqBoYrfoktFciabLfTM5g-rIpC0CrrDN-TfXLqZAPkIv7suGBmQn9-HbcFL8eZEIg-q6D3o7EAfCO7ki9ncrm47C2y5SX3zDOjG37_5pN2JGWztfSFRQ-YbbEV13-TuKvRG3HLJjJQk6g', PS512: - 'eyJhbGciOiJQUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJhIjoxLCJiIjoyLCJjIjozLCJpYXQiOjE1ODA5MTUyOTd9.IfPgE2aPz9dBzvb8hvn_RjBvdq5jLgfzRw-lM5Q2Ah67NYuRYjCzpvywJASY-0Y-tvk94kwwmDUpqR7nPPPlcv9o_OYQVGhPnndh6iMww0D-MGZwP0sqIauu6NgUsCY4rFG2_K8lxCYbdNThJJVDDN8v_VmKrK3qh7DJC89PE-ZbIMr4N3AuLww-vPgB-9hFmuVnjgO43scZb8C_SaA1HuSbw_SU6OWguAlaTP3zUKlyQmTvvx843J8byi5jDcqK4Rtah5gRaO8U1l6bzmVCKs8Fh3tv7A7GWs-eFukFu5dUr-Ig0iyIhPSAVOjnk0dEZ4s5YI1XaPrnm3wAKV9fyzSri2LaaElp_8Cy7xyJNDPgWSTUmm4BGU7m5x_zwamRbQ1zI7p-YxftwkL4Jl8VD1km7CP9T_6cOEt6RzSTNbTgkk3XhcqYZ0oTgAJ4nXa4j47-7E0n5drtM1xoYeWBaWQvXPdMwGTAwXMx33B1WOm80B7Ncn6AzZKtJtEYalFEKntNfJhWi5x9nZNc4-3cja4o1appVm5PWSOtV4mkLsrLL1T0x1c9ymyF_XtYkRAuxdOKaRs2N5YxHcikgX-iifI1Ih4l79BrWAgyioGjMTU78VMV_gLRQ1VLexmgYJLKL2fBUrBR-k8fI4VwbKEtEZF3wBINWBAp4-urFV3QVig', + 'eyJhbGciOiJQUzUxMiIsInR5cCI6IkpXVCIsImtpZCI6IjEyMyJ9.eyJhIjoxLCJiIjoyLCJjIjozLCJpYXQiOjE3MzY3NzM0OTd9.KgJrc8DZI1XPOH3qh3xUn1_9Fpl-Js5OsBZCoPJJ9agfIPC1xkKGH7XQZId6P3_JWObZLmq2-17wB9-wUNQJA3wRH3wZpmWVrdY7qJDmAQT1cSqXHu4xfZaclG3jiawweLeGmAOcwnZFv0xsFaT-Pa_McTTl-BqNN20Kzs5GnLSCb8upy1xi73_hVwIqo9l3l02xFxcJp2UpELXLe6hzfsUZ43OEN-L4aYtLPKHW1KGGMz3TmNRfS0cjvC3swRr3B-jCdfcnofxHR2NBtPw0kkMePqWyr8k6O85vFYPlDyoNjbEIVtTwr2umJn_BA5IgDphj_Xa8YffHTg02-EUX9kKGhDhMKhx7Yeh_2wOyySPCG1gt1A5CxjndSvs14jCoA3F5GSPCWn0x2EG-mtUsERHXyNPuGvhlqrVAl5KYcGf8NgbDeVtSTbgy3ykFew76__d_P6j9GlIfvZ4rFVuAEvL4nhcBB7Tpp7Phtl72k19TlfeHe2REZkRMQ2Dc9FHH7gM-ey3A9m0ZT-IUFZkJgCVOjbgG3Vm7cDBmvn2oIGtsG6fkdHTHqWfUOcgkkJFEd28_75rnL-I_KhsXU8DCCVHedql-uMErwPiBThS9NqTv2ZqSVdEV4es0mK03X1IV3U6oXpP6sxOqE6FTrzDstEYTiZhQq8wDnWznlxaNWA8', EdDSA: 'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCIsImt0eSI6Ik9LUCIsImNydiI6IkVkMjU1MTkiLCJraWQiOiIxMjMifQ.eyJhIjoxLCJiIjoyLCJjIjozLCJpYXQiOjE1ODY3ODQ3ODF9.PDIxWOWhAHr-7Zy7UC8W4pRuk7dMYTD8xy0DR0N102P0pXK6U4r6THHe66muTdSM3qiDHZnync1WQp-10QFLCQ' } @@ -129,6 +132,28 @@ export function compareDecoding(token, algorithm) { ) } +function jsonwebtokenPrivateKeyFromString(privateKey) { + return jsonwebtokenKeyFromString(privateKey, createPrivateKey) +} + +function jsonwebtokenSecretKeyFromString(publicKey) { + return jsonwebtokenKeyFromString(publicKey, createSecretKey) +} + +function jsonwebtokenPublicKeyFromString(publicKey) { + return jsonwebtokenKeyFromString(publicKey, createPublicKey) +} + +function jsonwebtokenKeyFromString(key, keyFunc) { + const jsonwebtokenBuffer = Buffer.from(key) + const jwtSecretDataview = new DataView( + jsonwebtokenBuffer.buffer, + jsonwebtokenBuffer.byteOffset, + jsonwebtokenBuffer.byteLength + ) + return keyFunc(jwtSecretDataview) +} + export async function compareSigning(payload, algorithm, privateKey, publicKey) { const isEdDSA = algorithm.slice(0, 2) === 'Ed' @@ -137,6 +162,9 @@ export async function compareSigning(payload, algorithm, privateKey, publicKey) const fastjwtVerify = createVerifier({ key: publicKey }) const josePrivateKey = asKey(privateKey) + const jsonwebtokenKey = /^(?:RS|PS|ES)/.test(algorithm) + ? jsonwebtokenPrivateKeyFromString(privateKey) + : jsonwebtokenSecretKeyFromString(publicKey) const joseOptions = { algorithm, iat: false, @@ -153,7 +181,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:`) @@ -165,7 +193,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))}`) @@ -184,10 +212,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) } }) } @@ -222,12 +250,15 @@ export function compareVerifying(token, algorithm, publicKey) { const fastjwtCachedVerifyAsync = createVerifier({ key: async () => publicKey, cache: true }) const josePublicKey = asKey(publicKey) + const jsonwebtokenKey = /^(?:RS|PS|ES)/.test(algorithm) + ? jsonwebtokenPublicKeyFromString(publicKey) + : jsonwebtokenSecretKeyFromString(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))}`) @@ -256,10 +287,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) } } diff --git a/benchmarks/verify.mjs b/benchmarks/verify.mjs index 4ee7c64..90eec3b 100644 --- a/benchmarks/verify.mjs +++ b/benchmarks/verify.mjs @@ -1,17 +1,17 @@ 'use strict' import { isMainThread } from 'worker_threads' -import { tokens, publicKeys, compareVerifying, saveLogs } from './utils.mjs' +import { tokens, publicKeys, compareVerifying, saveLogs, algorithms } from './utils.mjs' async function runSuites() { if (!isMainThread) { - const algorightm = process.env.CURRENT_ALGORITHM - compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm]) + const algorithm = process.env.CURRENT_ALGORITHM + compareVerifying(tokens[algorithm], algorithm, publicKeys[algorithm]) return } else { - for (const algorightm of ['HS512', 'ES512', 'RS512', 'PS512', 'EdDSA']) { - process.env.CURRENT_ALGORITHM = algorightm - await compareVerifying(tokens[algorightm], algorightm, publicKeys[algorightm]) + for (const algorithm of algorithms) { + process.env.CURRENT_ALGORITHM = algorithm + await compareVerifying(tokens[algorithm], algorithm, publicKeys[algorithm]) } }