crypto-js (v14)

Revision 14 of this benchmark created on


Description

Comparison between some standard crypto algorithms SHA1, HMAC-SHA1, MD5, SHA-256, AES-128, AES-256, ChaCHA20

Preparation HTML

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/src/core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/src/cipher-core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/src/mode-ofb.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/src/aes.js"></script>
<script src="https://siphash.googlecode.com/svn/trunk/chacha20.js"></script>
<script src="https://raw.github.com/garycourt/murmurhash-js/master/murmurhash3_gc.js">
</script>
<script src="https://raw.github.com/blueimp/JavaScript-MD5/master/md5.js">
</script>

<script>

function fromHex(h) {
  h.replace(' ', '');
  var out = [], len = h.length, w = '';
  for (var i = 0; i < len; i += 2) {
    w = h[i];
    if (((i+1) >= len) || typeof h[i+1] === 'undefined') {
        w += '0';
    } else {
        w += h[i+1];
    }
    out.push(parseInt(w, 16));
  }
  return out;
}
var hashCode = function(str){
      if (Array.prototype.reduce){
        return str.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
      }
      var hash = 0;
      if (str.length === 0) return hash;
      for (var i = 0; i < str.length; i++) {
        var character  = str.charCodeAt(i);
        hash  = ((hash<<5)-hash)+character;
        hash = hash & hash; // Convert to 32bit integer
      }
      return hash;
    }

  var makeCRCTable = function() {
    var c;
    var crcTable = [];
    for (var n = 0; n < 256; n++) {
      c = n;
      for (var k = 0; k < 8; k++) {
        c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
      }
      crcTable[n] = c;
    }
    console.log("CREATED")
    return crcTable;
  }
  var crcTable = makeCRCTable();
  var crc32_g = function(str) {
    var crc = 0 ^ (-1);
    for (var i = 0; i < str.length; i++) {
      crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
    }
    return (crc ^ (-1)) >>> 0;
  };
</script>
<script src="https://raw.github.com/garycourt/murmurhash-js/master/murmurhash3_gc.js">
</script>
<script src="https://raw.github.com/blueimp/JavaScript-MD5/master/md5.js">
</script>

Setup

var key128 = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
    var iv128 = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
    var key256 = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f');
    var iv256 = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f101112131415161718191a1b1c1d1e1f');
    
    
    chacha_key = fromHex('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f');
    chacha_nonce = fromHex('0001020304050607');
    chacha_ctx = chacha20_init(chacha_key, chacha_nonce);
    //same string than ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367 but in HEX:
    chacha_keystream = fromHex("41536f6d65776861744c6f6e674d65737361676557697468536f6d65536563726574416e644157686f6c6542756e63684f664e756d626572733132333938373633383436353336343536373833353237363534373836323534333637");
    chacha_klen = chacha_keystream.length;
    chacha_out = new Array(chacha_klen);

Test runner

Ready to run.

Testing in
TestOps/sec
SHA1
CryptoJS.SHA1("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367").toString()
ready
HMAC-SHA1
CryptoJS.HmacSHA1("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367", "12398763846536456783527654786254367").toString()
ready
MD5
CryptoJS.MD5("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367").toString()
ready
SHA-256
CryptoJS.SHA256("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367").toString();
ready
AES-128-CBC
CryptoJS.AES.encrypt("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367", key128, {
  iv: iv128,
  mode: CryptoJS.mode.CBC
}).toString();
ready
AES-256-CBC
CryptoJS.AES.encrypt("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367", key256, {
  iv: iv256,
  mode: CryptoJS.mode.CBC
}).toString();
ready
AES-256-OFB
CryptoJS.AES.encrypt("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367", key256, {
  iv: iv256,
  mode: CryptoJS.mode.OFB
}).toString();
 
ready
ChaCha20
chacha20_keystream(chacha_ctx, chacha_out, chacha_out, chacha_klen);
ready
crc
crc32_g("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367");
ready
hashCode
hashCode("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367");
ready
murmurhash3_128_gc
murmurhash3_128_gc("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367", 6802145);
ready
md5("ASomewhatLongMessageWithSomeSecretAndAWholeBunchOfNumbers12398763846536456783527654786254367");
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.