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 15, 2018
1 parent ad47845 commit 02753e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 () {
Expand Down
8 changes: 8 additions & 0 deletions src/keys/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 02753e1

Please sign in to comment.