UNPKG

emotionctl

Version:

A secure terminal-based journaling system designed as a safe space for developers going through heartbreak, breakups, or betrayal

144 lines 5.08 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CryptoService = void 0; const CryptoJS = __importStar(require("crypto-js")); class CryptoService { /** * Generates a cryptographic key from password using PBKDF2 */ static deriveKey(password, salt) { return CryptoJS.PBKDF2(password, salt, { keySize: this.KEY_SIZE / 32, iterations: this.ITERATIONS, hasher: CryptoJS.algo.SHA256 }); } /** * Encrypts data using AES-256-CBC */ static encrypt(data, password) { try { // Generate random salt and IV const salt = CryptoJS.lib.WordArray.random(this.SALT_SIZE / 8); const iv = CryptoJS.lib.WordArray.random(this.IV_SIZE / 8); // Derive key from password const key = this.deriveKey(password, salt); // Encrypt the data const encrypted = this.ALGORITHM.encrypt(data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return { data: encrypted.toString(), iv: iv.toString(), salt: salt.toString() }; } catch (error) { throw new Error(`Encryption failed: ${error}`); } } /** * Decrypts data using AES-256-CBC */ static decrypt(encryptedData, password) { try { // Parse salt and IV const salt = CryptoJS.enc.Hex.parse(encryptedData.salt); const iv = CryptoJS.enc.Hex.parse(encryptedData.iv); // Derive key from password const key = this.deriveKey(password, salt); // Decrypt the data const decrypted = this.ALGORITHM.decrypt(encryptedData.data, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); const decryptedString = decrypted.toString(CryptoJS.enc.Utf8); if (!decryptedString) { throw new Error('Invalid password or corrupted data'); } return decryptedString; } catch (error) { throw new Error(`Decryption failed: ${error}`); } } /** * Generates a secure hash of a password for verification */ static hashPassword(password) { const salt = CryptoJS.lib.WordArray.random(128 / 8); const hash = CryptoJS.PBKDF2(password, salt, { keySize: 256 / 32, iterations: this.ITERATIONS, hasher: CryptoJS.algo.SHA256 }); return salt.toString() + ':' + hash.toString(); } /** * Verifies a password against its hash */ static verifyPassword(password, hash) { try { const [saltStr, hashStr] = hash.split(':'); const salt = CryptoJS.enc.Hex.parse(saltStr); const computedHash = CryptoJS.PBKDF2(password, salt, { keySize: 256 / 32, iterations: this.ITERATIONS, hasher: CryptoJS.algo.SHA256 }); return computedHash.toString() === hashStr; } catch (error) { return false; } } /** * Generates a random ID for journal entries */ static generateId() { return CryptoJS.lib.WordArray.random(64 / 8).toString(); } } exports.CryptoService = CryptoService; CryptoService.ALGORITHM = CryptoJS.AES; CryptoService.KEY_SIZE = 256; CryptoService.IV_SIZE = 128; CryptoService.SALT_SIZE = 128; CryptoService.ITERATIONS = 10000; //# sourceMappingURL=CryptoService.js.map