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

Commit 1c0eabe

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

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/keys/rsa-browser.js

Lines changed: 8 additions & 0 deletions
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('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('Not yet implemented in the browser! (PRs welcome: https://github.com/libp2p/js-libp2p-crypto)'))
125+
}

src/keys/rsa-class.js

Lines changed: 4 additions & 4 deletions
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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,27 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
7777
callback(null, result)
7878
})
7979
}
80+
81+
exports.encrypt = function (key, bytes, cb) {
82+
let res
83+
84+
try {
85+
res = crypto.publicEncrypt(jwkToPem(key), bytes)
86+
} catch (err) {
87+
return cb(err)
88+
}
89+
90+
return cb(null, res)
91+
}
92+
93+
exports.decrypt = function (key, bytes, cb) {
94+
let res
95+
96+
try {
97+
res = crypto.privateDecrypt(jwkToPem(key), bytes)
98+
} catch (err) {
99+
return cb(err)
100+
}
101+
102+
return cb(null, res)
103+
}

0 commit comments

Comments
 (0)