如何使用CryptoJS获取BLOB的正确SHA1哈希? [英] How to get correct SHA1 hash of BLOB using CryptoJS?

查看:533
本文介绍了如何使用CryptoJS获取BLOB的正确SHA1哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CryptoJS v3.1.2,sha1.js汇总

CryptoJS v3.1.2, sha1.js rollup

在JS中,我想在将斑点发送到服务器之前计算其SHA1.在服务器上,我想计算结果文件的SHA1并将其与从JS接收到的SHA1进行比较.问题是CryptoJS.SHA1()生成的哈希值不正确(总是9844f81e1408f6ecb932137d33bed7cfdcf518a3)

In JS I want to calculate the SHA1 of a blob before sending it to the server. On the server I want to calculate the SHA1 of the resulting file and compare it to the SHA1 received from JS. The problem is that the hash generated by CryptoJS.SHA1() is incorrect (always 9844f81e1408f6ecb932137d33bed7cfdcf518a3)

JS代码:

function uploadFileslice (slice) { // slice is a blob
    var fileReader = new FileReader()
    fileReader.onload = function(event){
        var arrayBuffer = event.target.result
        var wordArray = CryptoJS.lib.WordArray.create(arrayBuffer)
        var sha1crc = CryptoJS.SHA1(wordArray).toString(CryptoJS.enc.Hex)
        //etc
        requestParams.append('fileslice', slice)
        requestParams.append('sha1crc', sha1crc)
        //etc
    }
    fileReader.readAsArrayBuffer(slice)
}

PHP代码:

$file_crc = sha1_file($_FILES['fileslice']['tmp_name']);
if ($_REQUEST['sha1crc'] !== $file_crc) {
    echo "Invalid CRC: {$_REQUEST['sha1crc']} (expected $file_crc)";
    return;
}

输出:

无效的CRC:9844f81e1408f6ecb932137d33bed7cfdcf518a3(预计3ebe2cd2d8fd8d8f977b6d715f0b1adf5b08b407

Invalid CRC: 9844f81e1408f6ecb932137d33bed7cfdcf518a3 (expected 3ebe2cd2d8fd8d8f977b6d715f0b1adf5b08b407

我希望有类似myHash = CryptoJS.SHA1(blob)...

I was hoping for something like myHash = CryptoJS.SHA1(blob)...

推荐答案

根据您提供的信息,我不确定您如何进行设置,但是为了支持ArrayBuffer,必须包含组件/lib-typedarrays-min.js.

From the info that you've provided I'm not sure exactly how you have things setup but in order for ArrayBuffers to be supported you have to include components/lib-typedarrays-min.js.

https://code上对此进行了讨论. google.com/p/crypto-js/issues/detail?id=67 .

希望这会有所帮助!

这篇关于如何使用CryptoJS获取BLOB的正确SHA1哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆