UNPKG

radius-read

Version:

Realtime Dashboard

102 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CipherService = void 0; const tslib_1 = require("tslib"); const crypto = tslib_1.__importStar(require("crypto")); const CryptoJS = tslib_1.__importStar(require("crypto-js")); /** * CipherService provides methods for encrypting and decrypting strings using AES-256-CBC. */ class CipherService { /** * Logger instance for logging debug information. */ constructor() { } /** * Encrypts the given text using AES-256-CBC algorithm. * @param text * @returns */ static encrypt(text) { let cipher = crypto.createCipheriv(this.ALGORITHM, Buffer.from(this.KEY, 'hex'), Buffer.from(this.IV, 'hex')); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return encrypted.toString('hex'); } /** * Decrypts the given text using AES-256-CBC algorithm. * @param text * @returns */ static decrypt(text) { let iv = Buffer.from(this.IV, 'hex'); let encryptedText = Buffer.from(text, 'hex'); let decipher = crypto.createDecipheriv(this.ALGORITHM, Buffer.from(this.KEY, 'hex'), iv); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); } /** * AES encrypts the given text and optionally encodes it as a URI component. * @param textToEncrypt * @param isUri * @returns */ static aesEncrypt(textToEncrypt, isUri = false) { if (isUri === true) return encodeURIComponent(CryptoJS.AES.encrypt(textToEncrypt, this.AES_CHIPERKEY.trim()).toString()); var result = CryptoJS.AES.encrypt(textToEncrypt, this.AES_CHIPERKEY.trim()).toString(); return result; } /** * Aes decrypts the given text and optionally decodes it from a URI component. * @param textToDecrypt * @param isUri * @returns */ static aesDecrypt(textToDecrypt, isUri = false) { if (isUri === true) { textToDecrypt = decodeURIComponent(textToDecrypt); } var result = CryptoJS.AES.decrypt(textToDecrypt, this.AES_CHIPERKEY.trim()).toString(CryptoJS.enc.Utf8); return result; } } exports.CipherService = CipherService; /** * The algorithm used for encryption and decryption. * @date Jun 26, 2025 11:03:22 AM * @author Biswaranjan Nayak * * @private * @type {string} */ CipherService.ALGORITHM = 'aes-256-cbc'; /** * Key used for AES encryption and decryption. * @date Jun 26, 2025 11:03:37 AM * @author Biswaranjan Nayak * * @private * @type {*} */ CipherService.KEY = `f4a643800c1c1b21371dea5926e9af24ba12c424460ef374e3d0055f05e938be`; /** * IV (Initialization Vector) used for AES encryption and decryption. * @date Jun 26, 2025 11:03:51 AM * @author Biswaranjan Nayak * * @private * @type {*} */ CipherService.IV = `aaa82fc0c8e950500195e64a1e53efbb`; /** * AES cipher key used for encryption and decryption in static methods. * @date Jun 26, 2025 11:07:13 AM * @author Biswaranjan Nayak * * @private * @type {string} */ CipherService.AES_CHIPERKEY = 'c4257a45-dc4d-4fbc-8052-c24f4768b8ca'; //# sourceMappingURL=cipher.service.js.map