Last active
April 9, 2024 07:54
-
-
Save ravshansbox/f91f4b3952e82320d850f4092d844368 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const name = 'RSA-OAEP'; | |
const hash = 'SHA-256'; | |
const modulusLength = 2048; | |
const publicExponent = new Uint8Array([1, 0, 1]); | |
const format = 'spki'; | |
const message = 'hello'; | |
// browser | |
const { privateKey, publicKey } = await crypto.subtle.generateKey( | |
{ name, hash, modulusLength, publicExponent }, | |
true, | |
['decrypt'] | |
); | |
// browser | |
const exportedPublicKey = await crypto.subtle.exportKey(format, publicKey); | |
// node | |
const importedPublicKey = await crypto.subtle.importKey( | |
format, | |
exportedPublicKey, | |
{ name, hash }, | |
false, | |
['encrypt'] | |
); | |
// node | |
const encoded = new TextEncoder().encode(message).buffer; | |
const encrypted = await crypto.subtle.encrypt( | |
{ name }, | |
importedPublicKey, | |
encoded | |
); | |
// browser | |
const decrypted = await crypto.subtle.decrypt({ name }, privateKey, encrypted); | |
const decoded = new TextDecoder().decode(decrypted); | |
console.log(decoded); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment