diff --git a/.gitignore b/.gitignore index e70f3b8..a6a8e16 100644 --- a/.gitignore +++ b/.gitignore @@ -32,8 +32,8 @@ coverage # files created with miaoShare service temp miaoShare.db -addUser.js -removeUser.js +# addUser.js +# removeUser.js # JWT SK *.key \ No newline at end of file diff --git a/addUser.js b/addUser.js new file mode 100644 index 0000000..fb7b1c6 --- /dev/null +++ b/addUser.js @@ -0,0 +1,16 @@ +import sha256 from 'crypto-js/sha256.js'; +import Database from "better-sqlite3"; + + +const dbPath = "./miaoShare.db"; + +const db = new Database(dbPath, { verbose: null }); +const stmt_insertId2FileName = db.prepare(`INSERT INTO USERS (user_id, password_hash, user_group, uploadFilesSizeCount) VALUES (?, ?, ?, 0)`); + +let userList = [ + ['539943419','zjh030628','admin'], + // ['guest','guest','guest'] +].map(a=>{a[1]=sha256(a[0]+sha256(a[1]).toString()).toString();return a}) +for(let i of userList){ + stmt_insertId2FileName.run(...i) +} \ No newline at end of file diff --git a/removeUser.js b/removeUser.js new file mode 100644 index 0000000..8db0386 --- /dev/null +++ b/removeUser.js @@ -0,0 +1,15 @@ +import sha256 from 'crypto-js/sha256.js'; +import Database from "better-sqlite3"; + + +const dbPath = "./miaoShare.db"; + +const db = new Database(dbPath, { verbose: null }); +const stmt_ = db.prepare(`DELETE FROM USERS WHERE user_id = ?`); + +let userList = [ + ['admin'],["guest"] +] +for(let i of userList){ + stmt_.run(...i) +} \ No newline at end of file diff --git a/src/func/SM4.js b/src/func/SM4.js index 206e72f..4fe970a 100644 --- a/src/func/SM4.js +++ b/src/func/SM4.js @@ -1,4 +1,5 @@ import sm4_encrypt_worker from '@/worker/sm4_encrypt?worker' +import sm4_decrypt_worker from '@/worker/sm4_decrypt?worker' const SM4 = { encrypt: async (key, data) => { @@ -12,14 +13,14 @@ const SM4 = { return encryptedBytes }, decrypt: async (key, data) => { - const encryptedBytes = await new Promise((resolve) => { - const sm4Worker = new sm4_encrypt_worker() + const decryptedBytes = await new Promise((resolve) => { + const sm4Worker = new sm4_decrypt_worker() sm4Worker.onmessage = (event) => { resolve(event.data) } sm4Worker.postMessage({ data, key }) }) - return encryptedBytes + return decryptedBytes } } diff --git a/src/func/upload.js b/src/func/upload.js index 93dfe71..cc00197 100644 --- a/src/func/upload.js +++ b/src/func/upload.js @@ -1,55 +1,55 @@ -import aesjs from 'aes-js' -import { Base64 } from 'js-base64' -import { sm4 } from 'sm-crypto' -import AES from './AES' -import SM4 from './SM4' +import aesjs from "aes-js"; +import { Base64 } from "js-base64"; +import { sm4 } from "sm-crypto"; +import AES from "./AES"; +import SM4 from "./SM4"; // const setting = useSettingStore(); // const message = useMessage(); // 生成密钥 const generateKey = function (setting) { - if (setting.activeFuction === '不加密') { - return '' - } - var charMap = '' - for (let i of setting.activeKeyMap) { - if (i === 'A-Z') { - charMap = charMap + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' - } else if (i === 'a-z') { - charMap = charMap + 'abcdefghijklmnopqrstuvwxyz' - } else if (i === '0-9') { - charMap = charMap + '0123456789' - } - } - var returnStr = '', - length = setting.activeFuction !== 'AES256' ? 16 : 32 - // charMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + if (setting.activeFuction === "不加密") { + return ""; + } + var charMap = ""; + for (let i of setting.activeKeyMap) { + if (i === "A-Z") { + charMap = charMap + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + } else if (i === "a-z") { + charMap = charMap + "abcdefghijklmnopqrstuvwxyz"; + } else if (i === "0-9") { + charMap = charMap + "0123456789"; + } + } + var returnStr = "", + length = setting.activeFuction !== "AES256" ? 16 : 32; + // charMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - for (var i = 0; i < length; i++) { - var index = parseInt(Math.random() * charMap.length) - returnStr += charMap.substring(index, index + 1) - } - if (setting.activeFuction === 'SM4') { - returnStr = 'SM4' + returnStr - } - // console.log(returnStr,returnStr.length) - return returnStr -} + for (var i = 0; i < length; i++) { + var index = parseInt(Math.random() * charMap.length); + returnStr += charMap.substring(index, index + 1); + } + if (setting.activeFuction === "SM4") { + returnStr = "SM4" + returnStr; + } + // console.log(returnStr,returnStr.length) + return returnStr; +}; const blob2Uint8Array = function (blob) { - return new Promise((resolve, reject) => { - let reader = new FileReader() - reader.readAsArrayBuffer(blob) - reader.onload = function () { - let buffer = reader.result - let uint8array = new Uint8Array(buffer) - resolve(uint8array) - } - reader.onerror = function () { - reject(reader.error) - } - }) -} + return new Promise((resolve, reject) => { + let reader = new FileReader(); + reader.readAsArrayBuffer(blob); + reader.onload = function () { + let buffer = reader.result; + let uint8array = new Uint8Array(buffer); + resolve(uint8array); + }; + reader.onerror = function () { + reject(reader.error); + }; + }); +}; // const AES = (key, data) => { // key = aesjs.utils.utf8.toBytes(key) @@ -74,65 +74,65 @@ const blob2Uint8Array = function (blob) { // return new Uint8Array(encryptedBytes) // } const encrypt = (key, data, fileName, setting, message) => { - // if(!encryptFunction){ - // encryptFunction = setting.activeFuction - // } - let encryptFunction = { - 0: '不加密', - 16: 'AES128', - 19: 'SM4', - 32: 'AES256' - }[key.length] - return blob2Uint8Array(data).then(async (data) => { - let encrypted_data = null - if (encryptFunction === '不加密' || key === '') { - encrypted_data = data - // AES - } else if (encryptFunction != 'SM4') { - if (message) message.info(`开始加密${fileName}`) - encrypted_data = await AES(key, data) - if (message) message.info(`${fileName}加密完成`) - // SM4 - } else { - if (message) message.info(`开始加密${fileName}`) - key = [...aesjs.utils.utf8.toBytes(key.substring(3))] - encrypted_data = await SM4.encrypt(key, data) - if (message) message.info(`${fileName}加密完成`) - } - console.log(encrypted_data) - return encrypted_data - }) -} + // if(!encryptFunction){ + // encryptFunction = setting.activeFuction + // } + let encryptFunction = { + 0: "不加密", + 16: "AES128", + 19: "SM4", + 32: "AES256" + }[key.length]; + return blob2Uint8Array(data).then(async data => { + let encrypted_data = null; + if (encryptFunction === "不加密" || key === "") { + encrypted_data = data; + // AES + } else if (encryptFunction != "SM4") { + if (message) message.info(`开始加密${fileName}`); + encrypted_data = await AES(key, data); + if (message) message.info(`${fileName}加密完成`); + // SM4 + } else { + if (message) message.info(`开始加密${fileName}`); + key = [...aesjs.utils.utf8.toBytes(key.substring(3))]; + encrypted_data = await SM4.encrypt(key, data); + if (message) message.info(`${fileName}加密完成`); + } + console.log(encrypted_data); + return encrypted_data; + }); +}; // setting为pinia存储的setting。。。 const encryptString = (key, str, setting) => { - // if(!encryptFunction){ - // encryptFunction = setting.activeFuction - // } - // console.log(key,key.length) - let encryptFunction = { - 0: '不加密', - 16: 'AES128', - 19: 'SM4', - 32: 'AES256' - }[key.length] - if (encryptFunction === '不加密' || key === '') { - // return sm4.encrypt(str, 'miaospringmiaospringmiaospringao') - return Base64.encode(str) - } else if (encryptFunction !== 'SM4') { - key = aesjs.utils.utf8.toBytes(key) - // var data = aesjs.utils.utf8.toBytes(str); - var data = new TextEncoder().encode(str) - var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5)) - var encryptedBytes = aesCtr.encrypt(data) - var res = Base64.fromUint8Array(encryptedBytes, true) - // console.log(encryptedBytes,res) - return res - } else { - key = [...aesjs.utils.utf8.toBytes(key.substring(3))] - // console.log(key) - return sm4.encrypt(str, key) - } -} + // if(!encryptFunction){ + // encryptFunction = setting.activeFuction + // } + // console.log(key,key.length) + let encryptFunction = { + 0: "不加密", + 16: "AES128", + 19: "SM4", + 32: "AES256" + }[key.length]; + if (encryptFunction === "不加密" || key === "") { + // return sm4.encrypt(str, 'miaospringmiaospringmiaospringao') + return Base64.encode(str); + } else if (encryptFunction !== "SM4") { + key = aesjs.utils.utf8.toBytes(key); + // var data = aesjs.utils.utf8.toBytes(str); + var data = new TextEncoder().encode(str); + var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5)); + var encryptedBytes = aesCtr.encrypt(data); + var res = Base64.fromUint8Array(encryptedBytes, true); + // console.log(encryptedBytes,res) + return res; + } else { + key = [...aesjs.utils.utf8.toBytes(key.substring(3))]; + // console.log(key) + return sm4.encrypt(str, key); + } +}; -export { encrypt, encryptString, generateKey } +export { encrypt, encryptString, generateKey };