vormiaqueryjs
Version:
Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application
43 lines (42 loc) • 1.23 kB
JavaScript
import CryptoJS from "crypto-js";
let crypto;
try {
crypto = require("crypto");
} catch {
crypto = null;
}
const PUBLIC_KEY = process.env.VORMIA_PUBLIC_KEY;
const PRIVATE_KEY = process.env.VORMIA_PRIVATE_KEY;
const encryptData = (data, key) => {
try {
const jsonString = JSON.stringify(data);
return CryptoJS.AES.encrypt(jsonString, key).toString();
} catch {
return null;
}
};
function encryptWithPublicKey(data, publicKey = PUBLIC_KEY) {
if (!crypto || !publicKey)
throw new Error("RSA encryption requires Node.js and a public key");
const buffer = Buffer.from(
typeof data === "string" ? data : JSON.stringify(data)
);
return crypto.publicEncrypt(publicKey, buffer).toString("base64");
}
function decryptWithPrivateKey(encrypted, privateKey = PRIVATE_KEY) {
if (!crypto || !privateKey)
throw new Error("RSA decryption requires Node.js and a private key");
const buffer = Buffer.from(encrypted, "base64");
const decrypted = crypto.privateDecrypt(privateKey, buffer).toString("utf8");
try {
return JSON.parse(decrypted);
} catch {
return decrypted;
}
}
export {
decryptWithPrivateKey,
encryptData,
encryptWithPublicKey
};
//# sourceMappingURL=encryption.mjs.map