Skip to content
File

Blob: src/worker/auth/crypto.ts

typescript26 lines
1import { hexFromBytes, utf8Bytes } from "@/worker/auth/bytes";
2 
3async function importHkdfMaterial(secret: string): Promise<CryptoKey> {
4 return await crypto.subtle.importKey("raw", utf8Bytes(secret), "HKDF", false, ["deriveBits", "deriveKey"]);
5}
6 
7export async function deriveHmacKeyBytes(secret: string, salt: string, info: string): Promise<Uint8Array> {
8 const material = await importHkdfMaterial(secret);
9 const bits = await crypto.subtle.deriveBits(
10 {
11 name: "HKDF",
12 hash: "SHA-256",
13 salt: utf8Bytes(salt),
14 info: utf8Bytes(info),
15 },
16 material,
17 256,
18 );
19 return new Uint8Array(bits);
20}
21 
22export async function sha256Hex(value: string): Promise<string> {
23 const digest = await crypto.subtle.digest("SHA-256", utf8Bytes(value));
24 return hexFromBytes(new Uint8Array(digest));
25}