Created
February 17, 2022 16:40
-
-
Save AdamJLemmon/f0ea93b75546562e5ab69f88fb53c62c to your computer and use it in GitHub Desktop.
This file contains 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
JWE_ALG: 'A256KW', | |
JWE_ENC: 'A256CBC-HS512', | |
ENCRYPTION_ALGORITHM: "AES-CBC", | |
IV_LENGTH: 16, | |
KEY_LENGTH: 32, | |
W3C_RSA_ALGORITHM: { | |
name: "RSASSA-PKCS1-v1_5", | |
modulusLength: 2048, | |
publicExponent: new Uint8Array([1, 0, 1]), | |
hash: { name: "SHA-256" }, | |
}, | |
KEY_OPS: ["sign", "verify"], | |
const encrypt = async ({ plaintext }) => { | |
const header = { | |
alg: JWE_ALG, | |
enc: JWE_ENC, | |
}; | |
const ivBuffer = window.crypto.getRandomValues(Buffer(IV_LENGTH)); | |
const cryptoKey = await window.crypto.subtle.generateKey({ | |
name: ENCRYPTION_ALGORITHM, | |
length: KEY_LENGTH * 8 | |
}, | |
true, | |
["encrypt", "decrypt"] | |
); | |
const keyBuffer = await window.crypto.subtle.exportKey("raw", cryptoKey); | |
const key = Buffer.from(keyBuffer).toString("base64"); | |
const encrypted = await window.crypto.subtle.encrypt( | |
{ | |
name: ENCRYPTION_ALGORITHM, | |
iv: ivBuffer | |
}, | |
cryptoKey, | |
Buffer.from(plaintext, 'utf8') | |
); | |
const ciphertext = base64url(encrypted); | |
const protectedHeader = base64url(JSON.stringify(header)) | |
const iv = base64url(ivBuffer) | |
const jwe = { | |
protected: protectedHeader, | |
iv, | |
ciphertext | |
}; | |
return { jwe, key } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment