JS SHA1 文件 函数

编写于

需要的库

CryptoJS/rollups/sha1.js
CryptoJS/components/lib-typedarrays-min.js

hash: (fobj, callback_end, callback_progress)->
blob = new Blob()
read = 0
unit = 1024 * 1024
sha1 = CryptoJS.algo.SHA1.create()
reader = new FileReader()
reader.onload = (e)->
bytes = CryptoJS.lib.WordArray.create(new Uint8Array(e.target.result))
sha1.update bytes
read += unit

if read < fobj.size
callback_progress read / fobj.size
blob = fobj.slice read, read + unit
reader.readAsArrayBuffer blob

else
hash = sha1.finalize()
callback_progress 1
callback_end hash.toString(CryptoJS.enc.Hex)
@processing = false

reader.readAsArrayBuffer fobj.slice(read, read + unit)