Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: use forge to convert jwk2forge
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 authored and jacobheun committed Oct 25, 2019
1 parent adc6eb4 commit b998f63
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/keys/jwk2pem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict'

const forge = {
util: require('node-forge/lib/util'),
pki: require('node-forge/lib/pki'),
jsbn: require('node-forge/lib/jsbn')
}

function base64urlToBigInteger (str) {
var bytes = forge.util.decode64(
(str + '==='.slice((str.length + 3) % 4))
.replace(/-/g, '+')
.replace(/_/g, '/'))
return new forge.jsbn.BigInteger(forge.util.bytesToHex(bytes), 16)
}

function convert (key, types) {
return types.map(t => base64urlToBigInteger(key[t]))
}

function jwk2priv (key) {
return forge.pki.setRsaPrivatKey(...convert(key, ['n', 'e', 'd', 'p', 'q', 'dP', 'dQ', 'qInv']))
}

function jwk2privPem (key) {
return forge.pki.privateKeyToPem(jwk2priv(key))
}

function jwk2pub (key) {
return forge.pki.setRsaPublicKey(...convert(key, ['n', 'e']))
}

function jwk2pubPem (key) {
return forge.pki.publicKeyToPem(jwk2pub(key))
}

module.exports = {
jwk2pub,
jwk2pubPem,
jwk2priv,
jwk2privPem
}
11 changes: 4 additions & 7 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,16 @@ RSA encryption/decryption for the browser with webcrypto workarround
"bloody dark magic. webcrypto's why."
Explanation:
- Convert JWK to PEM
- Load PEM with nodeForge
- Convert JWK to nodeForge
- Convert msg buffer to nodeForge buffer: ByteBuffer is a "binary-string backed buffer", so let's make our buffer a binary string
- Convert resulting nodeForge buffer to buffer: it returns a binary string, turn that into a uint8array(buffer)
*/

const forge = require('node-forge')
const pki = forge.pki
const jwkToPem = require('pem-jwk').jwk2pem
const { jwk2pub, jwk2priv } = require('./jwk2pem')

function convertKey (key, pub, msg, handle) {
const pem = jwkToPem(key)
const fkey = pki[pub ? 'publicKeyFromPem' : 'privateKeyFromPem'](pem)
const fkey = pub ? jwk2pub(key) : jwk2priv(key)
const fmsg = Buffer.from(msg).toString('binary')
const fomsg = handle(fmsg, fkey)
return Buffer.from(fomsg, 'binary')
Expand Down

0 comments on commit b998f63

Please sign in to comment.