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


var hash = CryptoJS.MD5(text);


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


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

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


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  -


$ echo abc | md5sum
0bee89b07a248e27c83fc3d5951213c1  -

Need Your Help

Laravel required_if not working

php laravel laravel-4

My required_if validation does not work. No error is output when "doubles" is selected but "team_mate2 and opponent2" are not selected. I need the form to require a second team mate and opponent when