From ba8a7cd6ef52b5540291a0cb0baba1c4d075a9a0 Mon Sep 17 00:00:00 2001 From: HoshinoKoji Date: Wed, 25 Sep 2024 22:41:36 +0800 Subject: [PATCH] Implement util function to generate hash string --- src/index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/index.js b/src/index.js index de66b62..8b93fed 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,14 @@ * Learn more at https://developers.cloudflare.com/workers/ */ +async function getHashString(string, method = 'SHA-256') { + const encoder = new TextEncoder(); + const data = encoder.encode(string); + const hash = await crypto.subtle.digest(method, data); + const hashArray = Array.from(new Uint8Array(hash)); + return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); +} + export default { async fetch(request, env, ctx) { return new Response('Hello World!');