Skip to content

Commit 53df0a2

Browse files
committed
feat: add key encoding (text or hex string) for hmac
1 parent b430bae commit 53df0a2

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/tools/hmac-generator/hmac-generator.vue

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const algos = {
2727
} as const;
2828
2929
type Encoding = keyof typeof enc | 'Bin';
30+
type KeyEncoding = 'Text' | 'Hex';
3031
3132
function formatWithEncoding(words: lib.WordArray, encoding: Encoding) {
3233
if (encoding === 'Bin') {
@@ -39,17 +40,36 @@ const plainText = ref('');
3940
const secret = ref('');
4041
const hashFunction = ref<keyof typeof algos>('SHA256');
4142
const encoding = ref<Encoding>('Hex');
42-
const hmac = computed(() =>
43-
formatWithEncoding(algos[hashFunction.value](plainText.value, secret.value), encoding.value),
44-
);
43+
const keyEncoding = ref<KeyEncoding>('Text');
44+
const hmac = computed(() => {
45+
// normalize secret according to the key encoding
46+
const key = keyEncoding.value === 'Text' ? secret.value : enc.Hex.parse(secret.value);
47+
return formatWithEncoding(algos[hashFunction.value](plainText.value, key), encoding.value);
48+
});
4549
const { copy } = useCopy({ source: hmac });
4650
</script>
4751

4852
<template>
4953
<div flex flex-col gap-4>
5054
<c-input-text v-model:value="plainText" multiline raw-text placeholder="Plain text to compute the hash..." rows="3" autosize autofocus label="Plain text to compute the hash" />
51-
<c-input-text v-model:value="secret" raw-text placeholder="Enter the secret key..." label="Secret key" clearable />
52-
55+
<div flex gap-2>
56+
<c-input-text v-model:value="secret" placeholder="Enter the secret key..." label="Secret key" raw-text clearable flex-1 />
57+
<c-select
58+
v-model:value="keyEncoding" label="Key encoding"
59+
flex-1
60+
placeholder="Select the key encoding..."
61+
:options="[
62+
{
63+
label: 'Plain Text',
64+
value: 'Text',
65+
},
66+
{
67+
label: 'Hexadecimal Text',
68+
value: 'Hex',
69+
},
70+
]"
71+
/>
72+
</div>
5373
<div flex gap-2>
5474
<c-select
5575
v-model:value="hashFunction" label="Hashing function"

0 commit comments

Comments
 (0)