Implement util function to generate hash string

This commit is contained in:
HoshinoKoji 2024-09-25 22:41:36 +08:00
parent 85d462a53d
commit ba8a7cd6ef

View File

@ -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!');