This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add (rsa)pubKey.encrypt and (rsa)privKey.decrypt nodeJS only for now * feat: browser enc/dec * fix: browser rsa enc/dec * refactor: cleanup * fix: lint * fix: use direct buffers instead of converting to hex * fix: padding error * test: add interop test * feat: use forge to convert jwk2forge * fix: jwk var naming * fix: missing cbwrap
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
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
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
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.setRsaPrivateKey(...convert(key, ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi'])) | ||
} | ||
|
||
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 | ||
} |
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
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
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
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