This commit is contained in:
Jesse-Ma
2023-03-30 10:19:18 +08:00
parent 0de98e660b
commit f369e59f6f
3 changed files with 75 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
import axios from "axios"; import axios from "axios";
import { getStoreKey } from "@/api/lock"; import { getStoreKey } from "@/api/lock";
import storage from "@/libs/storage"; import storage from "@/libs/storage";
import { md5, wrap} from "@/libs/secret"; import { md5, wrap,convertBase64ToHexString,convertHexStringToUint8Array} from "@/libs/secret";
import NoteConstant from "@/libs/constants"; import NoteConstant from "@/libs/constants";
axios.interceptors.response.use(undefined, (err) => { axios.interceptors.response.use(undefined, (err) => {
@@ -51,8 +51,11 @@ export function saveNote(noteForm, secret) {
// let bufferArrary = eval("[" + note.text + "]"); // let bufferArrary = eval("[" + note.text + "]");
// let array = Uint8Array.from(bufferArrary); // let array = Uint8Array.from(bufferArrary);
let encoder = new TextEncoder() // let encoder = new TextEncoder()
let array = encoder.encode(note.text) // let array = encoder.encode(note.text)
let hexString = convertBase64ToHexString(note.text)
let array = convertHexStringToUint8Array(hexString)
let blob = new Blob([array], { type: "application/octet-stream" }); let blob = new Blob([array], { type: "application/octet-stream" });
let form = new FormData(); let form = new FormData();

View File

@@ -4,6 +4,41 @@ import pako from "pako";
const wasmFlate = window.wasm_bindgen; const wasmFlate = window.wasm_bindgen;
CryptoJS.enc.Uint8Array = {
/**
* WordArray转Uint8Array
* @param wordArray
* @returns {Uint8Array}
*/
stringify: function (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;
},
/**
* Uint8Array转WordArray
* @param u8arr
* @returns {WordArray}
*/
parse: function (u8arr) {
var len = u8arr.length;
var words = [];
for (var i = 0; i < len; i++) {
words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
}
return CryptoJS.lib.WordArray.create(words, len);
}
};
/** /**
* @word 要加密的内容 * @word 要加密的内容
* @keyWord String 服务器随机返回的关键字 * @keyWord String 服务器随机返回的关键字
@@ -92,35 +127,33 @@ export function unzip(data){
return pako.ungzip(data); return pako.ungzip(data);
} }
export function convertBase64ToHexString(base64) {
return CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(base64));
}
export function convertHexStringToBase64(hexString) {
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(hexString));
}
export function convertHexStringToUint8Array(hexString) {
var result = [];
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
}
return Uint8Array.from(result);
}
export function convertUint8ArrayToHexString(byteArray) {
return Array.prototype.map.call(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function convertWordArrayToUint8Array(wordArray) { function convertWordArrayToUint8Array(wordArray) {
var len = wordArray.words.length, return CryptoJS.enc.Uint8Array.stringify(wordArray);
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) { function convertUint8ArrayToWordArray(u8Array) {
var words = [], i = 0, len = u8Array.length; return CryptoJS.enc.Uint8Array.parse(u8Array);
while (i < len) {
words.push(
(u8Array[i++] << 24) |
(u8Array[i++] << 16) |
(u8Array[i++] << 8) |
(u8Array[i++])
);
}
return {
sigBytes: words.length * 4,
words: words
};
} }

View File

@@ -336,7 +336,7 @@
<script> <script>
import { md5, unwrap } from "@/libs/secret"; import { md5, unwrap,convertUint8ArrayToHexString,convertHexStringToBase64 } from "@/libs/secret";
import { getStoreKey } from "@/api/lock"; import { getStoreKey } from "@/api/lock";
import { deleteNote, getNoteBlob } from "@/api/note"; import { deleteNote, getNoteBlob } from "@/api/note";
import storage from "@/libs/storage"; import storage from "@/libs/storage";
@@ -589,14 +589,18 @@ export default {
return; return;
} }
let decoder = new TextDecoder(); // let decoder = new TextDecoder();
let bytesString = decoder.decode(e.target.result); // let bytesString = decoder.decode(e.target.result);
that.noteForm.text = unwrap(bytesString, that.secret.secretKey);
let hexString = convertUint8ArrayToHexString(new Uint8Array(e.target.result));
let base64String = convertHexStringToBase64(hexString);
that.noteForm.text = unwrap(base64String, that.secret.secretKey);
//that.noteForm.escapeText = getEscapeText(that.noteForm.text); //that.noteForm.escapeText = getEscapeText(that.noteForm.text);
// if local is enough, set local // if local is enough, set local
if (storage.local.getAvailableSize() > 1 * 1024 * 1024) { if (storage.local.getAvailableSize() > 1 * 1024 * 1024) {
storage.local.setText(that.secret.storeKey, that.state.lock + '|' + that.secret.cipher + '|1|' + that.state.serverTime + '|' + bytesString); storage.local.setText(that.secret.storeKey, that.state.lock + '|' + that.secret.cipher + '|1|' + that.state.serverTime + '|' + base64String);
} }
}; };
reader.readAsArrayBuffer(blob); reader.readAsArrayBuffer(blob);