CryproJS md5 & sha1 vs utility

JavaScript performance comparison

Revision 12 of this test case created by Xotic750

Info

Tests comprise of an empty string (x 10), then the UCS2 character code range of of to 65535, split into 256 character chunks (x 256), and finally one big string of the complete UCS2 character range (x 1). Some of the functions below claim to utf8Encode the string so that they can encode it. Errors are when this encoding has failed and the function falls over or if the routine does not produce hashes that match others. Otherwise on basic strings all of the passed functions provide a correct hash. For the ones that fail due to utf8 encoding, they are run again, this time with the string externally utf8 encoded before being used.

Preparation code

<script src="//rghost.net/download/44839442/8a4d1e253dfd3d09643d64a4b3c2e04bf2a5a040/utility3.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js">
</script>
<script src="//tiny-sha1.googlecode.com/files/tinySHA1.r4.js">
</script>
<script src="//bitbucket.org/keesey/sha1/raw/fcbee69dcc6f6e9038e7d78376ab207e0816896a/bin/sha1.js">
</script>
<script src="//www.movable-type.co.uk/scripts/utf8.js">
</script>
<script src="//www.movable-type.co.uk/scripts/sha1.js">
</script>
<script src="//www.movable-type.co.uk/scripts/sha256.js">
</script>
<script src="//utility-js.googlecode.com/files/sha1.js">
</script>
<script>
  var s = "",
      a = [];

  $u.range(0, 9).forEach(function() {
    a.push(s);
  });

  $u.range(1, 257).forEach(function(part) {
    s = "";
    $u.range(0, 255).forEach(function(character) {
      s += String.fromCharCode(part * character);
    });

    a.push(s);
  });

  a.push(a.join(''));
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
CryptoJs md5
a.forEach(function(str) {
  CryptoJS.MD5(str).toString();
});
pending…
utility md5
a.forEach(function(str) {
  $u.md5(str);
});
pending…
CryptoJs sha1
a.forEach(function(str) {
  CryptoJS.SHA1(str).toString();
});

// handles the first 225 tests and then fails, otherwise had matching hashes, passing it a utf8encoded string causes the hashes to be incorrect.
pending…
utility sha1
a.forEach(function(str) {
  $u.sha1(str);
});
pending…
tiny sha1
throw new Error("Fails to match hashes produced by other routines, irrespective of utf8");
a.forEach(function(str) {
  SHA1(str);
});
pending…
movable-type sha1
a.forEach(function(str) {
  Sha1.hash(str, true);
});
pending…
other sha1
throw new Error("Fails to match hashes produced by other routines, irrespective of utf8");
a.forEach(function(str) {
  sha1.hash(str);
});
pending…
jssha sha1
a.forEach(function(str) {
  (new jsSHA($u.utf8Encode(str), 'ASCII')).getHash('SHA-1', 'HEX');
});
pending…
CryptoJs sha256
a.forEach(function(str) {
  CryptoJS.SHA256(str).toString();
});
pending…
utility sha256
a.forEach(function(str) {
  $u.sha256(str);
});
pending…
movable-type sha256
a.forEach(function(str) {
  Sha256.hash(str, true);
});
pending…
CryptoJs md5 w xtra utf8
a.forEach(function(str) {
  CryptoJS.MD5($u.utf8Encode(str)).toString();
});
pending…
CryptoJs sha256 w xtra utf8
a.forEach(function(str) {
  CryptoJS.SHA256($u.utf8Encode(str)).toString();
});
pending…
check sha1's
a.forEach(function(str, index) {
  var //sw1 = CryptoJS.SHA1((str)).toString(),
      //sw2 = sha1.hash(str),
      sw3 = Sha1.hash(str, true),
      //sw4 = SHA1(str),
      sw5 = $u.sha1(str),
      sw6 = (new jsSHA($u.utf8Encode(str), 'ASCII')).getHash('SHA-1', 'HEX'),
      //t1 = sw5 === sw1,
      //t2 = sw5 === sw2,
      t3 = sw5 === sw3,
      //t4 = sw5 === sw4,
      t5 = sw5 === sw6;

  if (!t3 || !t5) {
    console.error(index, [t3, t5]);
    throw new Error("mismatch index: " + index + " " + [t3, t5].toString());
  }
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment