|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import * as asn1js from 'asn1js' |
| 3 | +import Benchmark from 'benchmark' |
| 4 | +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' |
| 5 | +import { encodeSequence, encodeInteger } from '../../dist/src/keys/rsa/der.js' |
| 6 | +import { RSA_KEY_512_BITS } from '../../dist/test/fixtures/rsa.js' |
| 7 | + |
| 8 | +// results |
| 9 | +// % node ./benchmark/der/encode-jwk-to-pkcs1.js |
| 10 | +// asn1js x 21,453 ops/sec ±0.54% (92 runs sampled) |
| 11 | +// encodeSequence x 65,991 ops/sec ±0.36% (98 runs sampled) |
| 12 | + |
| 13 | +const jwk = RSA_KEY_512_BITS.privateKey |
| 14 | + |
| 15 | +const suite = new Benchmark.Suite('encode JWK private key to PKCS#1') |
| 16 | + |
| 17 | +suite.add('asn1js', async () => { |
| 18 | + const root = new asn1js.Sequence({ |
| 19 | + value: [ |
| 20 | + new asn1js.Integer({ value: 0 }), |
| 21 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.n, 'base64url'))), |
| 22 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.e, 'base64url'))), |
| 23 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.d, 'base64url'))), |
| 24 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.p, 'base64url'))), |
| 25 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.q, 'base64url'))), |
| 26 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.dp, 'base64url'))), |
| 27 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.dq, 'base64url'))), |
| 28 | + asn1js.Integer.fromBigInt(bufToBn(uint8ArrayFromString(jwk.qi, 'base64url'))) |
| 29 | + ] |
| 30 | + }) |
| 31 | + |
| 32 | + const der = root.toBER() |
| 33 | + |
| 34 | + return new Uint8Array(der, 0, der.byteLength) |
| 35 | +}) |
| 36 | + |
| 37 | +suite.add('encodeSequence', async () => { |
| 38 | + return encodeSequence([ |
| 39 | + encodeInteger(Uint8Array.from([0])), |
| 40 | + encodeInteger(uint8ArrayFromString(jwk.n, 'base64url')), |
| 41 | + encodeInteger(uint8ArrayFromString(jwk.e, 'base64url')), |
| 42 | + encodeInteger(uint8ArrayFromString(jwk.d, 'base64url')), |
| 43 | + encodeInteger(uint8ArrayFromString(jwk.p, 'base64url')), |
| 44 | + encodeInteger(uint8ArrayFromString(jwk.q, 'base64url')), |
| 45 | + encodeInteger(uint8ArrayFromString(jwk.dp, 'base64url')), |
| 46 | + encodeInteger(uint8ArrayFromString(jwk.dq, 'base64url')), |
| 47 | + encodeInteger(uint8ArrayFromString(jwk.qi, 'base64url')) |
| 48 | + ]).subarray() |
| 49 | +}) |
| 50 | + |
| 51 | +suite |
| 52 | + .on('cycle', (event) => console.log(String(event.target))) |
| 53 | + .run({ async: true }) |
| 54 | + |
| 55 | +function bufToBn (u8) { |
| 56 | + const hex = [] |
| 57 | + |
| 58 | + u8.forEach(function (i) { |
| 59 | + let h = i.toString(16) |
| 60 | + |
| 61 | + if (h.length % 2 > 0) { |
| 62 | + h = `0${h}` |
| 63 | + } |
| 64 | + |
| 65 | + hex.push(h) |
| 66 | + }) |
| 67 | + |
| 68 | + return BigInt('0x' + hex.join('')) |
| 69 | +} |
0 commit comments