tag1
This commit is contained in:
44
src/libs/noteStorage.js
Normal file
44
src/libs/noteStorage.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import {zip, aesEncrypt} from '../libs/secret'
|
||||
import storage from "@/libs/storage";
|
||||
import {getSecretKey} from "@/api/lock";
|
||||
import escapeHtml from "escape-html";
|
||||
|
||||
export function setStoreText(text, secret, password) {
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
password = "";
|
||||
}
|
||||
|
||||
text = "FLAGNOTE#" + text;
|
||||
|
||||
let secretKey = getSecretKey(secret.cipher, password);
|
||||
let storeText = aesEncrypt(text, secretKey);
|
||||
storeText = zip(storeText);
|
||||
|
||||
console.log("sssssssss" + secret.cipher)
|
||||
console.log("sssssssss" + password)
|
||||
console.log("sssssssss" + secretKey)
|
||||
console.log("s" + storeText)
|
||||
|
||||
let lock = '0';
|
||||
if (password) {
|
||||
lock = '1';
|
||||
}
|
||||
|
||||
storage.local.setText(secret.storeKey + '.text', lock + '|' + secret.cipher + '|' + storeText);
|
||||
}
|
||||
|
||||
|
||||
export function getEscapeText(text) {
|
||||
let textEscape = escapeHtml(text);
|
||||
textEscape = textEscape.replaceAll(" ", " ");
|
||||
textEscape = textEscape.replaceAll("\r\n", "<br/>");
|
||||
textEscape = textEscape.replaceAll("\r", "<br/>");
|
||||
textEscape = textEscape.replaceAll("\n", "<br/>");
|
||||
textEscape = textEscape.replaceAll("\t", " ");
|
||||
|
||||
return textEscape;
|
||||
}
|
||||
57
src/libs/secret.js
Normal file
57
src/libs/secret.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import CryptoJS from 'crypto-js'
|
||||
import pako from 'pako'
|
||||
|
||||
/**
|
||||
* @word 要加密的内容
|
||||
* @keyWord String 服务器随机返回的关键字
|
||||
* */
|
||||
|
||||
//加密
|
||||
export function md5(word, keyWord = 'F1agn0te') {
|
||||
let srcWords = CryptoJS.enc.Utf8.parse(word + '_' + keyWord);
|
||||
let encrypted = CryptoJS.MD5(srcWords);
|
||||
return encrypted.toString();
|
||||
}
|
||||
|
||||
//加密
|
||||
export function aesEncrypt(word, keyWord = 'F1agn0te') {
|
||||
let key = CryptoJS.enc.Utf8.parse(keyWord);
|
||||
let srcWords = CryptoJS.enc.Utf8.parse(word);
|
||||
let encrypted = CryptoJS.AES.encrypt(srcWords, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
|
||||
return encrypted.toString();
|
||||
}
|
||||
|
||||
//解密
|
||||
export function aesDecrypt(word, keyWord = 'F1agn0te') {
|
||||
let key = CryptoJS.enc.Utf8.parse(keyWord);
|
||||
let decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
|
||||
return CryptoJS.enc.Utf8.stringify(decrypt).toString()
|
||||
}
|
||||
|
||||
export function encode(text) {
|
||||
return btoa(encodeURIComponent(text))
|
||||
}
|
||||
|
||||
export function decode(text) {
|
||||
return decodeURIComponent(atob(text))
|
||||
}
|
||||
|
||||
export function unzip(text) {
|
||||
let charData = text.split(',').map(function (x) {
|
||||
return parseInt(x)
|
||||
});
|
||||
let binData = new Uint8Array(charData);
|
||||
let data = pako.ungzip(binData);
|
||||
//text = String.fromCharCode.apply(null, new Uint8Array(data));
|
||||
text = new Uint8Array(data).reduce(function (data, byte) {
|
||||
return data + String.fromCharCode(byte);
|
||||
}, '');
|
||||
return text;
|
||||
}
|
||||
|
||||
export function zip(text) {
|
||||
text = pako.gzip(text, {to: 'string'});
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
46
src/libs/storage.js
Normal file
46
src/libs/storage.js
Normal file
@@ -0,0 +1,46 @@
|
||||
class Storage {
|
||||
constructor() {
|
||||
this.session = new Session();
|
||||
this.local = new Local();
|
||||
}
|
||||
}
|
||||
|
||||
class Session {
|
||||
|
||||
setObject(key, value) {
|
||||
sessionStorage.setItem(key, JSON.stringify(value));
|
||||
}
|
||||
|
||||
getObject(key) {
|
||||
return JSON.parse(sessionStorage.getItem(key));
|
||||
}
|
||||
|
||||
setText(key, value) {
|
||||
sessionStorage.setItem(key, value);
|
||||
}
|
||||
|
||||
getText(key) {
|
||||
return sessionStorage.getItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
class Local {
|
||||
setObject(key, value) {
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
}
|
||||
|
||||
getObject(key) {
|
||||
return JSON.parse(localStorage.getItem(key));
|
||||
}
|
||||
|
||||
setText(key, value) {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
|
||||
getText(key) {
|
||||
return localStorage.getItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
const storage = new Storage();
|
||||
export default storage;
|
||||
Reference in New Issue
Block a user