-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathhmac_generate_verify.ts
55 lines (45 loc) · 1.79 KB
/
hmac_generate_verify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @title HMAC Generation and Verification
* @difficulty intermediate
* @tags cli, web
* @run <url>
* @group Cryptography
*
* This example demonstrates how to generate and verify an HMAC (Hash-based Message Authentication Code)
* using Deno's built-in SubtleCrypto API with the SHA-256 hash function.
*/
// Define the secret key for HMAC (in a real application, store this securely)
const secret = "supersecretkey";
// Convert the secret key to a Uint8Array using TextEncoder
const encoder = new TextEncoder();
const keyData = encoder.encode(secret);
// Import the secret key into the SubtleCrypto API for HMAC operations
const key = await crypto.subtle.importKey(
"raw", // The format of the key
keyData, // The key data
{ // Algorithm details
name: "HMAC",
hash: { name: "SHA-256" },
},
false, // Whether the key is extractable
["sign", "verify"], // Key usages: Sign and Verify
);
// The message to be authenticated
const message = "Authenticate this message";
// Convert the message to a Uint8Array
const messageData = encoder.encode(message);
// Generate the HMAC signature for the message
const signature = await crypto.subtle.sign("HMAC", key, messageData);
// Function to convert ArrayBuffer to hex string for readability only. This isn't part of the generation or verification
function bufferToHex(buffer: ArrayBuffer): string {
const byteArray = new Uint8Array(buffer);
return Array.from(byteArray)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}
// Output the generated HMAC signature in hexadecimal format
console.log("Generated HMAC:", bufferToHex(signature));
// Verify the HMAC signature
const isValid = await crypto.subtle.verify("HMAC", key, signature, messageData);
// Output the verification result
console.log("Is the HMAC valid?", isValid);