当前位置:网站首页>Use ASE encryption and decryption cache encapsulation in Vue project
Use ASE encryption and decryption cache encapsulation in Vue project
2022-06-13 04:05:00 【Shadow night with the wind】
Cache encryption function encapsulation
Not long ago, I developed a project that requires data cache encryption , For the convenience of handling , Encapsulate a cache file .
Use ASE Encryption and decryption
When caching data, encrypt the data , Decrypt the cached data .
For local debugging , Local data is not encrypted , Online data encryption processing
// Judge whether it is a development environment The development environment does not need encryption Online environments require encryption
hasEncrypt = process.env.NODE_ENV === "development";Use ASE Before encryption, you need to introduce ase
npm install crypto-js --save-devOf course, I use encapsulated locally ASE Tool class , You can modify the code according to the actual situation
What I use in my project is localStorage Cache encryption and decryption , You can also modify the code according to your needs
The reference codes are as follows :
/*
* @Author: wangweiruning
* @Date: 2021-12-08 17:37:34
* @Last Modified by: chenjie
* @Last Modified time: 2021-12-09 15:16:23
*/
const EncryptUtils = require('../encrypt/encryptUtils')
let hasEncrypt = process.env.NODE_ENV === "development"; // Judge whether it is a development environment The development environment does not need encryption Online environments require encryption
let timeout = 60 * 60 * 24 * 7; // Cache time The default cache 7 God
const WebStorage = class WebStorage {
hasEncrypt = false;
constructor() {
this.hasEncrypt = hasEncrypt
}
getKey(key) {
return key.toUpperCase();
}
/**
* @description Set up storage
* @param key Store property name
* @param value Store property values
* @param expire Expiration period
*/
set(key, value, expire = timeout) {
// if (!expire) expire = timeout;
const stringData = JSON.stringify({
value,
time: new Date(),
expire: !isNullOrUndef(expire) ? new Date().getTime() + expire * 1000 : null
})
const stringValue = this.hasEncrypt ? EncryptUtils.AESEncrypt(stringData) : stringData
localStorage.setItem(this.getKey(key), stringValue)
}
/**
* @description Get storage
* @param key Store property name
* @returns Stored value or null
*/
get(key, def) {
const val = localStorage.getItem(this.getKey(key));
if (!val) return def || null;
try {
const decData = this.hasEncrypt ? EncryptUtils.AESDecrypt(val) : val;
const data = JSON.parse(decData)
const { value, expire } = data;
if (isNullOrUndef(expire) || expire >= new Date().getTime()) {
return value
}
this.remove(key)
} catch (error) {
return def || null
}
}
/**
* @description Delete a storage
* @param key Store property name
*/
remove(key) {
localStorage.removeItem(this.getKey(key))
}
/**
* @description Clear all storage
*/
clear() {
localStorage.clear()
}
}
export function isNull(val) {
return val === null
}
export function isDef(val) {
return typeof val !== 'undefined'
}
export function isUnDef(val) {
return !isDef(val)
}
export function isNullOrUndef(val) {
return isUnDef(val) || isNull(val)
}
export const localS = new WebStorage();Of course, it is very convenient to use , Refer to the following code :
/*
* @Author: wangweiruning
* @Date: 2021-12-08 17:37:27
* @Last Modified by: chenjie
* @Last Modified time: 2021-12-09 11:00:23
*/
import { localS } from './local'
// Set the cache
localS.set('userInfo', { name: "wangweiruning", token: "test" }, null)
// Access to the cache
let user = localS.get('userInfo');
// Instructions
// sessionStorage and localStorage In the same way Refer to the above effect
Print :

Cache encryption :

The cache is not encrypted :

Finally, it needs to be encapsulated ASE file , Please read the next article ASE Encryption and decryption encapsulation !
边栏推荐
猜你喜欢

单片机:NEC 协议红外遥控器

Real time requirements for 5g China Unicom repeater network management protocol

微信扫描二维码无法下载文件的解决办法
![[test development] automated test selenium (II) -- common APIs for webdriver](/img/d5/bc38f0bee98b137abc1197c6e03158.png)
[test development] automated test selenium (II) -- common APIs for webdriver

Translation of ego planner papers
![[MySQL] index and transaction](/img/19/f87fee3749690902c349c42673f148.png)
[MySQL] index and transaction

Single chip microcomputer: main index of a/d (analog-to-digital conversion)

EIA map making - data processing + map making

Single chip microcomputer: basic concepts of a/d and d/a

Single chip microcomputer: a/d differential input signal
随机推荐
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-2 personal solutions
Interpretation of usb-if bc1.2 charging protocol
5g China Unicom ap:b SMS ASCII transcoding requirements
Promise combined with await
无人机避障四种常见技术中,为何大疆首选双目视觉
单片机:A/D 差分输入信号
单片机:PCF8591硬件接口
Lambda终结操作collect
[test development] file compression project practice
Talking about the wavelength of laser radar
USB-IF BC1.2充电协议解读
【愚公系列】2022年06月 .NET架构班 081-分布式中间件 ScheduleMaster的API自定义任务
SCM: introduction to Modbus communication protocol
Intervention analysis + pseudo regression
Real time requirements for 5g China Unicom repeater network management protocol
Flex layout
高等数学(第七版)同济大学 习题1-3 个人解答
微信扫描二维码无法下载文件的解决办法
Principle and control program of single chip microcomputer serial port communication
Install cnpm and use cnpm command in vscode terminal
https://blog.csdn.net/qq_39197547/article/details/121809181