mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-26 09:46:15 -04:00
feat(hash-text): digest base selector (#254)
This commit is contained in:
parent
fad4833ca2
commit
422b6eb05a
3 changed files with 62 additions and 7 deletions
15
src/tools/hash-text/hash-text.service.test.ts
Normal file
15
src/tools/hash-text/hash-text.service.test.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { convertHexToBin } from './hash-text.service';
|
||||
|
||||
describe('hash text', () => {
|
||||
describe('convertHexToBin', () => {
|
||||
it('convert hex to bin', () => {
|
||||
expect(convertHexToBin('')).toEqual('');
|
||||
expect(convertHexToBin('FF')).toEqual('11111111');
|
||||
expect(convertHexToBin('F'.repeat(200))).toEqual('1111'.repeat(200));
|
||||
expect(convertHexToBin('2123006AD00F694CE120')).toEqual(
|
||||
'00100001001000110000000001101010110100000000111101101001010011001110000100100000',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
7
src/tools/hash-text/hash-text.service.ts
Normal file
7
src/tools/hash-text/hash-text.service.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
export function convertHexToBin(hex: string) {
|
||||
return hex
|
||||
.trim()
|
||||
.split('')
|
||||
.map((byte) => parseInt(byte, 16).toString(2).padStart(4, '0'))
|
||||
.join('');
|
||||
}
|
|
@ -1,10 +1,34 @@
|
|||
<template>
|
||||
<div>
|
||||
<n-card>
|
||||
<n-input v-model:value="clearText" type="textarea" placeholder="Your string..." :autosize="{ minRows: 3 }" />
|
||||
<n-input v-model:value="clearText" type="textarea" placeholder="Your string to hash..." rows="3" />
|
||||
|
||||
<n-divider />
|
||||
|
||||
<n-form-item label="Digest encoding">
|
||||
<n-select
|
||||
v-model:value="encoding"
|
||||
:options="[
|
||||
{
|
||||
label: 'Binary (base 2)',
|
||||
value: 'Bin',
|
||||
},
|
||||
{
|
||||
label: 'Hexadecimal (base 16)',
|
||||
value: 'Hex',
|
||||
},
|
||||
{
|
||||
label: 'Base64 (base 64)',
|
||||
value: 'Base64',
|
||||
},
|
||||
{
|
||||
label: 'Base64url (base 64 with url safe chars)',
|
||||
value: 'Base64url',
|
||||
},
|
||||
]"
|
||||
/>
|
||||
</n-form-item>
|
||||
|
||||
<div v-for="algo in algoNames" :key="algo" style="margin: 5px 0">
|
||||
<n-input-group>
|
||||
<n-input-group-label style="flex: 0 0 120px"> {{ algo }} </n-input-group-label>
|
||||
|
@ -16,9 +40,10 @@
|
|||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import InputCopyable from '../../components/InputCopyable.vue';
|
||||
import { enc, lib, MD5, RIPEMD160, SHA1, SHA224, SHA256, SHA3, SHA384, SHA512 } from 'crypto-js';
|
||||
import { ref } from 'vue';
|
||||
import { MD5, SHA1, SHA256, SHA224, SHA512, SHA384, SHA3, RIPEMD160 } from 'crypto-js';
|
||||
import InputCopyable from '../../components/InputCopyable.vue';
|
||||
import { convertHexToBin } from './hash-text.service';
|
||||
|
||||
const algos = {
|
||||
MD5,
|
||||
|
@ -32,10 +57,18 @@ const algos = {
|
|||
} as const;
|
||||
|
||||
type AlgoNames = keyof typeof algos;
|
||||
type Encoding = keyof typeof enc | 'Bin';
|
||||
const algoNames = Object.keys(algos) as AlgoNames[];
|
||||
const encoding = ref<Encoding>('Hex');
|
||||
const clearText = ref('');
|
||||
|
||||
const clearText = ref(
|
||||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lacus metus blandit dolor lacus natoque ad fusce aliquam velit.',
|
||||
);
|
||||
const hashText = (algo: AlgoNames, value: string) => algos[algo](value).toString();
|
||||
function formatWithEncoding(words: lib.WordArray, encoding: Encoding) {
|
||||
if (encoding === 'Bin') {
|
||||
return convertHexToBin(words.toString(enc.Hex));
|
||||
}
|
||||
|
||||
return words.toString(enc[encoding]);
|
||||
}
|
||||
|
||||
const hashText = (algo: AlgoNames, value: string) => formatWithEncoding(algos[algo](value), encoding.value);
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue