UNPKG

jwk-rsa-compute-primes

Version:

Compute p, q, dp, dq, and qi from e, d, and n for private JWKs without these parameters

37 lines 1.25 kB
import * as base64url from 'base64url'; import * as BN from 'bn.js'; import { factor } from './factor'; const one = new BN(1); export function computePrimes(jwk) { const required = ['e', 'd', 'n'].filter(r => jwk[r] == null); if (required.length > 0) { throw Error('The following parameters are required: ' + required.join(', ')); } if (jwk.kty.toUpperCase() !== 'RSA') { throw Error('kty MUST be RSA!'); } const d = string2bn(jwk.d); const e = string2bn(jwk.e); const n = string2bn(jwk.n); const { p, q } = factor(e, d, n); const dp = d.mod(p.sub(one)); const dq = d.mod(q.sub(one)); const qi = q.invm(p); return Object.assign({}, jwk, { p: binaryToBase64Url(p), q: binaryToBase64Url(q), dp: binaryToBase64Url(dp), dq: binaryToBase64Url(dq), qi: binaryToBase64Url(qi) }); } function pad(hex) { return hex.length % 2 === 1 ? '0' + hex : hex; } function binaryToBase64Url(bn) { return base64url(pad(bn.toString('hex')), 'hex'); } function base64UrlToBinary(str) { return new BN(base64url.toBuffer(str)); } function string2bn(str) { if (/^[0-9]+$/.test(str)) { return new BN(str, 10); } return base64UrlToBinary(str); } //# sourceMappingURL=index.js.map