summaryrefslogtreecommitdiff
path: root/apps/web/lib/api-key.ts
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/lib/api-key.ts')
-rw-r--r--apps/web/lib/api-key.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/apps/web/lib/api-key.ts b/apps/web/lib/api-key.ts
new file mode 100644
index 0000000..ce59f89
--- /dev/null
+++ b/apps/web/lib/api-key.ts
@@ -0,0 +1,20 @@
+import { randomBytes, createHash } from "crypto"
+
+const API_KEY_PREFIX = "asn_"
+
+export function generateApiKey(): {
+ fullKey: string
+ keyHash: string
+ keyPrefix: string
+} {
+ const randomPart = randomBytes(20).toString("hex")
+ const fullKey = `${API_KEY_PREFIX}${randomPart}`
+ const keyHash = hashApiKey(fullKey)
+ const keyPrefix = fullKey.slice(0, 8)
+
+ return { fullKey, keyHash, keyPrefix }
+}
+
+export function hashApiKey(key: string): string {
+ return createHash("sha256").update(key).digest("hex")
+}