namastejs
Version:
A spiritual greeting from your JavaScript code. Because every function deserves a 'Namaste 🙏'
35 lines (30 loc) • 990 B
JavaScript
const crypto = require("crypto");
// AES-256-CBC encryption
function encrypt(text, key) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
let encrypted = cipher.update(text, "utf8", "hex");
encrypted += cipher.final("hex");
return iv.toString("hex") + ":" + encrypted;
}
function decrypt(data, key) {
try {
const [ivHex, encrypted] = data.split(":");
const iv = Buffer.from(ivHex, "hex");
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
} catch {
throw new Error("❌ Wrong master password or corrupted data.");
}
}
// Simple hash for validating master password
function hashPassword(password) {
return crypto.createHash("sha256").update(password).digest("hex");
}
module.exports = {
encrypt,
decrypt,
hashPassword,
};