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

Commit

Permalink
feat: add (rsa)pubKey.encrypt and (rsa)privKey.decrypt
Browse files Browse the repository at this point in the history
nodeJS only for now
  • Loading branch information
mkg20001 committed Jun 17, 2018
1 parent ad47845 commit 61149b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ function derivePublicFromPrivate (jwKey) {
['verify']
)
}

exports.encrypt = function (key, bytes, cb) {
return cb(new Error('rsa.pubKey.encrypt() not yet implemented in the browser! (PRs welcome: https://github.com/libp2p/js-libp2p-crypto)'))
}

exports.decrypt = function (key, bytes, cb) {
return cb(new Error('rsa.privKey.decrypt() not yet implemented in the browser! (PRs welcome: https://github.com/libp2p/js-libp2p-crypto)'))
}
8 changes: 4 additions & 4 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class RsaPublicKey {
})
}

encrypt (bytes) {
return this._key.encrypt(bytes, 'RSAES-PKCS1-V1_5')
encrypt (bytes, cb) {
crypto.encrypt(this._key, bytes, cb)
}

equals (key) {
Expand Down Expand Up @@ -69,8 +69,8 @@ class RsaPrivateKey {
return new RsaPublicKey(this._publicKey)
}

decrypt (msg, callback) {
crypto.decrypt(this._key, msg, callback)
decrypt (bytes, cb) {
crypto.decrypt(this._key, bytes, cb)
}

marshal () {
Expand Down
24 changes: 24 additions & 0 deletions src/keys/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,27 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
callback(null, result)
})
}

exports.encrypt = function (key, bytes, cb) {
let res

try {
res = crypto.publicEncrypt(jwkToPem(key), bytes)
} catch (err) {
return cb(err)
}

return cb(null, res)
}

exports.decrypt = function (key, bytes, cb) {
let res

try {
res = crypto.privateDecrypt(jwkToPem(key), bytes)
} catch (err) {
return cb(err)
}

return cb(null, res)
}

0 comments on commit 61149b5

Please sign in to comment.