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K,KAAK,UAAW,GACxE,EAAe,gBAAgB,KAAK,KAAK,KAAK,SAAS,OAAQ,GAEnE,EAAe,EAAa,GAAK,EAAa,EAC9C,EAAe,EAAa,GAAK,EAAa,EAI9C,OAFwB,OAAO,KAAK,GAAG,KAAgB,KAAgB,EAAoB,SAAS,UAAU,EAAoB,SAAS,UAAU,SAAS,MAGlK,CACA,MAAA,CAAO,GAEH,MAEM,EAFyB,OAAO,KAAK,EAAiB,OAAO,WAEpB,MAAM,KAG/C,EAAsB,OAAO,KAAK,EAAgB,GAAI,OACtD,EAAsB,OAAO,KAAK,EAAgB,GAAI,OAEtD,EAAe,EAAgB,GAC/B,EAAe,EAAgB,GAE/B,EAAmB,OAAO,KAAK,EAAa,UAAU,EAAG,IAAK,OAC9D,EAAsB,EAAa,UAAU,GAAI,EAAa,QAE9D,EAAe,KAAK,MAAM,gBAAgB,CAC5C,GAAI,EACJ,EAAG,GACJ,IAKG,EAAe,gBAAgB,CACjC,GAJqB,OAAO,KAAK,EAAa,UAAU,EAAG,IAAK,OAKhE,EAJwB,EAAa,UAAU,GAAI,EAAa,SAKjE,GAcG,EAAa,CACf,WAbwB,OAAO,iBAAiB,CAChD,IAAK,EAAa,QAClB,OAAQ,MACR,KAAM,UAWN,UARuB,OAAO,gBAAgB,CAC9C,IAAK,EAAa,OAClB,OAAQ,MACR,KAAM,UAMN,SAAU,CACN,QAAS,EAAa,QACtB,OAAQ,EAAa,SAIvB,EAAe,OAAO,KAAK,EAAc,OAE/C,KAAK,OAAO,CACR,KAAM,EACN,KAAM,GAEd,CACA,WAAA,CAAY,GACR,KAAK,QAAK,EAEL,EAGD,KAAK,OAAO,GAFZ,KAAK,WAIb,EAGJ,OAAO,QAAU,CACb","file":"/npm/zest-encryption@1.0.14/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/zest-encryption@1.0.14/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","// Zest - Hybrid encryption powered by RSA and AES\n// This file provides functions for utilising Zest's encryption schemes\n\nconst crypto = require(\"crypto\");\nconst sha256 = require(\"sha256\");\n\nfunction _generateRSAKeypair(modulusLength = 2048) {\n    const { publicKey, privateKey } = crypto.generateKeyPairSync(\"rsa\", {\n        modulusLength,\n    });\n\n    const sk = privateKey.export({\n        type: \"pkcs1\",\n        format: \"pem\",\n    }).toString();\n    \n    const pk = publicKey.export({\n\t\ttype: \"pkcs1\",\n\t\tformat: \"pem\",\n\t}).toString();\n\n    return {\n        privateKey,\n        publicKey,\n        exported: {\n            private: sk,\n            public: pk\n        }\n    }\n}\n\nfunction _generateAESKey() {\n    // 32 bytes == 256 bit\n    return crypto.randomBytes(32);\n}\n\nfunction _encryptWithRSA(plaintext, publicKey) {\n    return crypto.publicEncrypt(\n        {\n            key: publicKey,\n            padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,\n            oaepHash: \"sha256\",\n        },\n        Buffer.from(plaintext)\n    ).toString(\"hex\");\n}\n\nfunction _decryptWithRSA(ciphertext, privateKey) {\n    return crypto.privateDecrypt(\n        {\n            key: privateKey,\n            padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,\n            oaepHash: \"sha256\",\n        },\n        Buffer.from(ciphertext, \"hex\")\n    );\n}\n\nfunction _encryptWithAES(plaintext, key, algorithm = \"aes-256-cbc\") {\n    const iVec = crypto.randomBytes(16);\n\n    const cipher = crypto.createCipheriv(algorithm, key, iVec);\n\n    let encryptedData = cipher.update(plaintext, \"utf-8\", \"hex\");\n\n    encryptedData += cipher.final(\"hex\");\n\n    return {\n        _i: iVec.toString(\"hex\"),\n        e: encryptedData\n    };\n}\n\nfunction _decryptWithAES(ciphertext, key, algorithm = \"aes-256-cbc\") {\n    const decipher = crypto.createDecipheriv(algorithm, key, Buffer.from(ciphertext._i, \"hex\"));\n\n    let decryptedData = decipher.update(ciphertext.e, \"hex\", \"utf-8\");\n\n    decryptedData += decipher.final(\"utf8\");\n\n    return decryptedData;\n}\n\nfunction _signWithRSA(message, privateKey) {\n    const signature = crypto.sign(\"sha256\", Buffer.from(message), {\n        key: privateKey,\n        padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n    });\n\n    return signature.toString(\"hex\");\n}\n\nfunction _verifySignatureWithRSA(signature, message, publicKey) {\n    return crypto.verify(\"sha256\", Buffer.from(message), {\n        key: publicKey,\n        padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n    }, signature);\n}\nclass EncryptionKey {\n    setKey(keyPayload) {\n        this.id = sha256(JSON.stringify(keyPayload._RSA.exported.public) + sha256(keyPayload._AES.toString(\"hex\") + \".mnkr\")).slice(0, 16);\n        this._key = keyPayload;\n    }\n    createKey(RSAModulusLength = 2048) {\n        const RSAKeypair = _generateRSAKeypair(RSAModulusLength);\n        const AESSecret = _generateAESKey();\n\n        this.setKey({\n            _RSA: RSAKeypair,\n            _AES: AESSecret\n        });\n    }\n    encrypt(plaintext) {\n        // Generate random AES secret used for this encryption\n        const tempAESSecret = _generateAESKey();\n\n        // Encrypt temp AES key with the loaded RSA key\n        const tempAESSecretSecured = _encryptWithRSA(tempAESSecret, this._key._RSA.publicKey);\n\n        // Encrypt payload using the newly generated AES key\n        const encryptedPayload = _encryptWithAES(plaintext, tempAESSecret);\n\n        const encryptionPayload = {\n            aesSecure: tempAESSecretSecured,\n            payload: encryptedPayload\n        }\n\n        // Encrypt everything with this Zest EncryptionKey's AES key\n        var finalPayload = _encryptWithAES(JSON.stringify(encryptionPayload), this._key._AES);\n\n        // Combine the initialisation vector and encrypted payload\n        finalPayload = finalPayload._i + finalPayload.e;\n\n        return finalPayload;\n    }\n    decrypt(ciphertext) {\n        // Extract the initialisation vector and encrypted payload\n        const cipherIVec = Buffer.from(ciphertext.substring(0, 32), \"hex\");\n        const cipherPayload = ciphertext.substring(32, ciphertext.length);\n\n        // Decrypt the first-layer with the stored AES key\n        const encryptedPayload = JSON.parse(_decryptWithAES({\n            _i: cipherIVec,\n            e: cipherPayload\n        }, this._key._AES));\n\n        // Decrypt the temp AES key with stored RSA key\n        const tempAESSecret = _decryptWithRSA(encryptedPayload.aesSecure, this._key._RSA.privateKey);\n\n        // Finally, decrypt the original payload\n        const plaintext = _decryptWithAES(encryptedPayload.payload, tempAESSecret);\n\n        return plaintext;\n    }\n    sign(message) {\n        return _signWithRSA(message, this._key._RSA.privateKey);\n    }\n    verify(message, signature, publicKey) {\n        if (!publicKey) publicKey = this._key._RSA.publicKey;\n\n        return _verifySignatureWithRSA(Buffer.from(signature, \"hex\"), message, publicKey);\n    }\n    export() {\n        const rsaSecuredAESSecret = _generateAESKey();\n        const aesSecuredAESSecret = _generateAESKey();\n\n        var encryptedRSA = _encryptWithAES(JSON.stringify(this._key._RSA.exported), rsaSecuredAESSecret);\n        var encryptedAES = _encryptWithAES(this._key._AES.toString(\"hex\"), aesSecuredAESSecret);\n\n        encryptedRSA = encryptedRSA._i + encryptedRSA.e;\n        encryptedAES = encryptedAES._i + encryptedAES.e;\n\n        const exportedKeypair = Buffer.from(`${encryptedRSA}.${encryptedAES}.${rsaSecuredAESSecret.toString(\"hex\")}.${aesSecuredAESSecret.toString(\"hex\")}`).toString(\"hex\");\n\n        return exportedKeypair;\n    }\n    import(exportedKeypair) {\n        // Get the original exported data including the AES secrets\n        const exportedKeypairDecoded = Buffer.from(exportedKeypair, \"hex\").toString();\n\n        const keypairSections = exportedKeypairDecoded.split(\".\");\n\n        // Extract the keypair security keys\n        const rsaSecuredAESSecret = Buffer.from(keypairSections[2], \"hex\");\n        const aesSecuredAESSecret = Buffer.from(keypairSections[3], \"hex\");\n\n        const encryptedRSA = keypairSections[0];\n        const encryptedAES = keypairSections[1];\n\n        const rsaEncCipherIVec = Buffer.from(encryptedRSA.substring(0, 32), \"hex\");\n        const rsaEncCipherPayload = encryptedRSA.substring(32, encryptedRSA.length);\n        \n        const decryptedRSA = JSON.parse(_decryptWithAES({\n            _i: rsaEncCipherIVec,\n            e: rsaEncCipherPayload\n        }, rsaSecuredAESSecret));\n\n        const aesEncCipherIVec = Buffer.from(encryptedAES.substring(0, 32), \"hex\");\n        const aesEncCipherPayload = encryptedAES.substring(32, encryptedAES.length);\n\n        const decryptedAES = _decryptWithAES({\n            _i: aesEncCipherIVec,\n            e: aesEncCipherPayload\n        }, aesSecuredAESSecret);\n\n        const loadedRSAPrivateKey = crypto.createPrivateKey({\n            key: decryptedRSA.private,\n            format: \"pem\",\n            type: \"pkcs1\"\n        });\n\n        const loadedRSAPublicKey = crypto.createPublicKey({\n            key: decryptedRSA.public,\n            format: \"pem\", \n            type: \"pkcs1\"\n        });\n\n        const RSAPayload = {\n            privateKey: loadedRSAPrivateKey,\n            publicKey: loadedRSAPublicKey,\n            exported: {\n                private: decryptedRSA.private,\n                public: decryptedRSA.public\n            }\n        }\n\n        const loadedAESKey = Buffer.from(decryptedAES, \"hex\");\n\n        this.setKey({\n            _RSA: RSAPayload,\n            _AES: loadedAESKey\n        });\n    }\n    constructor(loadKeyObject) {\n        this.id = undefined;\n\n        if (!loadKeyObject) {\n            this.createKey();\n        } else {\n            this.import(loadKeyObject);\n        }\n    }\n}\n\nmodule.exports = {\n    EncryptionKey\n}"]}