UNPKG

@amaui/utils

Version:
33 lines (27 loc) 805 B
import AES from 'crypto-js/aes'; import encUtf8 from 'crypto-js/enc-utf8'; import parse from './parse'; const optionsDefault = { exception: false }; const decrypt = function (value_, privateValue) { let options_ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; const options = { ...optionsDefault, ...options_ }; let value = value_; try { value = AES.decrypt(value_, privateValue).toString(encUtf8); } 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 parse(value); }; export default decrypt;