From 508807509c276695803588eee598981c24f596cc Mon Sep 17 00:00:00 2001 From: ZvonimirSun Date: Wed, 27 Dec 2023 16:07:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(hashFile):=20=E6=96=B0=E5=A2=9E=E6=96=87?= =?UTF-8?q?=E4=BB=B6hash=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components.d.ts | 1 + src/tools.json | 4 + src/views/hashFile/hashFile.service.ts | 13 +++ src/views/hashFile/hashFile.vue | 153 +++++++++++++++++++++++++ src/views/hashFile/hashFile.worker.ts | 22 ++++ src/views/hashText/hashText.vue | 4 - src/views/idChinese/idChinese.vue | 1 - 7 files changed, 193 insertions(+), 5 deletions(-) create mode 100644 src/views/hashFile/hashFile.service.ts create mode 100644 src/views/hashFile/hashFile.vue create mode 100644 src/views/hashFile/hashFile.worker.ts diff --git a/src/components.d.ts b/src/components.d.ts index d7693e242..0ed653778 100644 --- a/src/components.d.ts +++ b/src/components.d.ts @@ -29,6 +29,7 @@ declare module 'vue' { ElButton: typeof import('element-plus/es')['ElButton'] ElCard: typeof import('element-plus/es')['ElCard'] ElCheckbox: typeof import('element-plus/es')['ElCheckbox'] + ElCheckboxGroup: typeof import('element-plus/es')['ElCheckboxGroup'] ElCol: typeof import('element-plus/es')['ElCol'] ElConfigProvider: typeof import('element-plus/es')['ElConfigProvider'] ElContainer: typeof import('element-plus/es')['ElContainer'] diff --git a/src/tools.json b/src/tools.json index d699cc787..48c3a8373 100644 --- a/src/tools.json +++ b/src/tools.json @@ -47,6 +47,10 @@ "name": "文本HASH", "link": "/hashText" }, + { + "name": "文件HASH", + "link": "/hashFile" + }, { "name": "在线JWT解密", "link": "/jwt" diff --git a/src/views/hashFile/hashFile.service.ts b/src/views/hashFile/hashFile.service.ts new file mode 100644 index 000000000..9bbeae5aa --- /dev/null +++ b/src/views/hashFile/hashFile.service.ts @@ -0,0 +1,13 @@ +import { MD5, SHA1, SHA256, SHA3, SHA512 } from 'crypto-js' + +export const algos = { + MD5, + SHA1, + SHA256, + SHA512, + SHA3 +} as const + +export type AlgoNames = keyof typeof algos; + +export const algoNames = Object.keys(algos) as AlgoNames[] diff --git a/src/views/hashFile/hashFile.vue b/src/views/hashFile/hashFile.vue new file mode 100644 index 000000000..5e931536e --- /dev/null +++ b/src/views/hashFile/hashFile.vue @@ -0,0 +1,153 @@ + + + diff --git a/src/views/hashFile/hashFile.worker.ts b/src/views/hashFile/hashFile.worker.ts new file mode 100644 index 000000000..bcd1ac547 --- /dev/null +++ b/src/views/hashFile/hashFile.worker.ts @@ -0,0 +1,22 @@ +import { lib } from 'crypto-js' +import { algos, type AlgoNames } from './hashFile.service' + +const hashFile = (algo: AlgoNames, value: lib.WordArray) => algos[algo](value).toString() + +self.addEventListener('message', function (event) { + if (event.data && event.data.value && event.data.value instanceof File) { + const algos = (event.data.algos || ['MD5']) as AlgoNames[] + const reader = new FileReader() + reader.readAsArrayBuffer(event.data.value) + reader.onload = function () { + const wordArray = lib.WordArray.create(reader.result as unknown as number[]) + const result = {} as { [key in AlgoNames]: string } + for (const algo of algos) { + result[algo] = hashFile(algo, wordArray) + self.postMessage(result) + } + } + } else { + self.postMessage(null) + } +}) diff --git a/src/views/hashText/hashText.vue b/src/views/hashText/hashText.vue index d7be35e2b..e90489df1 100644 --- a/src/views/hashText/hashText.vue +++ b/src/views/hashText/hashText.vue @@ -115,7 +115,3 @@ function calculate () { - - diff --git a/src/views/idChinese/idChinese.vue b/src/views/idChinese/idChinese.vue index 13e63234d..8b533684a 100644 --- a/src/views/idChinese/idChinese.vue +++ b/src/views/idChinese/idChinese.vue @@ -66,4 +66,3 @@ async function analyse () { } } -