fastest MD5 Implementation in JavaScript fastest MD5 Implementation in JavaScript javascript javascript

fastest MD5 Implementation in JavaScript


I've heard Joseph's Myers implementation is quite fast. Additionally, he has a lengthy article on Javascript optimization describing what he learned while writing his implementation. It's a good read for anyone interested in performant javascript.

http://www.webreference.com/programming/javascript/jkm3/

His MD5 implementation can be found here


I would suggest you use CryptoJS in this case.

Basically CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

So if you want to calculate the MD5 hash of your password string then do as follows:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/core.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/md5.js"></script><script>    var passhash = CryptoJS.MD5(password).toString();    $.post(      'includes/login.php',       { user: username, pass: passhash },      onLogin,       'json' );</script>

So this script will post the hash of your password string to the server.

For further info and support on other hash calculating algorithms you can visit:

http://code.google.com/p/crypto-js/


While selecting library it's also important to see if it supports modern frameworks such as Bower, passes jslint, supports plugin model for JQuery or module systems such as AMD/RequireJS in addition to being in active development and have more than 1 contributors. There are couple of options that satisfies some or all of these additional criteria:

  • CryptoJS: This is perhaps the most expansive library where each algorithm can be used separately without adding fat in to your JS code. Plus it as encoder/decoders for UTF8, UTF16 and Base64. I maintain github repository that is registered as Bower package plus instructions on how to use it with RequireJS.
  • Spark MD5: This is based on JKM code that other answer mentions which is also the faster implementation. However in addition, Spark implementation adds AMD support, passes jslint plus has incremental mode. It doesn't have Base64 o/p but it does have raw o/p (i.e. array of 32-bit int insead of string).
  • JQuery MD5 plugin: Very simple down to earth but doesn't seem to have raw mode.
  • JavaScript-MD5: Not as fancy or fast as Spark but simpler.

Example from CryptoJS:

//just include md5.js from the CryptoJS rollups foldervar hash = CryptoJS.MD5("Message");console.log(hash.toString()); 

There is a performance comparison between above libraries at http://jsperf.com/md5-shootout/7. On my machine current tests (which are admittedly old) shows that if you are looking for speed Spark MD5 is your best bet (and so is plain JKM code). However if you looking for more comprehensive library then CryptoJS is your best bet although it is 79% slower than Spark MD5. However I would imagine CryptoJS would eventually achieve same speed as it is bit more active project.