@iotize/device-client.js
Version:
IoTize Device client for Javascript
49 lines (48 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var crypto_1 = require("crypto");
var core_1 = require("../../core");
var buffer_1 = require("buffer");
var AesEcb128Converter = /** @class */ (function () {
function AesEcb128Converter(options) {
if (options) {
this.setOptions(options);
}
}
AesEcb128Converter.prototype.setOptions = function (value) {
this._options = value;
this._cipher = crypto_1.createCipheriv(AesEcb128Converter.ALGO, buffer_1.Buffer.from(this._options.key.buffer), buffer_1.Buffer.from(this._options.iv));
this._decipher = crypto_1.createDecipheriv(AesEcb128Converter.ALGO, buffer_1.Buffer.from(this._options.key.buffer), buffer_1.Buffer.from(this._options.iv));
this._decipher.setAutoPadding(false); // Mandatory apparently otherwise result is null...
};
Object.defineProperty(AesEcb128Converter.prototype, "options", {
get: function () {
return this._options;
},
enumerable: true,
configurable: true
});
AesEcb128Converter.prototype.encode = function (data) {
if (!this._cipher) {
throw new Error("You must set key/initialisation vector first");
}
var input = buffer_1.Buffer.from(data.buffer);
var encryptedData = this._cipher.update(input, AesEcb128Converter.ENCODE_INPUT_TYPE, AesEcb128Converter.ENCODE_OUTPUT_TYPE); // + cipher.final('hex');
return core_1.FormatHelper.hexStringToBuffer(encryptedData); // TODO optimisation: we get an hex string that we have to convert as a buffer. Not very optimized
};
AesEcb128Converter.prototype.decode = function (data) {
if (!this._decipher) {
throw new Error("You must set key/initialisation vector first");
}
var input = buffer_1.Buffer.from(data.buffer).toString(AesEcb128Converter.DECODE_INPUT_TYPE);
var result = this._decipher.update(input, AesEcb128Converter.DECODE_INPUT_TYPE, AesEcb128Converter.DECODE_OUTPUT_TYPE);
return Uint8Array.from(buffer_1.Buffer.from(result, AesEcb128Converter.DECODE_OUTPUT_TYPE));
};
AesEcb128Converter.ALGO = 'aes-128-cbc';
AesEcb128Converter.DECODE_INPUT_TYPE = 'hex';
AesEcb128Converter.DECODE_OUTPUT_TYPE = 'binary';
AesEcb128Converter.ENCODE_INPUT_TYPE = 'hex';
AesEcb128Converter.ENCODE_OUTPUT_TYPE = 'hex';
return AesEcb128Converter;
}());
exports.AesEcb128Converter = AesEcb128Converter;