diff --git a/src/settings/encryptionSetting.ts b/src/settings/encryptionSetting.ts index 3d6177684..c930a72c4 100644 --- a/src/settings/encryptionSetting.ts +++ b/src/settings/encryptionSetting.ts @@ -10,4 +10,4 @@ export const cacheCipher = { }; // Whether the system cache is encrypted using aes -export const enableStorageEncryption = !isDevMode(); +export const SHOULD_ENABLE_STORAGE_ENCRYPTION = !isDevMode(); diff --git a/src/store/plugin/persist.ts b/src/store/plugin/persist.ts index 4fa176e46..8c8939f7b 100644 --- a/src/store/plugin/persist.ts +++ b/src/store/plugin/persist.ts @@ -5,13 +5,49 @@ * */ import type { Pinia } from 'pinia'; -import { createPersistedState } from 'pinia-plugin-persistedstate'; +import { createPersistedState, Serializer } from 'pinia-plugin-persistedstate'; import type { PersistedStateFactoryOptions } from 'pinia-plugin-persistedstate'; import { getCommonStoragePrefix } from '@/utils/env'; +import { Encryption, EncryptionFactory } from '@/utils/cipher'; +import { cacheCipher, SHOULD_ENABLE_STORAGE_ENCRYPTION } from '@/settings/encryptionSetting'; export const PERSIST_KEY_PREFIX = getCommonStoragePrefix(); -// TODO customSerializer +const persistEncryption: Encryption = EncryptionFactory.createAesEncryption({ + key: cacheCipher.key, + iv: cacheCipher.iv, +}); + +/** + * Custom serializer for serialization and deserialization of storage data + * 自定义序列化器,用于序列化和反序列化存储数据 + * + * @param shouldEnableEncryption whether to enable encryption for storage data 是否启用存储数据加密 + * @returns serializer + */ +function customSerializer(shouldEnableEncryption: boolean): Serializer { + if (shouldEnableEncryption) { + return { + deserialize: (value) => { + const decrypted = persistEncryption.decrypt(value); + return JSON.parse(decrypted); + }, + serialize: (value) => { + const serialized = JSON.stringify(value); + return persistEncryption.encrypt(serialized); + }, + }; + } else { + return { + deserialize: (value) => { + return JSON.parse(value); + }, + serialize: (value) => { + return JSON.stringify(value); + }, + }; + } +} /** * Register Pinia Persist Plugin @@ -34,5 +70,6 @@ export function createPersistedStateOptions(keyPrefix: string): PersistedStateFa return { storage: localStorage, key: (id) => `${keyPrefix}__${id}`, + serializer: customSerializer(SHOULD_ENABLE_STORAGE_ENCRYPTION), }; } diff --git a/src/utils/cache/index.ts b/src/utils/cache/index.ts index 01a11f54e..0f3c655e0 100644 --- a/src/utils/cache/index.ts +++ b/src/utils/cache/index.ts @@ -1,13 +1,16 @@ import { getStorageShortName } from '/@/utils/env'; import { createStorage as create, CreateStorageParams } from './storageCache'; -import { enableStorageEncryption, DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; +import { + SHOULD_ENABLE_STORAGE_ENCRYPTION, + DEFAULT_CACHE_TIME, +} from '/@/settings/encryptionSetting'; export type Options = Partial; const createOptions = (storage: Storage, options: Options = {}): Options => { return { // No encryption in debug mode - hasEncrypt: enableStorageEncryption, + hasEncrypt: SHOULD_ENABLE_STORAGE_ENCRYPTION, storage, prefixKey: getStorageShortName(), ...options, diff --git a/src/utils/cache/storageCache.ts b/src/utils/cache/storageCache.ts index 6e1d4e593..6b1dabb96 100644 --- a/src/utils/cache/storageCache.ts +++ b/src/utils/cache/storageCache.ts @@ -9,6 +9,7 @@ export interface CreateStorageParams extends EncryptionParams { hasEncrypt: boolean; timeout?: Nullable; } +// TODO 移除此文件夹下全部代码 export const createStorage = ({ prefixKey = '', storage = sessionStorage,