mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 08:41:05 +00:00
feat: browser enc/dec
This commit is contained in:
parent
34c5f5c8f0
commit
9f747a173f
@ -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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user