简体   繁体   English

Cordova可在不耗尽内存的情况下计算大文件的sha1哈希

[英]Cordova calculate sha1 hash of large file without running out of memory

I am trying to use sha1 hashes as a means of checking for file changes, the problem is that when trying to calculate the hash of a larger file(50-100MB) the app throws a Fatal Exception because it ran out of memory. 我正在尝试使用sha1哈希作为检查文件更改的方法,问题是当尝试计算较大文件(50-100MB)的哈希值时,应用程序会抛出致命异常,因为它内存不足。

Version information: 版本信息:

  • Cordova 3.5 科尔多瓦3.5
  • Android 4.2.2 Android 4.2.2
  • Galaxy Tab 2 Galaxy Tab 2
  • org.apache.cordova.file 1.1.0 org.apache.cordova.file 1.1.0
  • CryptoJS latest? CryptoJS最新?
  • lib-typedarrays(module for CryptoJS) latest? lib-typedarrays(用于CryptoJS的模块)最新吗?
  • qjs latest qjs最新

Here is what im using: 这是即时通讯使用的内容:

function _getFileChecksum(file){

    var deferred = Q.defer();
    if(typeof CryptoJS === 'undefined'){
        if(DEBUG){
            console.log('CryptoJS is required.');
        }
        return deferred.reject(new Error('CryptoJS is required.'));
    }

    var reader = new FileReader();

    reader.onload = function (evt) {
        if(DEBUG){
            console.log('_getFileChecksum: reader finished loading');
        }
        var arrayBuffer = evt.target.result;
        var wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
        var hash = CryptoJS.SHA1(wordArray);
        if(DEBUG){
            console.log('_getFileChecksum: hash = '+hash);
        }
        deferred.resolve(hash);
    };
    reader.onerror = function(anError){
        if(DEBUG){
            console.log('_getFileChecksum: reader error');
        }
        deferred.reject(anError);
    };
    reader.readAsArrayBuffer(file);

    return deferred.promise;
}

Now this works just fine for smaller files. 现在,这对于较小的文件就可以了。 But when i get to the larger ones thats where the problem happens. 但是,当我到达较大的那时,那就是问题所在。 Does anyone know how i would calculate a sha1 has of a large file without running out of memory in the application? 有谁知道我将如何计算sha1具有大文件而又不会耗尽应用程序中的内存?

I was able to get around the out of memory issue by adding 我可以通过添加来解决内存不足的问题

android:largeHeap="true"

in my AndroidManifest.xml in the <application /> tag 在我的AndroidManifest.xml中的<application />标记中

<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:largeHeap="true">

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

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