This commit is contained in:
Marvin 2023-07-14 00:13:07 +00:00 committed by GitHub
commit 59be5974d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 203 additions and 1 deletions

1
components.d.ts vendored
View file

@ -51,6 +51,7 @@ declare module '@vue/runtime-core' {
Encryption: typeof import('./src/tools/encryption/encryption.vue')['default']
EtaCalculator: typeof import('./src/tools/eta-calculator/eta-calculator.vue')['default']
FavoriteButton: typeof import('./src/components/FavoriteButton.vue')['default']
FileHash: typeof import('./src/tools/file-hash/file-hash.vue')['default']
FormatTransformer: typeof import('./src/components/FormatTransformer.vue')['default']
GitMemo: typeof import('./src/tools/git-memo/git-memo.vue')['default']
'GitMemo.content': typeof import('./src/tools/git-memo/git-memo.content.md')['default']

View file

@ -0,0 +1,188 @@
<script setup lang="ts">
import { type Ref, ref, watch } from 'vue';
import type { UploadFileInfo } from 'naive-ui';
import { Upload } from '@vicons/tabler';
import CryptoJS from 'crypto-js';
import { useCopy } from '@/composable/copy';
type Hasher = 'md5' |
'sha1' |
'sha2_224' |
'sha2_256' |
'sha2_384' |
'sha2_512' |
'sha3_224' |
'sha3_256' |
'sha3_384' |
'sha3_512';
type Digest = 'base64' | 'hex';
function useFileHash(target: Ref<File>, hasher: Ref<Hasher>, digest: Ref<Digest>) {
const hash = ref('');
const promise = ref() as Ref<Promise<string>>;
function execute() {
if (typeof window === 'undefined') {
return;
}
promise.value = new Promise<string>((resolve, reject) => {
try {
if (target.value) {
resolve(readAndHashFile(target.value, hasher.value, digest.value));
}
}
catch (error) {
reject(error);
}
});
promise.value.then(res => hash.value = res);
return promise.value;
}
watch(target, execute, { immediate: true });
watch(hasher, execute, { immediate: true });
watch(digest, execute, { immediate: true });
return {
hash,
promise,
execute,
};
}
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;
const hasher: Record<Hasher, typeof CryptoJS.MD5> = {
md5: CryptoJS.MD5,
sha1: CryptoJS.SHA1,
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 }),
};
const digest = {
base64: CryptoJS.enc.Base64,
hex: CryptoJS.enc.Hex,
};
resolve(hasher[hashType](content).toString(digest[digestType]));
};
fr.onerror = reject;
fr.readAsBinaryString(file);
});
}
const fileList = ref();
const fileInput = ref() as Ref<File>;
const hasher = ref<Hasher>('md5');
const digest = ref<Digest>('hex');
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>
<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',
},
{
label: 'SHA1',
value: 'sha1',
},
{
label: 'SHA2-224',
value: 'sha2_224',
},
{
label: 'SHA2-256',
value: 'sha2_256',
},
{
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',
},
]"
/>
</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>

View file

@ -0,0 +1,12 @@
import { Key } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'File hash',
path: '/file-hash',
description: 'Calculate file hashes',
keywords: ['file', 'hash', 'md5', 'sha', 'crypto'],
component: () => import('./file-hash.vue'),
icon: Key,
createdAt: new Date('2023-07-13'),
});

View file

@ -1,6 +1,7 @@
import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as fileHash } from './file-hash';
import { tool as passwordStrengthAnalyser } from './password-strength-analyser';
import { tool as yamlToToml } from './yaml-to-toml';
import { tool as jsonToToml } from './json-to-toml';
@ -69,7 +70,7 @@ import { tool as xmlFormatter } from './xml-formatter';
export const toolsByCategory: ToolCategory[] = [
{
name: 'Crypto',
components: [tokenGenerator, hashText, bcrypt, uuidGenerator, cypher, bip39, hmacGenerator, rsaKeyPairGenerator, passwordStrengthAnalyser],
components: [fileHash, tokenGenerator, hashText, bcrypt, uuidGenerator, cypher, bip39, hmacGenerator, rsaKeyPairGenerator, passwordStrengthAnalyser],
},
{
name: 'Converter',