Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate md5 hash of a file using javascript

Tags:

javascript

md5

Is there a way to calculate the MD5 hash of a file before the upload to the server using Javascript?

like image 950
LuRsT Avatar asked Apr 20 '09 13:04

LuRsT


People also ask

How do I find the MD5 hash 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 MD5 hash JavaScript?

MD5 is a standardized 1-way function that allows any data input to be mapped to a fixed-size output string, no matter how large or small the input string is. A small change in the input drastically changes the output.

Can I use MD5 in JavaScript?

First of all, let's understand that there's no native way in JavaScript to decrypt MD5 hashes. Since MD5 hashing is a one-way algorithm, theoretically it's not possible to reverse MD5 hashes.


1 Answers

While there are JS implementations of the MD5 algorithm, older browsers are generally unable to read files from the local filesystem.

I wrote that in 2009. So what about new browsers?

With a browser that supports the FileAPI, you can read the contents of a file - the user has to have selected it, either with an <input> element or drag-and-drop. As of Jan 2013, here's how the major browsers stack up:

  • FF 3.6 supports FileReader, FF4 supports even more file based functionality
  • Chrome has supported the FileAPI since version 7.0.517.41
  • Internet Explorer 10 has partial FileAPI support
  • Opera 11.10 has partial support for FileAPI
  • Safari - I couldn't find a good official source for this, but this site suggests partial support from 5.1, full support for 6.0. Another article reports some inconsistencies with the older Safari versions

How?

See the answer below by Benny Neugebauer which uses the MD5 function of CryptoJS

like image 140
Paul Dixon Avatar answered Sep 21 '22 07:09

Paul Dixon