Created
November 2, 2023 20:55
-
-
Save vstyler96/ccaa7c7a4a410abd47efd59bd1bc4281 to your computer and use it in GitHub Desktop.
Cryptographic Helper for Vue 3 using Web Crypto API
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
| /* eslint-disable */ | |
| export function useCrypto() { | |
| const { crypto: { subtle: KeyLoader } } = window; | |
| /** | |
| * Create RSA Key Pair. | |
| * | |
| * @returns {Object} | |
| */ | |
| async function createKeyPair() { | |
| const keyPair = await KeyLoader.generateKey( | |
| { | |
| name: "RSA-OAEP", | |
| modulusLength: 4096, | |
| publicExponent: new Uint8Array([0x01, 0x00, 0x01]), | |
| hash: { name: "SHA-256" }, | |
| }, | |
| true, | |
| ["encrypt", "decrypt"], | |
| ); | |
| let [ | |
| exportedPrivate, | |
| exportedPublic, | |
| ] = [ | |
| Buffer.from(await KeyLoader.exportKey("pkcs8", keyPair.privateKey)).toString("base64"), | |
| Buffer.from(await KeyLoader.exportKey("spki", keyPair.publicKey)).toString("base64"), | |
| ]; | |
| exportedPublic = `-----BEGIN PUBLIC KEY-----\n${exportedPublic.match(/.{1,64}/g).join("\n")}\n-----END PUBLIC KEY-----`; | |
| exportedPrivate = `-----BEGIN PRIVATE KEY-----\n${exportedPrivate.match(/.{1,64}/g).join("\n")}\n-----END PRIVATE KEY-----`; | |
| return { privateKey: exportedPrivate, publicKey: exportedPublic }; | |
| } | |
| /** | |
| * Decrypt using RSA pem. | |
| * | |
| * @param {String} data | |
| * @param {String} key | |
| * | |
| * @returns {String} | |
| */ | |
| async function decryptRSA(data, key) { | |
| const parsedKey = key.replace(/-+[^-]+-+/g, "").replace("\n", "").replace("\r", ""); | |
| const importedKey = await KeyLoader.importKey( | |
| "pkcs8", | |
| Buffer.from(parsedKey, "base64"), | |
| { | |
| name: "RSA-OAEP", | |
| hash: "SHA-256", | |
| }, | |
| true, | |
| ["decrypt"], | |
| ); | |
| const decrypted = await KeyLoader.decrypt( | |
| { | |
| name: "RSA-OAEP", | |
| }, | |
| importedKey, | |
| Buffer.from(data, "base64"), | |
| ); | |
| return Buffer.from(decrypted).toString("ascii"); | |
| } | |
| /** | |
| * Encrypt using RSA pem. | |
| * | |
| * @param {String} data | |
| * @param {String} key | |
| * | |
| * @returns {String} | |
| */ | |
| async function encryptRSA(data, key) { | |
| const parsedKey = key.replace(/-+[^-]+-+/g, "").replace("\n", "").replace("\r", ""); | |
| const importedKey = await KeyLoader.importKey( | |
| "spki", | |
| Buffer.from(parsedKey, "base64"), | |
| { | |
| name: "RSA-OAEP", | |
| hash: "SHA-256", | |
| }, | |
| true, | |
| ["encrypt"], | |
| ); | |
| const encrypted = await KeyLoader.encrypt( | |
| { | |
| name: "RSA-OAEP", | |
| }, | |
| importedKey, | |
| Buffer.from(data, "ascii"), | |
| ); | |
| return Buffer.from(encrypted).toString("base64"); | |
| } | |
| /** | |
| * This method will encrypt data using AES methid. | |
| * it's demanded to pass the key and key options. | |
| * | |
| * @var {String} data | |
| * @var {String} key | |
| * @var {Object} iv | |
| * | |
| * @returns {String} | |
| */ | |
| async function encrypt(data, key, iv) { | |
| const AES = await KeyLoader.importKey("raw", key, "AES-CBC", false, ["encrypt"]); | |
| const encrypted = await KeyLoader.encrypt({ name: "AES-CBC", iv }, AES, Buffer.from(data, "ascii")); | |
| return Buffer.from(encrypted).toString("base64"); | |
| } | |
| /** | |
| * This method will decrypt data, it's demanded to pass the key and-- key options. | |
| * | |
| * @var {String} data | |
| * @var {String} key | |
| * @var {Object} iv | |
| * | |
| * @returns {String} | |
| */ | |
| async function decrypt(data, key, iv) { | |
| const AES = await KeyLoader.importKey("raw", key, "AES-CBC", false, ["decrypt"]); | |
| const decrypted = await KeyLoader.decrypt({ name: "AES-CBC", iv }, AES, Buffer.from(data, "base64")); | |
| return Buffer.from(decrypted).toString("ascii"); | |
| } | |
| return { | |
| createKeyPair, | |
| decryptRSA, | |
| encryptRSA, | |
| encrypt, | |
| decrypt, | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment