"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _crypto = _interopRequireDefault(require("crypto"));

var _lodash = _interopRequireDefault(require("lodash"));

var _indexModule = require("./../../../index.module.d");

var index = function index(Config) {
  var _this = this;

  (0, _classCallCheck2["default"])(this, index);
  (0, _defineProperty2["default"])(this, "encodeIv", /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(text) {
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              return _context4.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(resolve, rejected) {
                  return _regenerator["default"].wrap(function _callee3$(_context3) {
                    while (1) {
                      switch (_context3.prev = _context3.next) {
                        case 0:
                          _context3.next = 2;
                          return new Promise( /*#__PURE__*/function () {
                            var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(resolve2, rejected) {
                              return _regenerator["default"].wrap(function _callee$(_context) {
                                while (1) {
                                  switch (_context.prev = _context.next) {
                                    case 0:
                                      _context.t0 = _this.Config.algorithm;
                                      _context.next = _context.t0 === _indexModule.Options.ALGORITHM_AES_256_GCM ? 3 : _context.t0 === _indexModule.Options.ALGORITHM_AES_192_GCM ? 6 : _context.t0 === _indexModule.Options.ALGORITHM_AES_128_GCM ? 9 : 12;
                                      break;

                                    case 3:
                                      _context.next = 5;
                                      return resolve2({
                                        key: Buffer.alloc(32, _this.Config.secretKey),
                                        iv: _crypto["default"].randomBytes(16)
                                      });

                                    case 5:
                                      return _context.abrupt("break", 13);

                                    case 6:
                                      _context.next = 8;
                                      return resolve2({
                                        key: Buffer.alloc(24, _this.Config.secretKey),
                                        iv: _crypto["default"].randomBytes(16)
                                      });

                                    case 8:
                                      return _context.abrupt("break", 13);

                                    case 9:
                                      _context.next = 11;
                                      return resolve2({
                                        key: Buffer.alloc(16, _this.Config.secretKey),
                                        iv: _crypto["default"].randomBytes(16)
                                      });

                                    case 11:
                                      return _context.abrupt("break", 13);

                                    case 12:
                                      rejected({
                                        status: false,
                                        code: 500,
                                        msg: "Algoritm undefined"
                                      });

                                    case 13:
                                    case "end":
                                      return _context.stop();
                                  }
                                }
                              }, _callee);
                            }));

                            return function (_x4, _x5) {
                              return _ref3.apply(this, arguments);
                            };
                          }()).then( /*#__PURE__*/function () {
                            var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(res) {
                              var crypto, encrypted;
                              return _regenerator["default"].wrap(function _callee2$(_context2) {
                                while (1) {
                                  switch (_context2.prev = _context2.next) {
                                    case 0:
                                      _context2.prev = 0;
                                      crypto = _crypto["default"].createCipheriv(_this.Config.algorithm, res.key, res.iv); // Updating text

                                      // Updating text
                                      encrypted = crypto.update(text); // Using concatenation

                                      // Using concatenation
                                      encrypted = Buffer.concat([encrypted, crypto["final"]()]); // Returning iv and encrypted data

                                      _context2.next = 6;
                                      return resolve("".concat(res.iv.toString('hex'), ":").concat(encrypted.toString('hex')));

                                    case 6:
                                      _context2.next = 11;
                                      break;

                                    case 8:
                                      _context2.prev = 8;
                                      _context2.t0 = _context2["catch"](0);
                                      rejected({
                                        status: false,
                                        code: 500,
                                        msg: "Error, to Decode hash Data",
                                        error: _context2.t0
                                      });

                                    case 11:
                                    case "end":
                                      return _context2.stop();
                                  }
                                }
                              }, _callee2, null, [[0, 8]]);
                            }));

                            return function (_x6) {
                              return _ref4.apply(this, arguments);
                            };
                          }());

                        case 2:
                        case "end":
                          return _context3.stop();
                      }
                    }
                  }, _callee3);
                }));

                return function (_x2, _x3) {
                  return _ref2.apply(this, arguments);
                };
              }()));

            case 1:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }());
  (0, _defineProperty2["default"])(this, "encodeIvSync", function (text) {
    var res = {
      key: _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_256_GCM ? Buffer.alloc(32, _this.Config.secretKey) : _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_192_GCM ? Buffer.alloc(24, _this.Config.secretKey) : _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_128_GCM ? Buffer.alloc(16, _this.Config.secretKey) : "",
      iv: Buffer.alloc(16, _this.Config.secretKey)
    };

    var crypto = _crypto["default"].createCipheriv(_this.Config.algorithm, res.key, res.iv); // Updating text


    var mText = typeof text === "string" ? text : JSON.stringify(text);
    var encrypted = crypto.update(mText); // Using concatenation

    encrypted = Buffer.concat([encrypted, crypto["final"]()]); // Returning iv and encrypted data

    return Buffer.from("".concat(res.iv.toString('hex'), ":").concat(encrypted.toString('hex'))).toString('base64');
  });
  (0, _defineProperty2["default"])(this, "decodeIv", /*#__PURE__*/function () {
    var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(text) {
      return _regenerator["default"].wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              return _context8.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(resolve, rejected) {
                  var hash;
                  return _regenerator["default"].wrap(function _callee7$(_context7) {
                    while (1) {
                      switch (_context7.prev = _context7.next) {
                        case 0:
                          hash = text.split(':');
                          _context7.next = 3;
                          return new Promise( /*#__PURE__*/function () {
                            var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(resolve2, rejected) {
                              return _regenerator["default"].wrap(function _callee5$(_context5) {
                                while (1) {
                                  switch (_context5.prev = _context5.next) {
                                    case 0:
                                      _context5.t0 = _this.Config.algorithm;
                                      _context5.next = _context5.t0 === _indexModule.Options.ALGORITHM_AES_256_GCM ? 3 : _context5.t0 === _indexModule.Options.ALGORITHM_AES_192_GCM ? 6 : _context5.t0 === _indexModule.Options.ALGORITHM_AES_128_GCM ? 9 : 12;
                                      break;

                                    case 3:
                                      _context5.next = 5;
                                      return resolve2({
                                        key: Buffer.alloc(32, _this.Config.secretKey),
                                        iv: Buffer.from(hash[0].toString(), 'hex')
                                      });

                                    case 5:
                                      return _context5.abrupt("break", 13);

                                    case 6:
                                      _context5.next = 8;
                                      return resolve2({
                                        key: Buffer.alloc(24, _this.Config.secretKey),
                                        iv: Buffer.from(hash[0].toString(), 'hex')
                                      });

                                    case 8:
                                      return _context5.abrupt("break", 13);

                                    case 9:
                                      _context5.next = 11;
                                      return resolve2({
                                        key: Buffer.alloc(16, _this.Config.secretKey),
                                        iv: Buffer.from(hash[0].toString(), 'hex')
                                      });

                                    case 11:
                                      return _context5.abrupt("break", 13);

                                    case 12:
                                      rejected({
                                        status: false,
                                        code: 500,
                                        msg: "Algoritm undefined"
                                      });

                                    case 13:
                                    case "end":
                                      return _context5.stop();
                                  }
                                }
                              }, _callee5);
                            }));

                            return function (_x10, _x11) {
                              return _ref7.apply(this, arguments);
                            };
                          }()).then( /*#__PURE__*/function () {
                            var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(res) {
                              var crypto, decrpyted;
                              return _regenerator["default"].wrap(function _callee6$(_context6) {
                                while (1) {
                                  switch (_context6.prev = _context6.next) {
                                    case 0:
                                      try {
                                        crypto = _crypto["default"].createDecipheriv(_this.Config.algorithm, res.key, res.iv);
                                        decrpyted = crypto.update(Buffer.from(hash[1], 'hex')).toString();
                                        resolve(decrpyted);
                                      } catch (e) {
                                        rejected({
                                          status: false,
                                          code: 500,
                                          msg: "Error, to Decode hash Data",
                                          error: e
                                        });
                                      }

                                    case 1:
                                    case "end":
                                      return _context6.stop();
                                  }
                                }
                              }, _callee6);
                            }));

                            return function (_x12) {
                              return _ref8.apply(this, arguments);
                            };
                          }());

                        case 3:
                        case "end":
                          return _context7.stop();
                      }
                    }
                  }, _callee7);
                }));

                return function (_x8, _x9) {
                  return _ref6.apply(this, arguments);
                };
              }()));

            case 1:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    }));

    return function (_x7) {
      return _ref5.apply(this, arguments);
    };
  }());
  (0, _defineProperty2["default"])(this, "decodeIvSync", function (text) {
    var mReturn = null;
    var toUtf8 = Buffer.from(text, 'base64').toString('utf-8');
    var hash = toUtf8.split(":");
    var res = {
      key: _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_256_GCM ? Buffer.alloc(32, _this.Config.secretKey) : _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_192_GCM ? Buffer.alloc(24, _this.Config.secretKey) : _this.Config.algorithm === _indexModule.Options.ALGORITHM_AES_128_GCM ? Buffer.alloc(16, _this.Config.secretKey) : "",
      iv: Buffer.alloc(16, _this.Config.secretKey)
    };

    var crypto = _crypto["default"].createDecipheriv(_this.Config.algorithm, res.key, res.iv);

    var textUpdate = crypto.update(Buffer.from(hash[1], 'hex')).toString();

    try {
      mReturn = JSON.parse(textUpdate);
    } catch (e) {
      mReturn = textUpdate;
    }

    return mReturn;
  });
  (0, _defineProperty2["default"])(this, "encodeGeneral", function (text) {
    var Chiper = _crypto["default"].scryptSync(text, _this.Config.secretKey, 16);

    return Chiper.toString('base64');
  });
  (0, _defineProperty2["default"])(this, "decodeGeneral", function (text) {
    var Chiper = _crypto["default"];
  });
  this.Config = _lodash["default"].extend({
    algorithm: _indexModule.Options.ALGORITHM_AES_256_GCM,
    secretKey: "Cyberhack2010",
    keyLength: undefined
  }, Config);
};

var _default = index;
exports["default"] = _default;
//# sourceMappingURL=index.js.map