()
| 3 | let hmacKeyCache: CryptoKey | null = null; |
| 4 | |
| 5 | async function getHmacKey(): Promise<CryptoKey> { |
| 6 | if (hmacKeyCache) return hmacKeyCache; |
| 7 | const secret = serverEnv().NEXTAUTH_SECRET; |
| 8 | const encoder = new TextEncoder(); |
| 9 | hmacKeyCache = await crypto.subtle.importKey( |
| 10 | "raw", |
| 11 | encoder.encode(secret), |
| 12 | { name: "HMAC", hash: "SHA-256" }, |
| 13 | false, |
| 14 | ["sign"], |
| 15 | ); |
| 16 | return hmacKeyCache; |
| 17 | } |
| 18 | |
| 19 | export async function hashKey(key: string): Promise<string> { |
| 20 | const hmacKey = await getHmacKey(); |