文件与文件内容作为WordArray的不同sha256哈希值 [英] different sha256 hash value of file vs. file contents as WordArray

查看:102
本文介绍了文件与文件内容作为WordArray的不同sha256哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个测试文本文件,内容是: aaaaaaaabbbbbbbbccccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh

I made a test text file, the contents are: aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffgggggggghhhhhhhh

此值作为字符串的sha256十六进制摘要为: 75eef9680de51f6f70291057e9afc5975470960dfec5f37f83db69aa625786e5

The sha256 hex digest for this value as a string is: 75eef9680de51f6f70291057e9afc5975470960dfec5f37f83db69aa625786e5

在Python中使用hashlib,在js中使用Crypto.js或从命令行在文件上使用ssl对其进行哈希处理时,会得到相同的值.

I get this same value when hashing it in python using hashlib, js using Crypto.js, or using ssl on the file from the command line.

但是,在js中,当我这样读取文件时:

however, in js, when I read the file in like this:

var fr = new FileReader();
fr.readAsArrayBuffer(file);
console.log(fr.result.byteLength); // it's 64...
var input = CryptoJS.lib.WordArray.create(new Uint8Array(fr.result));
CryptoJS.SHA256(input).toString();

我得到: 8f76bf13468fb12ac4e59610adff70fd10282e8494a2749db4677f81e2c6e998

更新:来自crypto.js文档:

UPDATE: from the crypto.js docs:

/**
 * cryptojs use WordArray (CryptoJS.lib.WordArray) as parameter/result frequently.
 *    A WordArray object represents an array of 32-bit words. When you pass a string,
 * it's automatically converted to a WordArray encoded as UTF-8.
 */

怀疑它可能是utf-8 vs ascii之类的东西,或类似的东西,但不知道如何检查.

suspecting it could be a utf-8 vs ascii thing, or something like that, but have no idea how to check.

推荐答案

我发现 asmCrypto.js 接受ArrayBuffers和Uint8Arrays作为输入-我现在得到了预期的结果(它也非常快).我这样使用它:

I found asmCrypto.js which accepts both ArrayBuffers and Uint8Arrays as input - I'm now getting the expected result (It's also pretty fast). I use it like this:

var fr = new FileReader();
fr.readAsArrayBuffer(file);
console.log(fr.result.byteLength); // it's still 64...
asmCrypto.SHA256.hex(new Uint8Array(fr.result));
asmCrypto.SHA256.hex(fr.result); // this also works

这篇关于文件与文件内容作为WordArray的不同sha256哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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