UNPKG

@izzius94/crypter

Version:

The crypter library makes easy to share encrypted data with a Laravel project.

45 lines (44 loc) 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crypt_1 = require("./crypt"); const key_1 = require("./key"); class default_1 { /** * Initialize the class * * @param key The key used to encrypt/decrypt the strings * @param algorithm The algorithm used to encrypt/decrypt the strings */ constructor(key, algorithm = 'aes-256-cbc') { this.key = key; this.algorithm = algorithm; this.checkKey(); } /** * Method to decrypt an encrypted string * * @param encrypted The string to decrypt * @returns The string decrypted */ decrypt(encrypted) { return (0, crypt_1.decrypt)(encrypted, this.key, this.algorithm); } /** * Method to enctypt a string * * @param value The value to encrypt * @returns The string encrypted */ encrypt(value) { return (0, crypt_1.encrypt)(value, this.key, this.algorithm); } /** * Method to check if the provided key is valid */ checkKey() { if (this.key.toString('ascii').length !== key_1.config[this.algorithm]) { throw Error('Invalid key length.'); } } } exports.default = default_1;