UNPKG

silvie

Version:

Typescript Back-end Framework

110 lines (102 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _bcrypt = _interopRequireDefault(require("bcrypt")); var _crypto = _interopRequireDefault(require("crypto")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class Crypt { /** * Uses bcrypt to make hash from a plain value * @param plain */ static make(plain) { return _bcrypt.default.hashSync(plain, 8); } /** * Uses bcrypt to compare a plain value against a hashed value * @param plain * @param hashed */ static check(plain, hashed) { return _bcrypt.default.compareSync(plain, hashed); } /** * Hashes the specified data * @param data Data to be hashed * @param algorithm Hashing algorithm (defaults to sha256) * @param digest returning digest type (defaults to hex) */ static hash(data, algorithm, digest) { return _crypto.default.createHash(algorithm || 'sha256').update(data).digest(digest || 'hex'); } /** * Generate random IV * @param length */ static generateIV(length = 16) { return _crypto.default.randomBytes(length); } /** * Generate random Key * @param length */ static generateKey(length = 32) { return _crypto.default.randomBytes(length); } /** * Generate a buffer with random bytes * @param length */ static randomBytes(length) { return _crypto.default.randomBytes(length); } /** * Encrypt a data with a key and iv * @param data * @param key Encryption key * @param algorithm Encryption algorithm * @param digest Output digest type * @param IVLength Initialization vector length */ static encrypt(data, key, algorithm, digest, IVLength = 16) { if (key === undefined) { throw new Error('Cannot encrypt without a Key'); } const ivBuffer = this.generateIV(IVLength); const keyBuffer = key instanceof Buffer ? key : Buffer.from(key); const cipher = _crypto.default.createCipheriv(algorithm, keyBuffer, ivBuffer); let encrypted = cipher.update(data); encrypted = Buffer.concat([encrypted, cipher.final()]); return { data: encrypted.toString(digest || 'hex'), iv: ivBuffer.toString(digest || 'hex') }; } /** * Decrypt a data with a key and iv * @param data * @param key Decryption key * @param iv Initialization vector * @param algorithm Encryption algorithm * @param digest Output digest type */ static decrypt(data, key, iv, algorithm, digest) { if (key === undefined || iv === undefined) { throw new Error('Cannot decrypt without the Key and IV'); } try { const ivBuffer = iv instanceof Buffer ? iv : Buffer.from(iv, digest || 'hex'); const keyBuffer = key instanceof Buffer ? key : Buffer.from(key, digest || 'hex'); const encryptedBuffer = typeof data === 'string' ? Buffer.from(data, digest || 'hex') : data; const decipher = _crypto.default.createDecipheriv(algorithm, keyBuffer, ivBuffer); let decrypted = decipher.update(encryptedBuffer); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); } catch (e) { throw new Error('Something went wrong: Invalid Cipher Data'); } } } exports.default = Crypt;