UNPKG

1.04 kBJavaScriptView Raw
1'use strict';
2
3const AppError = require('../lib/appError');
4const crypto = require('crypto');
5
6class Secrets {
7
8 /**
9 * @param {string} password
10 */
11 constructor (password) {
12 this._password = password;
13 }
14
15 /**
16 * @param text
17 * @returns {string}
18 */
19 encrypt (text) {
20 const cipher = crypto.createCipher('aes-256-cbc', this._password);
21 let crypted = cipher.update(text, 'utf8', 'hex');
22 crypted += cipher.final('hex');
23 return crypted;
24 }
25
26 /**
27 * @param {string} encryptedText
28 * @returns {string}
29 * @throws {AppError}
30 */
31 decrypt (encryptedText) {
32 try {
33 const decipher = crypto.createDecipher('aes-256-cbc', this._password);
34 let dec = decipher.update(encryptedText, 'hex', 'utf8');
35 dec += decipher.final('utf8');
36 return dec;
37 } catch (error) {
38 throw new AppError('Error when decrypting integration credentials.', error);
39 }
40 }
41
42}
43
44module.exports = Secrets;