Skip to content

Instantly share code, notes, and snippets.

@cardoso
Last active October 17, 2022 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cardoso/0232aefe0bcc2314a1a7c716fec987b0 to your computer and use it in GitHub Desktop.
Save cardoso/0232aefe0bcc2314a1a7c716fec987b0 to your computer and use it in GitHub Desktop.
export default async (messageJSON, derivedKey) => {
try {
const message = JSON.parse(messageJSON);
const text = message.base64Data;
const initializationVector = new Uint8Array(message.initializationVector).buffer;
const string = atob(text);
const uintArray = new Uint8Array(
[...string].map((char) => char.charCodeAt(0))
);
const algorithm = {
name: "AES-GCM",
iv: initializationVector,
};
const decryptedData = await window.crypto.subtle.decrypt(
algorithm,
derivedKey,
uintArray
);
return new TextDecoder().decode(decryptedData);
} catch (e) {
return `error decrypting message: ${e}`;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment