Browse Source

更换计算hash方法

cynthia-qin 1 month ago
parent
commit
07b3879af8
2 changed files with 20 additions and 6 deletions
  1. 1 1
      .env.development
  2. 19 5
      src/dialogs/OCRDialog.vue

+ 1 - 1
.env.development

@@ -1,2 +1,2 @@
 NODE_ENV = development
-VITE_API_URL = http://192.18.1.235:8000
+VITE_API_URL = http://173.18.12.205:8005

+ 19 - 5
src/dialogs/OCRDialog.vue

@@ -418,11 +418,25 @@ const handleRemove = (file: any, fileList: any) => {
 }
 
 async function calculateFileHash(file: File) {
-  const arrayBuffer = await file.arrayBuffer();
-  const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
-  const hashArray = Array.from(new Uint8Array(hashBuffer));
-  const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
-  return hashHex;
+  // const arrayBuffer = await file.arrayBuffer();
+  // const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
+  // const hashArray = Array.from(new Uint8Array(hashBuffer));
+  // const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
+  // return hashHex;
+    // 使用 crypto-js 计算哈希值
+  return new Promise((resolve, reject) => {
+    const reader = new FileReader();
+    reader.readAsArrayBuffer(file);
+    reader.onload = function (e) {
+      const arrayBuffer = e.target?.result;
+      const wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
+      const hashHex = CryptoJS.SHA256(wordArray).toString(CryptoJS.enc.hex);
+      resolve(hashHex);
+    };
+    reader.onerror = () => {
+      reject('');
+    };
+  });
 }
 
 function handleImportFiles(filesList: any[]) {