Skip to content

Commit 71f7a4a

Browse files
authored
feat: add hmac demo
1 parent 0d43f47 commit 71f7a4a

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

crypsi/index.html

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
border-radius: 10px;
2424
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
2525
width: 90%;
26-
max-width: 500px;
26+
max-width: 900px;
2727
text-align: center;
2828
}
2929

@@ -148,8 +148,58 @@ <h3>Encrypted Output:</h3>
148148

149149
<h3>Decrypted Output:</h3>
150150
<p id="output"></p>
151+
152+
<h3>Hash-based message authentication code (or HMAC)</h3>
153+
154+
<label for="hmacModeSelect">Select HASH function:</label>
155+
<select id="hmacModeSelect">
156+
<option value="sha1">SHA1</option>
157+
<option value="sha256">SHA256</option>
158+
<option value="sha384">SHA384</option>
159+
<option value="sha512">SHA512</option>
160+
</select>
161+
162+
<label for="keyInput">Enter HMAC Key:</label>
163+
<input type="text" id="hmacKeyInput" placeholder="Enter 32-byte key">
164+
165+
<label for="dataInput">Enter Data to Encrypt:</label>
166+
<input type="text" id="hmacDataInput" placeholder="Enter text to encrypt">
167+
168+
<button onclick="hmacData()">Hash</button>
169+
170+
<h3>HMAC Output:</h3>
171+
<p id="hmacOutput"></p>
172+
151173

152174
<script>
175+
async function hmacData() {
176+
const key = document.getElementById("hmacKeyInput").value;
177+
const data = document.getElementById("hmacDataInput").value;
178+
const mode = document.getElementById("hmacModeSelect").value;
179+
180+
try {
181+
let hmacOutput;
182+
switch(mode) {
183+
case "sha1":
184+
hmacOutput = await crypsi.hmac.sha1(key, data);
185+
break;
186+
case "sha256":
187+
hmacOutput = await crypsi.hmac.sha256(key, data);
188+
break;
189+
case "sha384":
190+
hmacOutput = await crypsi.hmac.sha384(key, data);
191+
break;
192+
case "sha512":
193+
hmacOutput = await crypsi.hmac.sha512(key, data);
194+
break;
195+
}
196+
197+
document.getElementById("hmacOutput").innerText = hmacOutput;
198+
} catch (error) {
199+
document.getElementById("hmacOutput").innerText = "HMAC failed: " + error.message;
200+
}
201+
}
202+
153203
async function encryptData() {
154204
const key = document.getElementById("keyInput").value;
155205
const data = document.getElementById("dataInput").value;

0 commit comments

Comments
 (0)