diff options
Diffstat (limited to 'src/tools/hmac-generator/hmac-generator.vue')
-rw-r--r-- | src/tools/hmac-generator/hmac-generator.vue | 98 |
1 files changed, 50 insertions, 48 deletions
diff --git a/src/tools/hmac-generator/hmac-generator.vue b/src/tools/hmac-generator/hmac-generator.vue index f0fc239..6e6b1f1 100644 --- a/src/tools/hmac-generator/hmac-generator.vue +++ b/src/tools/hmac-generator/hmac-generator.vue @@ -1,3 +1,50 @@ +<script setup lang="ts"> +import type { lib } from 'crypto-js'; +import { + HmacMD5, + HmacRIPEMD160, + HmacSHA1, + HmacSHA224, + HmacSHA256, + HmacSHA3, + HmacSHA384, + HmacSHA512, + enc, +} from 'crypto-js'; +import { computed, ref } from 'vue'; +import { convertHexToBin } from '../hash-text/hash-text.service'; +import { useCopy } from '@/composable/copy'; + +const algos = { + MD5: HmacMD5, + RIPEMD160: HmacRIPEMD160, + SHA1: HmacSHA1, + SHA3: HmacSHA3, + SHA224: HmacSHA224, + SHA256: HmacSHA256, + SHA384: HmacSHA384, + SHA512: HmacSHA512, +} as const; + +type Encoding = keyof typeof enc | 'Bin'; + +function formatWithEncoding(words: lib.WordArray, encoding: Encoding) { + if (encoding === 'Bin') { + return convertHexToBin(words.toString(enc.Hex)); + } + return words.toString(enc[encoding]); +} + +const plainText = ref(''); +const secret = ref(''); +const hashFunction = ref<keyof typeof algos>('SHA256'); +const encoding = ref<Encoding>('Hex'); +const hmac = computed(() => + formatWithEncoding(algos[hashFunction.value](plainText.value, secret.value), encoding.value), +); +const { copy } = useCopy({ source: hmac }); +</script> + <template> <div> <n-form-item label="Plain text to compute the hash"> @@ -43,54 +90,9 @@ <n-input readonly :value="hmac" type="textarea" placeholder="The result of the HMAC..." /> </n-form-item> <div flex justify-center> - <c-button @click="copy()">Copy HMAC</c-button> + <c-button @click="copy()"> + Copy HMAC + </c-button> </div> </div> </template> - -<script setup lang="ts"> -import { useCopy } from '@/composable/copy'; -import { - enc, - HmacMD5, - HmacRIPEMD160, - HmacSHA1, - HmacSHA224, - HmacSHA256, - HmacSHA3, - HmacSHA384, - HmacSHA512, - lib, -} from 'crypto-js'; -import { computed, ref } from 'vue'; -import { convertHexToBin } from '../hash-text/hash-text.service'; - -const algos = { - MD5: HmacMD5, - RIPEMD160: HmacRIPEMD160, - SHA1: HmacSHA1, - SHA3: HmacSHA3, - SHA224: HmacSHA224, - SHA256: HmacSHA256, - SHA384: HmacSHA384, - SHA512: HmacSHA512, -} as const; - -type Encoding = keyof typeof enc | 'Bin'; - -function formatWithEncoding(words: lib.WordArray, encoding: Encoding) { - if (encoding === 'Bin') { - return convertHexToBin(words.toString(enc.Hex)); - } - return words.toString(enc[encoding]); -} - -const plainText = ref(''); -const secret = ref(''); -const hashFunction = ref<keyof typeof algos>('SHA256'); -const encoding = ref<Encoding>('Hex'); -const hmac = computed(() => - formatWithEncoding(algos[hashFunction.value](plainText.value, secret.value), encoding.value), -); -const { copy } = useCopy({ source: hmac }); -</script> |