UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

66 lines (65 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var crypto_js_1 = require("crypto-js"); var format_helper_1 = require("../../core/format/format-helper"); var AesEcb128Converter = /** @class */ (function () { // _cipher?: Cipher; // _decipher?: Decipher; function AesEcb128Converter(options) { if (options) { this.setOptions(options); } } AesEcb128Converter.prototype.setOptions = function (options) { if (!this._options) { this._options = {}; } this._options.key = crypto_js_1.enc.Hex.parse(options.key); var ivEncode = options.hasOwnProperty('ivEncode') ? options.ivEncode : options.iv; this._options.ivEncode = crypto_js_1.enc.Hex.parse(ivEncode); var ivDecode = options.hasOwnProperty('ivDecode') ? options.ivDecode : options.iv; this._options.ivDecode = crypto_js_1.enc.Hex.parse(ivDecode); }; Object.defineProperty(AesEcb128Converter.prototype, "options", { get: function () { return this._options; }, enumerable: true, configurable: true }); AesEcb128Converter.prototype.encode = function (data) { if (!this._options) { throw new Error("AES options has not been set yet"); } var input = crypto_js_1.enc.Hex.parse(format_helper_1.FormatHelper.toHexString(data)); var key = this._options.key; var iv = this._options.ivEncode; var encrypted = crypto_js_1.AES.encrypt(input, key, { iv: iv, mode: crypto_js_1.mode.CBC, padding: crypto_js_1.pad.NoPadding }); var encryptedHexString = crypto_js_1.enc.Hex.stringify(encrypted.ciphertext); return format_helper_1.FormatHelper.hexStringToBuffer(encryptedHexString); }; AesEcb128Converter.prototype.decode = function (data) { if (!this._options) { throw new Error("AES options has not been set yet"); } var input = crypto_js_1.enc.Hex.parse(format_helper_1.FormatHelper.toHexString(data)); var key = this._options.key; var iv = this._options.ivDecode; var decrypted = crypto_js_1.AES.decrypt({ ciphertext: input, salt: "", iv: iv, }, key, { iv: iv, mode: crypto_js_1.mode.CBC, padding: crypto_js_1.pad.NoPadding }); return format_helper_1.FormatHelper.hexStringToBuffer(decrypted.toString(crypto_js_1.enc.Hex)); }; return AesEcb128Converter; }()); exports.AesEcb128Converter = AesEcb128Converter;