Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions examples/scripts/hmac_generate_verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@ 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));
console.log("Generated HMAC:", new Uint8Array(signature).toHex());

// Verify the HMAC signature
const isValid = await crypto.subtle.verify("HMAC", key, signature, messageData);
Expand Down
5 changes: 1 addition & 4 deletions runtime/reference/std/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,9 @@ console.log(new Uint8Array(hash));
```ts
import { crypto } from "@std/crypto/crypto";

const toHex = (bytes: Uint8Array) =>
Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");

const data = new TextEncoder().encode("hello");
const buf = await crypto.subtle.digest("BLAKE3", data);
console.log(toHex(new Uint8Array(buf))); // e.g. "ea..."
console.log(new Uint8Array(buf).toHex()); // e.g. "ea..."
```

### Hash a file (BLAKE3)
Expand Down