1 | 'use strict';
|
2 |
|
3 | const AppError = require('../lib/appError');
|
4 | const crypto = require('crypto');
|
5 |
|
6 | class Secrets {
|
7 |
|
8 | |
9 |
|
10 |
|
11 | constructor (password) {
|
12 | this._password = password;
|
13 | }
|
14 |
|
15 | |
16 |
|
17 |
|
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 |
|
28 |
|
29 |
|
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 |
|
44 | module.exports = Secrets;
|