mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-06-12 02:01:38 +00:00
feat: add (rsa)pubKey.encrypt and (rsa)privKey.decrypt
nodeJS only for now
This commit is contained in:
committed by
Jacob Heun
parent
a008bc2fcb
commit
34c5f5c8f0
@ -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) {
|
||||
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user