it-tools/src/tools/file-hash/file-hash.vue

189 lines
4.7 KiB
Vue
Raw Normal View History

2023-07-14 01:55:02 +02:00
<script setup lang="ts">
2023-07-14 01:56:13 +02:00
import { type Ref, ref, watch } from 'vue';
2023-07-14 01:55:02 +02:00
import type { UploadFileInfo } from 'naive-ui';
import { Upload } from '@vicons/tabler';
2023-07-14 01:56:13 +02:00
import CryptoJS from 'crypto-js';
import { useCopy } from '@/composable/copy';
2023-07-14 01:55:02 +02:00
2023-07-14 02:05:20 +02:00
type Hasher = 'md5' |
'sha1' |
'sha2_224' |
'sha2_256' |
'sha2_384' |
'sha2_512' |
'sha3_224' |
'sha3_256' |
'sha3_384' |
'sha3_512';
2023-07-14 01:56:13 +02:00
type Digest = 'base64' | 'hex';
2023-07-14 01:55:02 +02:00
function useFileHash(target: Ref<File>, hasher: Ref<Hasher>, digest: Ref<Digest>) {
const hash = ref('');
2023-07-14 01:56:13 +02:00
const promise = ref() as Ref<Promise<string>>;
2023-07-14 01:55:02 +02:00
function execute() {
2023-07-14 01:56:13 +02:00
if (typeof window === 'undefined') {
return;
2023-07-14 01:55:02 +02:00
}
promise.value = new Promise<string>((resolve, reject) => {
try {
if (target.value) {
resolve(readAndHashFile(target.value, hasher.value, digest.value));
}
2023-07-14 01:56:13 +02:00
}
catch (error) {
2023-07-14 01:55:02 +02:00
reject(error);
}
});
2023-07-14 01:56:13 +02:00
promise.value.then(res => hash.value = res);
2023-07-14 01:55:02 +02:00
return promise.value;
}
watch(target, execute, { immediate: true });
watch(hasher, execute, { immediate: true });
watch(digest, execute, { immediate: true });
return {
hash,
promise,
execute,
2023-07-14 01:56:13 +02:00
};
2023-07-14 01:55:02 +02:00
}
function readAndHashFile(file: File, hashType: Hasher, digestType: Digest) {
return new Promise<string>((resolve, reject) => {
const fr = new FileReader();
fr.onload = (ev) => {
const content = ev.target!.result as string;
2023-07-14 02:05:20 +02:00
const hasher: Record<Hasher, typeof CryptoJS.MD5> = {
2023-07-14 01:55:02 +02:00
md5: CryptoJS.MD5,
sha1: CryptoJS.SHA1,
2023-07-14 02:05:20 +02:00
sha2_224: CryptoJS.SHA224,
sha2_256: CryptoJS.SHA256,
sha2_384: CryptoJS.SHA384,
sha2_512: CryptoJS.SHA512,
sha3_224: () => CryptoJS.SHA3(content, { outputLength: 224 }),
sha3_256: () => CryptoJS.SHA3(content, { outputLength: 256 }),
sha3_384: () => CryptoJS.SHA3(content, { outputLength: 384 }),
sha3_512: () => CryptoJS.SHA3(content, { outputLength: 512 }),
2023-07-14 01:56:13 +02:00
};
2023-07-14 01:55:02 +02:00
const digest = {
base64: CryptoJS.enc.Base64,
2023-07-14 01:56:13 +02:00
hex: CryptoJS.enc.Hex,
2023-07-14 01:55:02 +02:00
};
resolve(hasher[hashType](content).toString(digest[digestType]));
};
fr.onerror = reject;
fr.readAsBinaryString(file);
2023-07-14 01:56:13 +02:00
});
2023-07-14 01:55:02 +02:00
}
const fileList = ref();
const fileInput = ref() as Ref<File>;
2023-07-14 01:56:13 +02:00
const hasher = ref<Hasher>('md5');
const digest = ref<Digest>('hex');
2023-07-14 01:55:02 +02:00
const { hash } = useFileHash(fileInput, hasher, digest);
const { copy: copyHash } = useCopy({ source: hash, text: 'File hash copied to the clipboard' });
async function onUpload({ file: { file } }: { file: UploadFileInfo }) {
if (file) {
fileList.value = [];
fileInput.value = file;
}
}
</script>
2023-07-14 01:56:13 +02:00
<template>
<c-card title="Calculate file hash">
<n-upload v-model:file-list="fileList" :show-file-list="true" :on-before-upload="onUpload" list-type="image">
<n-upload-dragger>
<div mb-2>
<n-icon size="35" :depth="3" :component="Upload" />
</div>
<div op-60>
Click or drag a file to this area to upload
</div>
</n-upload-dragger>
</n-upload>
<n-form-item mt-5 label="Hash algorithm:" :show-feedback="false">
<n-select
v-model:value="hasher" mb-5 :options="[
{
label: 'MD5',
value: 'md5',
},
{
2023-07-14 02:05:20 +02:00
label: 'SHA1',
2023-07-14 01:56:13 +02:00
value: 'sha1',
},
{
2023-07-14 02:05:20 +02:00
label: 'SHA2-224',
value: 'sha2_224',
2023-07-14 01:56:13 +02:00
},
{
2023-07-14 02:05:20 +02:00
label: 'SHA2-256',
value: 'sha2_256',
2023-07-14 01:56:13 +02:00
},
{
2023-07-14 02:05:20 +02:00
label: 'SHA2-384',
value: 'sha2_384',
},
{
label: 'SHA2-512',
value: 'sha2_512',
},
{
label: 'SHA3-224',
value: 'sha3_224',
},
{
label: 'SHA3-256',
value: 'sha3_256',
},
{
label: 'SHA3-384',
value: 'sha3_384',
},
{
label: 'SHA3-512',
value: 'sha3_512',
2023-07-14 01:56:13 +02:00
},
]"
/>
</n-form-item>
<n-form-item label="Digest encoding:" :show-feedback="false">
<n-select
v-model:value="digest" mb-5 :options="[
{
label: 'Hexadecimal (base 16)',
value: 'hex',
},
{
label: 'Base64 (base 64)',
value: 'base64',
},
]"
/>
</n-form-item>
<n-form-item label="Your file's hash:" :show-feedback="false">
<c-input-text :value="hash" multiline readonly placeholder="Result" rows="5" mb-2 />
</n-form-item>
<div flex justify-center>
<c-button :disabled="!hash.length" @click="copyHash()">
Copy
</c-button>
</div>
</c-card>
</template>