botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
49 lines (35 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _crypto = require('crypto');
var _crypto2 = _interopRequireDefault(_crypto);
var _nanoid = require('nanoid');
var _nanoid2 = _interopRequireDefault(_nanoid);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const RANDOM_SECRET_KEY_LENGTH = 32;
const IV_LENGTH = 16;
class Crypto {
constructor({ botfile }) {
this.algorithm = 'aes-256-cbc';
this.secretKey = botfile.secretKey || (0, _nanoid2.default)(RANDOM_SECRET_KEY_LENGTH);
}
encrypt(text) {
const iv = _crypto2.default.randomBytes(IV_LENGTH);
const cipher = _crypto2.default.createCipheriv('aes-256-cbc', new Buffer(this.secretKey), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
decrypt(text) {
const textParts = text.split(':');
const iv = new Buffer(textParts.shift(), 'hex');
const encryptedText = new Buffer(textParts.join(':'), 'hex');
const decipher = _crypto2.default.createDecipheriv('aes-256-cbc', new Buffer(this.secretKey), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
}
exports.default = Crypto;
//# sourceMappingURL=index.js.map