| | |
| | | import { cacheCipher } from '/@/settings/encryptionSetting'; |
| | | import type { EncryptionParams } from '/@/utils/cipher'; |
| | | import { AesEncryption } from '/@/utils/cipher'; |
| | | import { isNullOrUnDef } from '/@/utils/is'; |
| | | import { cacheCipher } from '@/settings/encryptionSetting'; |
| | | import { isNil } from '@/utils/is'; |
| | | import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher'; |
| | | |
| | | export interface CreateStorageParams extends EncryptionParams { |
| | | prefixKey: string; |
| | |
| | | hasEncrypt: boolean; |
| | | timeout?: Nullable<number>; |
| | | } |
| | | // TODO 移除此文件夹下全部代码 |
| | | export const createStorage = ({ |
| | | prefixKey = '', |
| | | storage = sessionStorage, |
| | |
| | | throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!'); |
| | | } |
| | | |
| | | const encryption = new AesEncryption({ key, iv }); |
| | | |
| | | const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({ |
| | | key: cacheCipher.key, |
| | | iv: cacheCipher.iv, |
| | | }); |
| | | /** |
| | | * Cache class |
| | | * Construction parameters can be passed into sessionStorage, localStorage, |
| | |
| | | const WebStorage = class WebStorage { |
| | | private storage: Storage; |
| | | private prefixKey?: string; |
| | | private encryption: AesEncryption; |
| | | private encryption: Encryption; |
| | | private hasEncrypt: boolean; |
| | | /** |
| | | * |
| | |
| | | constructor() { |
| | | this.storage = storage; |
| | | this.prefixKey = prefixKey; |
| | | this.encryption = encryption; |
| | | this.encryption = persistEncryption; |
| | | this.hasEncrypt = hasEncrypt; |
| | | } |
| | | |
| | |
| | | const stringData = JSON.stringify({ |
| | | value, |
| | | time: Date.now(), |
| | | expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null, |
| | | expire: !isNil(expire) ? new Date().getTime() + expire * 1000 : null, |
| | | }); |
| | | const stringifyValue = this.hasEncrypt |
| | | ? this.encryption.encryptByAES(stringData) |
| | | : stringData; |
| | | const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData; |
| | | this.storage.setItem(this.getKey(key), stringifyValue); |
| | | } |
| | | |
| | |
| | | if (!val) return def; |
| | | |
| | | try { |
| | | const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val; |
| | | const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val; |
| | | const data = JSON.parse(decVal); |
| | | const { value, expire } = data; |
| | | if (isNullOrUnDef(expire) || expire >= new Date().getTime()) { |
| | | if (isNil(expire) || expire >= new Date().getTime()) { |
| | | return value; |
| | | } |
| | | this.remove(key); |