import * as base64js from "base64-js" ; import { IsoBuffer } from "./bufferBrowser" ; async function digestBuffer ( file , algorithm ) { const hash = await crypto . subtle . digest ( algorithm , file ) ; return new Uint8Array ( hash ) ; } function encodeDigest ( hashArray , encoding ) { switch ( encoding ) { case "hex" : { const hashHex = Array . prototype . map . call ( hashArray , ( byte ) => { return byte . toString ( 16 ) . padStart ( 2 , "0" ) as string ; } ) . join ( "" ) ; return hashHex ; } case "base64" : { return base64js . fromByteArray ( hashArray ) ; } } } param file param algorithm param hashEncoding returns export async function hashFile ( file , algorithm = "SHA-1" , hashEncoding = "hex" ) { if ( crypto . subtle === undefined ) { return import ( "./hashFileNode" ) . then ( async ( m ) => m . hashFile ( file , algorithm , hashEncoding ) ) ; } const hashArray = await digestBuffer ( file , algorithm ) ; return encodeDigest ( hashArray , hashEncoding ) ; } param file returns export async function gitHashFile ( file ) { const size = file . byteLength ; const filePrefix = `blob ${ size . toString ( ) }${ String . fromCharCode ( 0 ) }` ; const hashBuffer = IsoBuffer . from ( filePrefix + file . toString ( ) ) ; return hashFile ( hashBuffer ) ; }