fcoin/lib/crypto/aes.js
2017-07-17 14:26:40 -07:00

51 lines
1.1 KiB
JavaScript

/*!
* aes.js - aes for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
/**
* @module crypto.aes
*/
const crypto = require('crypto');
const util = require('../utils/util');
const native = require('../native').binding;
/**
* Encrypt data with aes 256 cbc.
* @param {Buffer} data
* @param {Buffer} key
* @param {Buffer} iv
* @returns {Buffer}
*/
exports.encipher = function encipher(data, key, iv) {
let cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
return util.concat(cipher.update(data), cipher.final());
};
/**
* Decrypt data with aes 256 cbc.
* @param {Buffer} data
* @param {Buffer} key
* @param {Buffer} iv
* @returns {Buffer}
*/
exports.decipher = function _decipher(data, key, iv) {
let decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
try {
return util.concat(decipher.update(data), decipher.final());
} catch (e) {
throw new Error('Bad key for decryption.');
}
};
if (native) {
exports.encipher = native.encipher;
exports.decipher = native.decipher;
}