Getting md5sum of a file through Crypto.js

好久不见. 提交于 2021-02-07 03:24:35

问题


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.

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();

In Linux I do:

md5sum name_of_file.tar

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?

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

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

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

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


回答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));



回答2:


I think this is because the file does not finish loading and the hash gets created before the file upload is 100%. Try moving the hashing into the onloadend event:

var reader = new FileReader();

reader.onloadend = function () {
     var hash = CryptoJS.MD5(reader.result);
     hash.toString();
}

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



回答3:


When you're running your bash, make sure you're not sending a newline to hashing function. If you use echo, add -n parameter:

$ echo -n abc | md5sum
900150983cd24fb0d6963f7d28e17f72  -

vs:

$ echo abc | md5sum
0bee89b07a248e27c83fc3d5951213c1  -


来源:https://stackoverflow.com/questions/20263741/getting-md5sum-of-a-file-through-crypto-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!