function bufferToHex (buffer: ArrayBuffer): string { const bytes = new Uint8Array(buffer) let hex = '' for (let i = 0; i < bytes.length; i++) { hex += bytes[i].toString(16).padStart(2, '0') } return hex } export async function sha256 (str: string): Promise { const data = new TextEncoder().encode(str) const digest = await crypto.subtle.digest('SHA-256', data) return bufferToHex(digest) } export async function hmacSha256 (str: string, key: string): Promise { const encoder = new TextEncoder() const cryptoKey = await crypto.subtle.importKey( 'raw', encoder.encode(key), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'] ) const signature = await crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(str)) return bufferToHex(signature) }