diff --git a/examples/scripts/hmac_generate_verify.ts b/examples/scripts/hmac_generate_verify.ts index 06c1f9543..14c7218c0 100644 --- a/examples/scripts/hmac_generate_verify.ts +++ b/examples/scripts/hmac_generate_verify.ts @@ -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); diff --git a/runtime/reference/std/crypto.md b/runtime/reference/std/crypto.md index f98f2c9be..b8af4781e 100644 --- a/runtime/reference/std/crypto.md +++ b/runtime/reference/std/crypto.md @@ -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)