Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting md5sum of a file through Crypto.js

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

like image 215
tblakey89 Avatar asked Nov 28 '13 10:11

tblakey89


People also ask

How do I find the md5sum of a file?

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.

What is the md5sum of the attachment?

The md5sum command is based on the MD5 algorithm and generates 128-bit message digests. The md5sum command enables you to verify the integrity of files downloaded over a network connection. You can also use the md5sum command to compare files and verify the integrity of files.

How do I get the md5sum of a file in Python?

Correct Way to create MD5 Hash of a file in Python The process of creating an MD5 hash in python is very simple. First import hashlib, then encode your string that you want to hash i.e., converts the string into the byte equivalent using encode(), then pass it through the hashlib. md5() function.


2 Answers

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));
like image 128
Jeff M Avatar answered Oct 06 '22 01:10

Jeff M


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]);
like image 42
Daniel W. Avatar answered Oct 06 '22 01:10

Daniel W.