1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
import CryptoJS from 'crypto-js';
const SECRET_KEY = CryptoJS.enc.Utf8.parse("3333e6e143439161");
const SECRET_IV = CryptoJS.enc.Utf8.parse("e3bbe7e3ba84431a");
const config = { type: 'localStorage', prefix: 'SDF_0.0.1', expire: 1, isEncrypt: true }
export const isSupportStorage = () => { return (typeof (Storage) !== "undefined") ? true : false }
export const setStorage = (key, value, expire = 0) => { if (value === '' || value === null || value === undefined) { value = null; } if (isNaN(expire) || expire < 0) throw new Error("Expire must be a number"); expire = (expire ? expire : config.expire) * 1000; let data = { value: value, time: Date.now(), expire: expire } const encryptString = config.isEncrypt ? encrypt(JSON.stringify(data)) : JSON.stringify(data); window[config.type].setItem(autoAddPrefix(key), encryptString); }
export const getStorage = (key) => { key = autoAddPrefix(key); if (!window[config.type].getItem(key) || JSON.stringify(window[config.type].getItem(key)) === 'null') { return null; } const storage = config.isEncrypt ? JSON.parse(decrypt(window[config.type].getItem(key))) : JSON.parse(window[config.type].getItem(key)); let nowTime = Date.now(); if (storage.expire && config.expire * 6000 < (nowTime - storage.time)) { removeStorage(key); return null; } else { setStorage(autoRemovePrefix(key), storage.value); return storage.value; } }
export const hasStorage = (key) => { key = autoAddPrefix(key); let arr = getStorageAll().filter((item)=>{ return item.key === key; }) return arr.length ? true : false; }
export const getStorageKeys = () => { let items = getStorageAll() let keys = [] for (let index = 0; index < items.length; index++) { keys.push(items[index].key) } return keys }
export const getStorageForIndex = (index) => { return window[config.type].key(index) }
export const getStorageLength = () => { return window[config.type].length }
export const getStorageAll = () => { let len = window[config.type].length let arr = new Array() for (let i = 0; i < len; i++) { let getKey = window[config.type].key(i) let getVal = window[config.type].getItem(getKey) arr[i] = {'key': getKey, 'val': getVal,} } return arr }
export const removeStorage = (key) => { window[config.type].removeItem(autoAddPrefix(key)); }
export const clearStorage = () => { window[config.type].clear(); }
const autoAddPrefix = (key) => { const prefix = config.prefix ? config.prefix + '_' : ''; return prefix + key; }
const autoRemovePrefix = (key) => { const len = config.prefix ? config.prefix.length+1 : ''; return key.substr(len) }
const encrypt = (data) => { if (typeof data === "object") { try { data = JSON.stringify(data); } catch (error) { console.log("encrypt error:", error); } } const dataHex = CryptoJS.enc.Utf8.parse(data); const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, { iv: SECRET_IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); }
const decrypt = (data) => { const encryptedHexStr = CryptoJS.enc.Hex.parse(data); const str = CryptoJS.enc.Base64.stringify(encryptedHexStr); const decrypt = CryptoJS.AES.decrypt(str, SECRET_KEY, { iv: SECRET_IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); }
|