refactor: remove cipher old (#3245)
This commit is contained in:
parent
ea51c492c2
commit
11cbe66937
|
|
@ -1,7 +1,6 @@
|
||||||
import { cacheCipher } from '/@/settings/encryptionSetting';
|
import { cacheCipher } from '/@/settings/encryptionSetting';
|
||||||
import type { EncryptionParams } from '/@/utils/cipherOld';
|
|
||||||
import { AesEncryption } from '/@/utils/cipherOld';
|
|
||||||
import { isNullOrUnDef } from '/@/utils/is';
|
import { isNullOrUnDef } from '/@/utils/is';
|
||||||
|
import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher';
|
||||||
|
|
||||||
export interface CreateStorageParams extends EncryptionParams {
|
export interface CreateStorageParams extends EncryptionParams {
|
||||||
prefixKey: string;
|
prefixKey: string;
|
||||||
|
|
@ -22,8 +21,10 @@ export const createStorage = ({
|
||||||
throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!');
|
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
|
* Cache class
|
||||||
* Construction parameters can be passed into sessionStorage, localStorage,
|
* Construction parameters can be passed into sessionStorage, localStorage,
|
||||||
|
|
@ -33,7 +34,7 @@ export const createStorage = ({
|
||||||
const WebStorage = class WebStorage {
|
const WebStorage = class WebStorage {
|
||||||
private storage: Storage;
|
private storage: Storage;
|
||||||
private prefixKey?: string;
|
private prefixKey?: string;
|
||||||
private encryption: AesEncryption;
|
private encryption: Encryption;
|
||||||
private hasEncrypt: boolean;
|
private hasEncrypt: boolean;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -42,7 +43,7 @@ export const createStorage = ({
|
||||||
constructor() {
|
constructor() {
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
this.prefixKey = prefixKey;
|
this.prefixKey = prefixKey;
|
||||||
this.encryption = encryption;
|
this.encryption = persistEncryption;
|
||||||
this.hasEncrypt = hasEncrypt;
|
this.hasEncrypt = hasEncrypt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,9 +64,7 @@ export const createStorage = ({
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
|
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
|
||||||
});
|
});
|
||||||
const stringifyValue = this.hasEncrypt
|
const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData;
|
||||||
? this.encryption.encryptByAES(stringData)
|
|
||||||
: stringData;
|
|
||||||
this.storage.setItem(this.getKey(key), stringifyValue);
|
this.storage.setItem(this.getKey(key), stringifyValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,7 +79,7 @@ export const createStorage = ({
|
||||||
if (!val) return def;
|
if (!val) return def;
|
||||||
|
|
||||||
try {
|
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 data = JSON.parse(decVal);
|
||||||
const { value, expire } = data;
|
const { value, expire } = data;
|
||||||
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
|
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
|
||||||
|
|
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
import { encrypt, decrypt } from 'crypto-js/aes';
|
|
||||||
import UTF8, { parse } from 'crypto-js/enc-utf8';
|
|
||||||
import pkcs7 from 'crypto-js/pad-pkcs7';
|
|
||||||
import ECB from 'crypto-js/mode-ecb';
|
|
||||||
import md5 from 'crypto-js/md5';
|
|
||||||
import Base64 from 'crypto-js/enc-base64';
|
|
||||||
|
|
||||||
export interface EncryptionParams {
|
|
||||||
key: string;
|
|
||||||
iv: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AesEncryption {
|
|
||||||
private key;
|
|
||||||
private iv;
|
|
||||||
|
|
||||||
constructor(opt: Partial<EncryptionParams> = {}) {
|
|
||||||
const { key, iv } = opt;
|
|
||||||
if (key) {
|
|
||||||
this.key = parse(key);
|
|
||||||
}
|
|
||||||
if (iv) {
|
|
||||||
this.iv = parse(iv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get getOptions() {
|
|
||||||
return {
|
|
||||||
mode: ECB,
|
|
||||||
padding: pkcs7,
|
|
||||||
iv: this.iv,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
encryptByAES(cipherText: string) {
|
|
||||||
return encrypt(cipherText, this.key, this.getOptions).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
decryptByAES(cipherText: string) {
|
|
||||||
return decrypt(cipherText, this.key, this.getOptions).toString(UTF8);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function encryptByBase64(cipherText: string) {
|
|
||||||
return UTF8.parse(cipherText).toString(Base64);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function decodeByBase64(cipherText: string) {
|
|
||||||
return Base64.parse(cipherText).toString(UTF8);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function encryptByMd5(password: string) {
|
|
||||||
return md5(password).toString();
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue