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

Commit 55c6908

Browse files
committed
feat: add (rsa)pubKey.encrypt and (rsa)privKey.decrypt
nodeJS only for now
1 parent df23d63 commit 55c6908

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/keys/rsa-browser.js

+8
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ function derivePublicFromPrivate (jwKey) {
115115
['verify']
116116
)
117117
}
118+
119+
exports.encrypt = function (key, bytes, cb) {
120+
return cb(new Error('rsa.pubKey.encrypt() not yet implemented in the browser! (PRs welcome: https://github.com/libp2p/js-libp2p-crypto)'))
121+
}
122+
123+
exports.decrypt = function (key, bytes, cb) {
124+
return cb(new Error('rsa.privKey.decrypt() not yet implemented in the browser! (PRs welcome: https://github.com/libp2p/js-libp2p-crypto)'))
125+
}

src/keys/rsa-class.js

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

33-
encrypt (bytes) {
34-
return this._key.encrypt(bytes, 'RSAES-PKCS1-V1_5')
33+
encrypt (bytes, cb) {
34+
crypto.encrypt(this._key, bytes, cb)
3535
}
3636

3737
equals (key) {
@@ -69,8 +69,8 @@ class RsaPrivateKey {
6969
return new RsaPublicKey(this._publicKey)
7070
}
7171

72-
decrypt (msg, callback) {
73-
crypto.decrypt(this._key, msg, callback)
72+
decrypt (bytes, cb) {
73+
crypto.decrypt(this._key, bytes, cb)
7474
}
7575

7676
marshal () {

src/keys/rsa.js

+24
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,27 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
9898
callback(null, result)
9999
})
100100
}
101+
102+
exports.encrypt = function (key, bytes, cb) {
103+
let res
104+
105+
try {
106+
res = crypto.publicEncrypt(jwkToPem(key), bytes)
107+
} catch (err) {
108+
return cb(err)
109+
}
110+
111+
return cb(null, res)
112+
}
113+
114+
exports.decrypt = function (key, bytes, cb) {
115+
let res
116+
117+
try {
118+
res = crypto.privateDecrypt(jwkToPem(key), bytes)
119+
} catch (err) {
120+
return cb(err)
121+
}
122+
123+
return cb(null, res)
124+
}

0 commit comments

Comments
 (0)