Skip to content

Instantly share code, notes, and snippets.

@ronanyeah
Created August 15, 2019 06:20
Show Gist options
  • Select an option

  • Save ronanyeah/4ec99ba3cf5d8c1f5db2cbb9edf19ff2 to your computer and use it in GitHub Desktop.

Select an option

Save ronanyeah/4ec99ba3cf5d8c1f5db2cbb9edf19ff2 to your computer and use it in GitHub Desktop.
const { publicKey, privateKey } = await crypto.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
const encryptedText = await crypto
.encrypt(
{ name: "RSA-OAEP" },
publicKey,
stringToArrayBuffer(encodeURI("plaintext"))
)
.then(arrayBufferToString);
const res = await crypto
.decrypt(
{ name: "RSA-OAEP" },
privateKey,
stringToArrayBuffer(encryptedText)
)
.then(arrayBufferToString)
.then(decodeURI);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment