Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { arrayBufferToBase64 } from './buffer'; |
| 2 | |
| 3 | /** |
| 4 | * Computes HMAC-SHA256 using Web Crypto API |
| 5 | */ |
| 6 | export async function hmacSha256Base64(secret: string, message: string): Promise<string> { |
| 7 | const encoder = new TextEncoder(); |
| 8 | const keyData = encoder.encode(secret); |
| 9 | const key = await globalThis.crypto.subtle.importKey( |
| 10 | 'raw', |
| 11 | keyData, |
| 12 | { name: 'HMAC', hash: 'SHA-256' }, |
| 13 | false, |
| 14 | ['sign'] |
| 15 | ); |
| 16 | const signatureData = encoder.encode(message); |
| 17 | const signatureBuffer = await globalThis.crypto.subtle.sign('HMAC', key, signatureData); |
| 18 | |
| 19 | return arrayBufferToBase64(signatureBuffer); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Constant-time comparison of two strings to prevent timing attacks |
| 24 | */ |
| 25 | export function timingSafeEqual(a: string, b: string): boolean { |
| 26 | if (a.length !== b.length) { |
| 27 | return false; |
| 28 | } |
| 29 | let result = 0; |
| 30 | for (let i = 0; i < a.length; i++) { |
| 31 | result |= a.charCodeAt(i) ^ b.charCodeAt(i); |
| 32 | } |
| 33 | return result === 0; |
| 34 | } |