131 lines
3.0 KiB
JavaScript
131 lines
3.0 KiB
JavaScript
import CryptoJS from "crypto-js";
|
|
import pako from "pako";
|
|
//import {gzip_encode_raw,gzip_decode_raw} from 'wasm-flate';
|
|
|
|
const wasmFlate = window.wasm_bindgen;
|
|
|
|
/**
|
|
* @word 要加密的内容
|
|
* @keyWord String 服务器随机返回的关键字
|
|
* */
|
|
|
|
export function wrap(text, secretKey) {
|
|
text = "FLAGNOTE#" + text;
|
|
let result = aesEncrypt(text, secretKey);
|
|
result = noteZip(result);
|
|
return result;
|
|
}
|
|
|
|
export function unwrap(storeText, secretKey) {
|
|
let result = noteUnzip(storeText);
|
|
result = aesDecrypt(result, secretKey);
|
|
if (result.startsWith("FLAGNOTE#")) {
|
|
return result.substring(9);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
//加密
|
|
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) {
|
|
let key = CryptoJS.enc.Utf8.parse(keyWord);
|
|
let encrypted = CryptoJS.AES.encrypt(word, key, {
|
|
mode: CryptoJS.mode.ECB,
|
|
padding: CryptoJS.pad.Pkcs7,
|
|
});
|
|
return encrypted.toString();
|
|
}
|
|
|
|
//解密
|
|
export function aesDecrypt(word, keyWord) {
|
|
|
|
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();
|
|
}
|
|
|
|
//base64 encode
|
|
export function encode(text) {
|
|
return Buffer.from(text, 'utf-8').toString('base64');
|
|
}
|
|
|
|
//base64 decode
|
|
export function decode(text) {
|
|
return Buffer.from(text,'base64').toString('utf-8');
|
|
}
|
|
|
|
export function noteUnzip(text) {
|
|
let charData = text.split(",").map(function (x) {
|
|
return parseInt(x);
|
|
});
|
|
let binData = new Uint8Array(charData);
|
|
let data = unzip(binData);
|
|
text = String.fromCharCode.apply(null, new Uint8Array(data));
|
|
return text;
|
|
}
|
|
|
|
export function noteZip(text) {
|
|
let encoder = new TextEncoder();
|
|
let data = encoder.encode(text);
|
|
text = zip(data);
|
|
return text;
|
|
}
|
|
|
|
export function zip(data){
|
|
if(wasmFlate.__wbindgen_wasm_module){
|
|
return wasmFlate.gzip_encode_raw(data);
|
|
}
|
|
|
|
return pako.gzip(data);
|
|
}
|
|
|
|
export function unzip(data){
|
|
if(wasmFlate.__wbindgen_wasm_module){
|
|
return wasmFlate.gzip_decode_raw(data);
|
|
}
|
|
|
|
return pako.ungzip(data);
|
|
}
|
|
|
|
// function convertWordArrayToUint8Array(wordArray) {
|
|
// var len = wordArray.words.length,
|
|
// u8_array = new Uint8Array(len << 2),
|
|
// offset = 0, word, i
|
|
// ;
|
|
// for (i=0; i<len; i++) {
|
|
// word = wordArray.words[i];
|
|
// u8_array[offset++] = word >> 24;
|
|
// u8_array[offset++] = (word >> 16) & 0xff;
|
|
// u8_array[offset++] = (word >> 8) & 0xff;
|
|
// u8_array[offset++] = word & 0xff;
|
|
// }
|
|
// return u8_array;
|
|
// }
|
|
|
|
// function convertUint8ArrayToWordArray(u8Array) {
|
|
// var words = [], i = 0, len = u8Array.length;
|
|
|
|
// while (i < len) {
|
|
// words.push(
|
|
// (u8Array[i++] << 24) |
|
|
// (u8Array[i++] << 16) |
|
|
// (u8Array[i++] << 8) |
|
|
// (u8Array[i++])
|
|
// );
|
|
// }
|
|
|
|
// return {
|
|
// sigBytes: words.length * 4,
|
|
// words: words
|
|
// };
|
|
// }
|