@onesy/utils
Version:
33 lines (32 loc) • 1.13 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const aes_1 = __importDefault(require("crypto-js/aes"));
const enc_utf8_1 = __importDefault(require("crypto-js/enc-utf8"));
const parse_1 = __importDefault(require("./parse"));
const optionsDefault = {
exception: false
};
const decrypt = (value_, privateValue, options_ = {}) => {
const options = Object.assign(Object.assign({}, optionsDefault), options_);
let value = value_;
try {
value = aes_1.default.decrypt(value_, privateValue).toString(enc_utf8_1.default);
}
catch (error) {
value = '';
}
// This is only incorrect,
// when encrypted data is an empty string
// it's a lib issue not giving any
// incorrect privateValue information atm
if (!value.length) {
if (options.exception)
throw new Error('Private value is incorrect');
return;
}
return (0, parse_1.default)(value);
};
exports.default = decrypt;
;