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

Commit

Permalink
feat: browser enc/dec
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 authored and jacobheun committed Oct 25, 2019
1 parent 34c5f5c commit 9f747a1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,47 @@ function derivePublicFromPrivate (jwKey) {
['verify']
)
}

exports.encrypt = async function (key, msg) {
key = Object.assign({}, key)
key.key_ops = ['encrypt']

return webcrypto.subtle.importKey(
'jwk',
key,
{
name: 'RSA-OAEP',
hash: { name: 'SHA-256' }
},
false,
['encrypt']
).then((publicKey) => {
return webcrypto.subtle.encrypt(
{ name: 'RSA-OEAP' },
publicKey,
Uint8Array.from(msg)
)
}).then((enc) => Buffer.from(enc))
}

exports.decrypt = async function (key, msg) {
key = Object.assign({}, key)
key.key_ops = ['decrypt']

return webcrypto.subtle.importKey(
'jwk',
key,
{
name: 'RSA-OAEP',
hash: { name: 'SHA-256' }
},
false,
['decrypt']
).then((privateKey) => {
return webcrypto.subtle.decrypt(
{ name: 'RSA-OAEP' },
privateKey,
Uint8Array.from(msg)
)
}).then((dec) => Buffer.from(dec))
}

0 comments on commit 9f747a1

Please sign in to comment.