通过Crypto.js获取文件的md5sum [英] Getting md5sum of a file through Crypto.js

查看:90
本文介绍了通过Crypto.js获取文件的md5sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用md5sum linux命令和CryptoJS的MD5方法时,我试图获取tar文件的md5sum以产生相同的值.

I am trying to get the md5sum of a tar file to produce the same value when using the md5sum linux command and CryptoJS's MD5 method.

在JavaScript中(在将文件放入HTML格式后):

In JavaScript I do (after a file has been put in an HTML form):

var reader = new FileReader();

reader.onloadend = function () {
     text = (reader.result);
}

reader.readAsBinaryString(document.getElementById("firmware_firmware").files[0]);

var hash = CryptoJS.MD5(text);

hash.toString();

在Linux中,我这样做:

In Linux I do:

md5sum name_of_file.tar

当前,这两个产生不同的结果.我如何能够像在Linux上使用md5sum一样,获取JavaScript来获取tar文件的内容以进行MD5处理?

Currently these two produce different results. How am I able to get JavaScript to get the contents of the tar file to be MD5ed in the same way that md5sum does on Linux?

对于简单的String,md5sum和CryptoJS产生相同的值.

For a simple String, md5sum and CryptoJS produce the same value.

对于名为Fred.txt且内容为"Fred"的文件,md5sum和CryptoJS都产生相同的值:c624decb46fa3d60e824389311b252f6.

With a file called Fred.txt, with content the content: "Fred", both md5sum and CryptoJS produce the same value: c624decb46fa3d60e824389311b252f6.

在update.tar文件上,Linux上的md5sum给我:1f046eedb7d8279953d233e590830e4f,在CryptoJS上给我:f0c3730e5a9863cffa0ba3fadd531788

On the update.tar file, the md5sum on linux gives me: 1f046eedb7d8279953d233e590830e4f, on CryptoJS it gives me: f0c3730e5a9863cffa0ba3fadd531788

Edit2:进一步的测试表明,这实际上是一个问题,原因是文件很大,例如7 MB.

Further testing shows that this is actually a problem due to large file size such as 7 MegaBytes

推荐答案

JavaScript中的所有字符串-甚至是二进制字符串"-实际上都是UTF-16字符.二进制字符串"是选择仅使用前256个代码点的二进制字符串.由于Latin-1编码也正好使用前256个代码点,因此您可以使用Latin-1将字符串转换为字节.

All strings in JavaScript - even "binary strings" - are actually UTF-16 characters. A "binary string" is one that chooses to use only the first 256 code points. Since the Latin-1 encoding also uses exactly the first 256 code points, you can convert the string to bytes using Latin-1.

var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(text));

这篇关于通过Crypto.js获取文件的md5sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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