fix(ts_web): Ensure sha256FromBuffer uses correct ArrayBuffer slice for Uint8Array inputs and add local project config files

This commit is contained in:
2025-09-12 21:51:15 +00:00
parent b6f6f16a45
commit 6ba69d84b7
6 changed files with 93 additions and 4 deletions

View File

@@ -60,7 +60,19 @@ export const sha256FromStringSync = (stringArg: string): string => {
*/
export const sha256FromBuffer = async (bufferArg: ArrayBuffer | Uint8Array): Promise<string> => {
if (isCryptoSubtleAvailable()) {
const hash = await crypto.subtle.digest("SHA-256", bufferArg);
// Ensure we pass an ArrayBuffer to satisfy BufferSource typing
let inputBuffer: ArrayBuffer;
if (bufferArg instanceof Uint8Array) {
const view = bufferArg;
inputBuffer = view.buffer.slice(
view.byteOffset,
view.byteOffset + view.byteLength
) as ArrayBuffer;
} else {
inputBuffer = bufferArg;
}
const hash = await crypto.subtle.digest("SHA-256", inputBuffer);
const result = hex(hash);
return result;
} else {
@@ -101,4 +113,4 @@ export const sha256FromFile = async (filePath: string): Promise<string> => {
*/
export const md5FromString = async (stringToHash: string): Promise<string> => {
throw new Error('md5FromString is not supported in browser environment. Web Crypto API does not support MD5.');
};
};