简体   繁体   English

使用javascript计算大文件的MD5哈希

[英]Calculate MD5 hash of a large file using javascript

How do you upload a 500mb file and get a MD5 hash with CryptoJS?如何使用 CryptoJS 上传 500mb 文件并获取 MD5 哈希值?

Here is my code:这是我的代码:

$('#upload-file').change(function(){
    var reader = new FileReader();
    reader.addEventListener('load',function () {
        var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(this.result));
        window.md5 = hash.toString(CryptoJS.enc.Hex);
    });

    reader.readAsBinaryString(this.files[0]);
});

If the file is under 200mb, it works.如果文件小于 200mb,它就可以工作。 Anything bigger, this.result is an empty "".任何更大的,this.result 是一个空的“”。

I've tried:我试过:

filereader api on big files 大文件上的文件阅读器 API

javascript FileReader - parsing long file in chunks javascript FileReader - 分块解析长文件

and almost got this to work , but console is complaining about .join("")几乎让它起作用了,但是控制台抱怨 .join("")

http://dojo4.com/blog/processing-huge-files-with-an-html5-file-input http://dojo4.com/blog/processing-huge-files-with-an-html5-file-input

CryptoJS has a progressive api for hash digests . CryptoJS 有一个用于哈希摘要渐进式 API The rest is taken form alediaferia's answer with slight modifications.其余部分采用alediaferia 的答案,稍作修改。

 function process() { getMD5( document.getElementById("my-file-input").files[0], prog => console.log("Progress: " + prog) ).then( res => console.log(res), err => console.error(err) ); } function readChunked(file, chunkCallback, endCallback) { var fileSize = file.size; var chunkSize = 4 * 1024 * 1024; // 4MB var offset = 0; var reader = new FileReader(); reader.onload = function() { if (reader.error) { endCallback(reader.error || {}); return; } offset += reader.result.length; // callback for handling read chunk // TODO: handle errors chunkCallback(reader.result, offset, fileSize); if (offset >= fileSize) { endCallback(null); return; } readNext(); }; reader.onerror = function(err) { endCallback(err || {}); }; function readNext() { var fileSlice = file.slice(offset, offset + chunkSize); reader.readAsBinaryString(fileSlice); } readNext(); } function getMD5(blob, cbProgress) { return new Promise((resolve, reject) => { var md5 = CryptoJS.algo.MD5.create(); readChunked(blob, (chunk, offs, total) => { md5.update(CryptoJS.enc.Latin1.parse(chunk)); if (cbProgress) { cbProgress(offs / total); } }, err => { if (err) { reject(err); } else { // TODO: Handle errors var hash = md5.finalize(); var hashHex = hash.toString(CryptoJS.enc.Hex); resolve(hashHex); } }); }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/md5.js"></script> <input id="my-file-input" type="file"> <button onclick="process()">Process</button>

You don't need to read the whole file at once and feed it all in one go to CryptoJS routines.您不需要一次读取整个文件并将其全部提供给 CryptoJS 例程。

You can create the hasher object, and feed chunks as you read them, and then get the final result.您可以创建 hasher 对象,并在读取时提供块,然后获得最终结果。

Sample taken from the CryptoJS documentation来自CryptoJS 文档的示例

var sha256 = CryptoJS.algo.SHA256.create();
sha256.update("Message Part 1");
sha256.update("Message Part 2");
sha256.update("Message Part 3");
var hash = sha256.finalize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM