(data: string, algorithm: 'SHA-512' | 'SHA-256' | 'SHA-1')
| 70 | } |
| 71 | |
| 72 | async function computeHashInternal(data: string, algorithm: 'SHA-512' | 'SHA-256' | 'SHA-1'): Promise<string> { |
| 73 | // Ensure crypto provider is initialized |
| 74 | if (!cryptoProvider) { |
| 75 | if (!cryptoProviderPromise) { |
| 76 | cryptoProviderPromise = initCryptoProvider(); |
| 77 | } |
| 78 | cryptoProvider = await cryptoProviderPromise; |
| 79 | } |
| 80 | |
| 81 | const inputBuffer = new TextEncoder().encode(data); |
| 82 | const hashBuffer = await cryptoProvider.subtle.digest({ name: algorithm }, inputBuffer); |
| 83 | |
| 84 | // Turn into hash string (got this logic from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) |
| 85 | const hashArray = Array.from(new Uint8Array(hashBuffer)); |
| 86 | return hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); |
| 87 | } |
no test coverage detected