MD5 Shootout (v75)

Revision 75 of this benchmark created on


Description

This does a simple comparison of a number of JS implementations of the MD5 algorithm.

Preparation HTML

<script>
//testGet = new XMLHttpRequest(); testGet.open('GET', '/faq', false); //testGet.send();

var utf8TestCases = [
  {
    str: "色は匂へど 散りぬるを 我が世誰ぞ 常ならむ 有為の奥山 今日越えて 浅き夢見じ 酔ひもせず",
    md5: "7e626d44cd9610f2fef198156730e7d9"
  },
  {
    str: "Около 863 года братья Константин (Кирилл) Философ и Мефодий из Солуни (Салоники) по приказу византийского императора Михаила III упорядочили письменность для старославянского языка и использовали новую азбуку для перевода на славянский язык греческих религиозных текстов[2]:44. Долгое время дискуссионным оставался вопрос, была ли это кириллица (и в таком случае глаголицу считают тайнописью, появившейся после запрещения кириллицы) или глаголица — азбуки, различающиеся почти исключительно начертанием. В настоящее время в науке преобладает точка зрения, согласно которой глаголица первична, а кириллица вторична (в кириллице глаголические буквы заменены на известные греческие). Глаголица длительное время в несколько изменённом виде употреблялась у хорватов (до XVII в). Появление кириллицы, основывающейся на греческом уставном (торжественном) письме — унциале[2]:45, связывают с деятельностью болгарской школы книжников (после Кирилла и Мефодия). В частности, в житии св. Климента Охридского прямо пишется о создании им славянской письменности уже после Кирилла и Мефодия. Благодаря предыдущей деятельности братьев азбука получила широкое распространение в южнославянских землях, что привело в 885 году к запрещению её использования в церковной службе римским папой, боровшимся с результатами миссии Константина-Кирилла и Мефодия.",
    md5: '0eb597b81d8b5733764c05c6d2222114'
  }
];

var asciiTestCases = [
  {
    str: "hello",
    md5: '5d41402abc4b2a76b9719d911017c592'
  },
  {
    str: "This document describes the MD5 message-digest algorithm. The algorithm takes as input a message of arbitrary length and produces as output a 128-bit \"fingerprint\" or \"message digest\" of the input. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. The MD5 algorithm is intended for digital signature applications, where a large file must be \"compressed\" in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA. The MD5 algorithm is designed to be quite fast on 32-bit machines. In addition, the MD5 algorithm does not require any large substitution tables; the algorithm can be coded quite compactly.",
    md5: 'de606e6e4b0d6c549ac7abfa6a7c7a52'
  }
];
</script>

<script src="https://rawgit.com/gorhill/yamd5.js/master/yamd5.min.js"></script>
<script src="https://rawgit.com/iReal/FastMD5/master/lib/md5.js"></script>

<script>
// project home: https://github.com/gorhill/yamd5.js
// minified using http://refresh-sf.com/yui/
(function(j){var b=function(p,o,v,u,n,r,q){p+=(o&v|~o&u)+n+q;return((p<<r|p>>>(32-r))+o)|0};var i=function(p,o,v,u,n,r,q){p+=(o&u|v&~u)+n+q;
return((p<<r|p>>>(32-r))+o)|0};var f=function(p,o,v,u,n,r,q){p+=(o^v^u)+n+q;return((p<<r|p>>>(32-r))+o)|0};var m=function(p,o,v,u,n,r,q){p+=(v^(o|~u))+n+q;
return((p<<r|p>>>(32-r))+o)|0};var d=function(o,q){var p=o[0],n=o[1],s=o[2],r=o[3];p=b(p,n,s,r,q[0],7,-680876936);r=b(r,p,n,s,q[1],12,-389564586);
s=b(s,r,p,n,q[2],17,606105819);n=b(n,s,r,p,q[3],22,-1044525330);p=b(p,n,s,r,q[4],7,-176418897);r=b(r,p,n,s,q[5],12,1200080426);
s=b(s,r,p,n,q[6],17,-1473231341);n=b(n,s,r,p,q[7],22,-45705983);p=b(p,n,s,r,q[8],7,1770035416);r=b(r,p,n,s,q[9],12,-1958414417);
s=b(s,r,p,n,q[10],17,-42063);n=b(n,s,r,p,q[11],22,-1990404162);p=b(p,n,s,r,q[12],7,1804603682);r=b(r,p,n,s,q[13],12,-40341101);
s=b(s,r,p,n,q[14],17,-1502002290);n=b(n,s,r,p,q[15],22,1236535329);p=i(p,n,s,r,q[1],5,-165796510);r=i(r,p,n,s,q[6],9,-1069501632);
s=i(s,r,p,n,q[11],14,643717713);n=i(n,s,r,p,q[0],20,-373897302);p=i(p,n,s,r,q[5],5,-701558691);r=i(r,p,n,s,q[10],9,38016083);
s=i(s,r,p,n,q[15],14,-660478335);n=i(n,s,r,p,q[4],20,-405537848);p=i(p,n,s,r,q[9],5,568446438);r=i(r,p,n,s,q[14],9,-1019803690);
s=i(s,r,p,n,q[3],14,-187363961);n=i(n,s,r,p,q[8],20,1163531501);p=i(p,n,s,r,q[13],5,-1444681467);r=i(r,p,n,s,q[2],9,-51403784);
s=i(s,r,p,n,q[7],14,1735328473);n=i(n,s,r,p,q[12],20,-1926607734);p=f(p,n,s,r,q[5],4,-378558);r=f(r,p,n,s,q[8],11,-2022574463);
s=f(s,r,p,n,q[11],16,1839030562);n=f(n,s,r,p,q[14],23,-35309556);p=f(p,n,s,r,q[1],4,-1530992060);r=f(r,p,n,s,q[4],11,1272893353);
s=f(s,r,p,n,q[7],16,-155497632);n=f(n,s,r,p,q[10],23,-1094730640);p=f(p,n,s,r,q[13],4,681279174);r=f(r,p,n,s,q[0],11,-358537222);
s=f(s,r,p,n,q[3],16,-722521979);n=f(n,s,r,p,q[6],23,76029189);p=f(p,n,s,r,q[9],4,-640364487);r=f(r,p,n,s,q[12],11,-421815835);
s=f(s,r,p,n,q[15],16,530742520);n=f(n,s,r,p,q[2],23,-995338651);p=m(p,n,s,r,q[0],6,-198630844);r=m(r,p,n,s,q[7],10,1126891415);
s=m(s,r,p,n,q[14],15,-1416354905);n=m(n,s,r,p,q[5],21,-57434055);p=m(p,n,s,r,q[12],6,1700485571);r=m(r,p,n,s,q[3],10,-1894986606);
s=m(s,r,p,n,q[10],15,-1051523);n=m(n,s,r,p,q[1],21,-2054922799);p=m(p,n,s,r,q[8],6,1873313359);r=m(r,p,n,s,q[15],10,-30611744);
s=m(s,r,p,n,q[6],15,-1560198380);n=m(n,s,r,p,q[13],21,1309151649);p=m(p,n,s,r,q[4],6,-145523070);r=m(r,p,n,s,q[11],10,-1120210379);
s=m(s,r,p,n,q[2],15,718787259);n=m(n,s,r,p,q[9],21,-343485551);o[0]=(p+o[0])|0;o[1]=(n+o[1])|0;o[2]=(s+o[2])|0;o[3]=(r+o[3])|0
};var g="0123456789abcdef";var e=[];var c=function(o){var t=g;var r=e;var u,s,p;for(var q=0;q<4;q++){s=q*8;u=o[q];for(p=0;
p<8;p+=2){r[s+1+p]=t.charAt(u&15);u>>>=4;r[s+0+p]=t.charAt(u&15);u>>>=4}}return r.join("")};var l=function(){this._dataLength=0;
this._state=new Int32Array(4);this._buffer=new ArrayBuffer(68);this._bufferLength=0;this._buffer8=new Uint8Array(this._buffer,0,68);
this._buffer32=new Uint32Array(this._buffer,0,17);this.start()};var a=new Int32Array([1732584193,-271733879,-1732584194,271733878]);
var k=new Int32Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);l.prototype.appendStr=function(r){var o=this._buffer8;var n=this._buffer32;
var s=this._bufferLength;var q;for(var p=0;p<r.length;p++){q=r.charCodeAt(p);if(q<128){o[s++]=q}else{if(q<2048){o[s++]=(q>>>6)+192;
o[s++]=q&63|128}else{if(q<55296||q>56319){o[s++]=(q>>>12)+224;o[s++]=(q>>>6&63)|128;o[s++]=(q&63)|128}else{q=((q-55296)*1024)+(r.charCodeAt(++p)-56320)+65536;
if(q>1114111){throw"Unicode standard supports code points up to U+10FFFF"}o[s++]=(q>>>18)+240;o[s++]=(q>>>12&63)|128;o[s++]=(q>>>6&63)|128;
o[s++]=(q&63)|128}}}if(s>=64){this._dataLength+=64;d(this._state,n);s-=64;n[0]=n[16]}}this._bufferLength=s;return this};l.prototype.appendAsciiStr=function(r){var o=this._buffer8;
var n=this._buffer32;var s=this._bufferLength;var q,p=0;for(;;){q=Math.min(r.length-p,64-s);while(q--){o[s++]=r.charCodeAt(p++)
}if(s<64){break}this._dataLength+=64;d(this._state,n);s=0}this._bufferLength=s;return this};l.prototype.appendByteArray=function(p){var o=this._buffer8;
var n=this._buffer32;var s=this._bufferLength;var r,q=0;for(;;){r=Math.min(p.length-q,64-s);while(r--){o[s++]=p[q++]}if(s<64){break
}this._dataLength+=64;d(this._state,n);s=0}this._bufferLength=s;return this};l.prototype.start=function(){this._dataLength=0;
this._bufferLength=0;this._state.set(a);return this};l.prototype.end=function(t){var u=this._bufferLength;this._dataLength+=u;
var v=this._buffer8;v[u]=128;v[u+1]=v[u+2]=v[u+3]=0;var o=this._buffer32;var q=(u>>2)+1;o.set(k.subarray(q),q);if(u>55){d(this._state,o);
o.set(k)}var n=this._dataLength*8;if(n<=4294967295){o[14]=n}else{var r=n.toString(16).match(/(.*?)(.{0,8})$/);var s=parseInt(r[2],16);
var p=parseInt(r[1],16)||0;o[14]=s;o[15]=p}d(this._state,o);return !!t?this._state:c(this._state)};var h=new l();l.hashStr=function(o,n){return h.start().appendStr(o).end(n)
};l.hashAsciiStr=function(o,n){return h.start().appendAsciiStr(o).end(n)};if(l.hashStr("hello")!=="5d41402abc4b2a76b9719d911017c592"){console.error("YaMD5Old> this javascript engine does not support YaMD5Old. Sorry.")
}if(typeof j==="object"){j.YaMD5Old=l}return l})(this);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
YaMD5 (old)
var i = utf8TestCases.length;
while ( i-- ) {
  if ( utf8TestCases[i].md5 !== YaMD5Old.hashStr(utf8TestCases[i].str) ) {
    throw new Error('bad MD5: ' + i);
  }
}
i = asciiTestCases.length;
while ( i-- ) {
  if ( asciiTestCases[i].md5 !== YaMD5Old.hashStr(asciiTestCases[i].str) ) {
    throw new Error('bad MD5: ' + i);
  }
}
ready
YaMD5 (new)
var i = utf8TestCases.length;
while ( i-- ) {
  if ( utf8TestCases[i].md5 !== YaMD5.hashStr(utf8TestCases[i].str) ) {
    throw new Error('bad MD5: ' + i);
  }
}
i = asciiTestCases.length;
while ( i-- ) {
  if ( asciiTestCases[i].md5 !== YaMD5.hashStr(asciiTestCases[i].str) ) {
    throw new Error('bad MD5: ' + i);
  }
}
ready
FastMD5
var i = utf8TestCases.length;
while ( i-- ) {
  if ( utf8TestCases[i].md5 !== md5(utf8TestCases[i].str, false) ) {
    throw new Error('bad MD5: ' + i);
  }
}
i = asciiTestCases.length;
while ( i-- ) {
  if ( asciiTestCases[i].md5 !== md5(asciiTestCases[i].str, true) ) {
    throw new Error('bad MD5: ' + i);
  }
}
ready

Revisions

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