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 authored and jacobheun committed Oct 25, 2019
1 parent a008bc2 commit 34c5f5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/keys/rsa-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class RsaPublicKey {
})
}

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

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

async decrypt (bytes) {
return crypto.decrypt(this._key, bytes)
}

marshal () {
return crypto.utils.jwkToPkcs1(this._key)
}
Expand Down
8 changes: 8 additions & 0 deletions src/keys/rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ exports.hashAndVerify = async function (key, sig, msg) { // eslint-disable-line
const pem = jwkToPem(key)
return verify.verify(pem, sig)
}

exports.encrypt = async function (key, bytes) {
return crypto.publicEncrypt(jwkToPem(key), bytes)
}

exports.decrypt = async function (key, bytes) {
return crypto.privateDecrypt(jwkToPem(key), bytes)
}
7 changes: 7 additions & 0 deletions test/keys/rsa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ describe('RSA', function () {
expect(valid).to.be.eql(true)
})

it('encrypt and decrypt', async () => {
const data = Buffer.from('hello world')
const enc = await key.public.encrypt(data)
const dec = await key.decrypt(enc)
expect(dec).to.be.eql(data)
})

it('fails to verify for different data', async () => {
const data = Buffer.from('hello world')
const sig = await key.sign(data)
Expand Down

0 comments on commit 34c5f5c

Please sign in to comment.