Skip to content

Instantly share code, notes, and snippets.

@ravshansbox
Last active April 9, 2024 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravshansbox/f91f4b3952e82320d850f4092d844368 to your computer and use it in GitHub Desktop.
Save ravshansbox/f91f4b3952e82320d850f4092d844368 to your computer and use it in GitHub Desktop.
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