Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e2c63d

Browse files
committedJun 15, 2018
feat: Add (rsa)pubKey.encrypt and (rsa)privKey.decrypt (nodeJS only for now)
1 parent ad47845 commit 3e2c63d

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
 

‎src/keys/rsa-class.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,18 @@ class RsaPublicKey {
3030
})
3131
}
3232

33-
encrypt (bytes) {
34-
return this._key.encrypt(bytes, 'RSAES-PKCS1-V1_5')
33+
encrypt (bytes, cb) {
34+
if (cb) {
35+
let res
36+
try {
37+
res = crypto.encrypt(this._key, bytes)
38+
} catch(e) {
39+
return cb(e)
40+
}
41+
return cb(null, res)
42+
} else {
43+
return crypto.encrypt(this._key, bytes)
44+
}
3545
}
3646

3747
equals (key) {
@@ -69,8 +79,18 @@ class RsaPrivateKey {
6979
return new RsaPublicKey(this._publicKey)
7080
}
7181

72-
decrypt (msg, callback) {
73-
crypto.decrypt(this._key, msg, callback)
82+
encrypt (bytes, cb) {
83+
if (cb) {
84+
let res
85+
try {
86+
res = crypto.decrypt(this._key, bytes)
87+
} catch(e) {
88+
return cb(e)
89+
}
90+
return cb(null, res)
91+
} else {
92+
return crypto.decrypt(this._key, bytes)
93+
}
7494
}
7595

7696
marshal () {

‎src/keys/rsa.js

+8
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
7777
callback(null, result)
7878
})
7979
}
80+
81+
exports.encrypt = function (key, bytes) {
82+
return crypto.publicEncrypt(jwkToPem(key), bytes)
83+
}
84+
85+
exports.decrypt = function (key, bytes) {
86+
return crypto.privateDecrypt(jwkToPem(key), bytes)
87+
}

0 commit comments

Comments
 (0)
This repository has been archived.