js计算大文件sha1值,分块计算

news/2024/7/10 0:47:41 标签: javascript, 前端, vue, js-sha1

计算文件hash,使用到了模块 js-sha1

该模块文档:GitHub - emn178/js-sha1: A simple SHA1 hash function for JavaScript supports UTF-8 encoding.

 主要思路就是通过内置方法 slice 分块按顺序计算hash值

javascript">import sha1 from 'js-sha1'

export async function ContentHash(file, progress) {

    return new Promise((resolve, reject) => {
        const chunkSize = 50 * 1024 * 1024
        let promise = Promise.resolve()
        for (let index = 0; index < file.size; index += chunkSize) {
            promise = promise.then(() => hashBlob(file.slice(index, index + chunkSize)))
        }
        let count = 0
        const hash = sha1.create()

        function hashBlob(blob) {
            return new Promise((resolve, reject) => {
                const reader = new FileReader()
                reader.onload = ({target}) => {
                    hash.update(target.result)
                    count += 1
                    resolve()
                }
                reader.onerror = function (event) {
                    reject(event)
                }
                reader.onprogress = function (event) {
                    if (event) {
                        progress = Math.ceil((event.loaded + count * chunkSize) * 100 / progress.file_size)
                    }
                }
                reader.readAsArrayBuffer(blob)
            })
        }

        promise.then(async () => {
            const conHash = hash.hex().toUpperCase()
            resolve({conHash})
        }).catch(() => {
            reject()
        })
    })
}


http://www.niftyadmin.cn/n/1265352.html

相关文章

基于阿里云盘的文件分享系统

阿里云网盘不限速简直是太好用了&#xff0c;由于平常会传输一下大文件分享给好友&#xff0c;但是分享的文件必须登录网盘才行&#xff0c;感觉很是不便&#xff0c;因此便萌发了一个新想法&#xff0c;基于不限速网盘的分享系统是不是也很好用呢&#xff1f; 开发该系统&…

centos8安装NVIDIA显卡驱动,docker模式运行机器学习

1.下载驱动 a.查看显卡版本&#xff0c;版本是1050Ti,需要在官网下载该型号驱动 [rootlocalhost ~]# lspci|grep -i nvidia 00:10.0 VGA compatible controller: NVIDIA Corporation GP107 [GeForce GTX 1050 Ti] (rev a1) 00:10.1 Audio device: NVIDIA Corporation GP107GL …

kvm虚拟机centos8磁盘扩容

通过PVE搭建虚拟机&#xff0c;系统盘只有30G,发现使用的时候已经不足&#xff0c;需要进行扩容&#xff0c; 虚拟机关机状态&#xff0c;在PVE增加磁盘大小之后&#xff0c;虚机开机之后&#xff0c;还需要在虚拟机里面进行扩容操作 开机检查磁盘大小 lsblk [rootlocalhost …

阿里云盘视频m3u8播放-python+vue3实现

如图&#xff0c;先看看效果 实现方式 1. 通过api获取视频相关m3u8地址,并进行分析&#xff08;web版可通过调试模式查看&#xff09; 结果如下 {"domain_id": "bj29","drive_id": "650296441","file_id": "63857a5c…

centos7 源码编译安装Python3.11

1.去官网下载最新的python11源码包 Python Source Releases | Python.org cd /opt/ wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgz 2.解压源码压缩包 tar zxvf Python-3.11.1.tgz 3.安装依赖环境 a.python3.11需要1.11版本以上的openssl,需要安装该版本o…

把项目中常用的小工具做个总结吧,方便自己以后用到

1、根据手机的分辨率从 dp 的单位 转成为 px(像素) public static int dip2px(Context context, float dpValue) { final float scale context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale 0.5f); }2、根据手机的分辨率从 px(像素) 的单…

身份证正确性校验

验证身份证的有效性&#xff0c;是否符合身份证规范&#xff1b;调用IDCardValidate方法&#xff0c;返回“true”为有效&#xff0c;否则为无效提示 /** * 功能&#xff1a;身份证的有效验证 * param IDStr 身份证号 * return 有效&#xff1a;返回"true" &#xff…

ImageLoader初始化以及调用

1、首先在当前程序的Application中调用ImageLoader的初始化init()方法 private void initImageLoader() {ImageLoaderConfiguration config new ImageLoaderConfiguration.Builder(this).imageDownloader(new BaseImageDownloader(this, 60 * 1000, 60 * 1000)) // connectTim…