mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { noop } from 'lodash-es'
|
|
import { createSHA1 } from 'hash-wasm'
|
|
|
|
interface CalcFileHashProps {
|
|
file: File
|
|
onProgress?: (current: number) => void
|
|
chunkSize?: number
|
|
engine?: 'native' | 'wasm'
|
|
}
|
|
|
|
const calcFileHash = async (props: CalcFileHashProps) => {
|
|
const { file, onProgress = noop, chunkSize = 100, engine = 'native' } = props || {}
|
|
|
|
if (engine === 'native') {
|
|
const buffer = await file.arrayBuffer()
|
|
const hashBuffer = await crypto.subtle.digest('SHA-1', buffer)
|
|
return Array.from(new Uint8Array(hashBuffer))
|
|
.map((b) => b.toString(16).padStart(2, '0'))
|
|
.join('')
|
|
}
|
|
|
|
const chunkBytes = chunkSize * 1024 * 1024
|
|
const hasher = await createSHA1()
|
|
let offset = 0
|
|
while (offset < file.size) {
|
|
const buffer = await file.slice(offset, offset + chunkBytes).arrayBuffer()
|
|
hasher.update(new Uint8Array(buffer))
|
|
offset += chunkBytes
|
|
onProgress(Math.min(offset, file.size) / file.size)
|
|
}
|
|
return hasher.digest('hex')
|
|
}
|
|
|
|
export default calcFileHash
|