From fb42f5e1d2b5ce07885dd6ec161b92f08ebfd61a Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Thu, 15 Jan 2026 21:10:31 +0900 Subject: [PATCH] fix: use Uint8Array.prototype.toHex() intead of custom function --- examples/scripts/hmac_generate_verify.ts | 10 +--------- runtime/reference/std/crypto.md | 5 +---- 2 files changed, 2 insertions(+), 13 deletions(-) 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)