From 02753e1ea612745ea62beaa776792360a49c62ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= Date: Fri, 15 Jun 2018 11:59:54 +0200 Subject: [PATCH] feat: add (rsa)pubKey.encrypt and (rsa)privKey.decrypt nodeJS only for now --- src/keys/rsa-class.js | 28 ++++++++++++++++++++++++---- src/keys/rsa.js | 8 ++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/keys/rsa-class.js b/src/keys/rsa-class.js index afaea88e..7be391ae 100644 --- a/src/keys/rsa-class.js +++ b/src/keys/rsa-class.js @@ -30,8 +30,18 @@ class RsaPublicKey { }) } - encrypt (bytes) { - return this._key.encrypt(bytes, 'RSAES-PKCS1-V1_5') + encrypt (bytes, cb) { + if (cb) { + let res + try { + res = crypto.encrypt(this._key, bytes) + } catch(e) { + return cb(e) + } + return cb(null, res) + } else { + return crypto.encrypt(this._key, bytes) + } } equals (key) { @@ -69,8 +79,18 @@ class RsaPrivateKey { return new RsaPublicKey(this._publicKey) } - decrypt (msg, callback) { - crypto.decrypt(this._key, msg, callback) + decrypt (bytes, cb) { + if (cb) { + let res + try { + res = crypto.decrypt(this._key, bytes) + } catch(e) { + return cb(e) + } + return cb(null, res) + } else { + return crypto.decrypt(this._key, bytes) + } } marshal () { diff --git a/src/keys/rsa.js b/src/keys/rsa.js index 2da2678b..c8acedeb 100644 --- a/src/keys/rsa.js +++ b/src/keys/rsa.js @@ -77,3 +77,11 @@ exports.hashAndVerify = function (key, sig, msg, callback) { callback(null, result) }) } + +exports.encrypt = function (key, bytes) { + return crypto.publicEncrypt(jwkToPem(key), bytes) +} + +exports.decrypt = function (key, bytes) { + return crypto.privateDecrypt(jwkToPem(key), bytes) +}