Ver Fonte

feat: computeHash() function

Sven há 9 meses atrás
pai
commit
e1d467c
2 ficheiros alterados com 20 adições e 0 exclusões
  1. 5 0
      .changeset/lazy-fishes-hide.md
  2. 15 0
      lib/misc.ts

+ 5 - 0
.changeset/lazy-fishes-hide.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+Added function `computeHash()` to calculate the hash / checksum of a string

+ 15 - 0
lib/misc.ts

@@ -136,3 +136,18 @@ function ab2str(buf: ArrayBuffer) {
 function str2ab(str: string) {
   return Uint8Array.from(getUnsafeWindow().atob(str), c => c.charCodeAt(0));
 }
+
+/**
+ * Creates a hash / checksum of the given {@linkcode input} string using the specified {@linkcode algorithm} ("SHA-256" by default).  
+ * ⚠️ Uses the Web Crypto API so it needs to run in a secure context (HTTPS).
+ */
+export async function computeHash(input: string, algorithm = "SHA-256") {
+  const encoder = new TextEncoder();
+  const data = encoder.encode(input);
+
+  const hashBuffer = await crypto.subtle.digest(algorithm, data);
+  const hashArray = Array.from(new Uint8Array(hashBuffer));
+  const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, "0")).join("");
+
+  return hashHex;
+}