view at http://bl.ocks.org/2970392
Created
June 22, 2012 05:16
-
-
Save dsc/2970392 to your computer and use it in GitHub Desktop.
Performance of JS Hash Implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>JS Hash Test Suite</title> | |
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script> | |
<script src="http://d3js.org/d3.v2.js"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script src="http://www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script> | |
<script src="md5-izumo.js"></script> | |
<script src="md5-johnston.js"></script> | |
<script src="md5-meyers.js"></script> | |
<script src="murmurhash3_gc.js"></script> | |
<script src="tests.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From http://www.onicos.com/staff/iz/amuse/javascript/expert/md5.txt | |
/* md5.js - MD5 Message-Digest | |
* Copyright (C) 1999,2002 Masanao Izumo <[email protected]> | |
* Version: 2.0.0 | |
* LastModified: May 13 2002 | |
* | |
* This program is free software. You can redistribute it and/or modify | |
* it without any warranty. This library calculates the MD5 based on RFC1321. | |
* See RFC1321 for more information and algorism. | |
*/ | |
/* Interface: | |
* md5_128bits = MD5_hash(data); | |
* md5_hexstr = MD5_hexhash(data); | |
*/ | |
/* ChangeLog | |
* 2002/05/13: Version 2.0.0 released | |
* NOTICE: API is changed. | |
* 2002/04/15: Bug fix about MD5 length. | |
*/ | |
md5_izumo = (function(){ | |
// md5_T[i] = parseInt(Math.abs(Math.sin(i)) * 4294967296.0); | |
var MD5_T = [0x00000000, 0xd76aa478, 0xe8c7b756, 0x242070db, | |
0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, | |
0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, | |
0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, | |
0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, | |
0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, | |
0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, | |
0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, | |
0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, | |
0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, | |
0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, | |
0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, | |
0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, | |
0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, | |
0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, | |
0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, | |
0xeb86d391]; | |
var MD5_round1 = [[ 0, 7, 1], [ 1,12, 2], | |
[ 2,17, 3], [ 3,22, 4], | |
[ 4, 7, 5], [ 5,12, 6], | |
[ 6,17, 7], [ 7,22, 8], | |
[ 8, 7, 9], [ 9,12,10], | |
[10,17,11], [11,22,12], | |
[12, 7,13], [13,12,14], | |
[14,17,15], [15,22,16]]; | |
var MD5_round2 = [[ 1, 5,17], [ 6, 9,18], | |
[11,14,19], [ 0,20,20], | |
[ 5, 5,21], [10, 9,22], | |
[15,14,23], [ 4,20,24], | |
[ 9, 5,25], [14, 9,26], | |
[ 3,14,27], [ 8,20,28], | |
[13, 5,29], [ 2, 9,30], | |
[ 7,14,31], [12,20,32]]; | |
var MD5_round3 = [[ 5, 4,33], [ 8,11,34], | |
[11,16,35], [14,23,36], | |
[ 1, 4,37], [ 4,11,38], | |
[ 7,16,39], [10,23,40], | |
[13, 4,41], [ 0,11,42], | |
[ 3,16,43], [ 6,23,44], | |
[ 9, 4,45], [12,11,46], | |
[15,16,47], [ 2,23,48]]; | |
var MD5_round4 = [[ 0, 6,49], [ 7,10,50], | |
[14,15,51], [ 5,21,52], | |
[12, 6,53], [ 3,10,54], | |
[10,15,55], [ 1,21,56], | |
[ 8, 6,57], [15,10,58], | |
[ 6,15,59], [13,21,60], | |
[ 4, 6,61], [11,10,62], | |
[ 2,15,63], [ 9,21,64]]; | |
function MD5_F(x, y, z) { return (x & y) | (~x & z); } | |
function MD5_G(x, y, z) { return (x & z) | (y & ~z); } | |
function MD5_H(x, y, z) { return x ^ y ^ z; } | |
function MD5_I(x, y, z) { return y ^ (x | ~z); } | |
var MD5_round = [[MD5_F, MD5_round1], | |
[MD5_G, MD5_round2], | |
[MD5_H, MD5_round3], | |
[MD5_I, MD5_round4]]; | |
function MD5_pack(n32) { | |
return String.fromCharCode(n32 & 0xff) + | |
String.fromCharCode((n32 >>> 8) & 0xff) + | |
String.fromCharCode((n32 >>> 16) & 0xff) + | |
String.fromCharCode((n32 >>> 24) & 0xff); | |
} | |
function MD5_unpack(s4) { | |
return s4.charCodeAt(0) | | |
(s4.charCodeAt(1) << 8) | | |
(s4.charCodeAt(2) << 16) | | |
(s4.charCodeAt(3) << 24); | |
} | |
function MD5_number(n) { | |
while (n < 0) | |
n += 4294967296; | |
while (n > 4294967295) | |
n -= 4294967296; | |
return n; | |
} | |
function MD5_apply_round(x, s, f, abcd, r) { | |
var a, b, c, d; | |
var kk, ss, ii; | |
var t, u; | |
a = abcd[0]; | |
b = abcd[1]; | |
c = abcd[2]; | |
d = abcd[3]; | |
kk = r[0]; | |
ss = r[1]; | |
ii = r[2]; | |
u = f(s[b], s[c], s[d]); | |
t = s[a] + u + x[kk] + MD5_T[ii]; | |
t = MD5_number(t); | |
t = ((t<<ss) | (t>>>(32-ss))); | |
t += s[b]; | |
s[a] = MD5_number(t); | |
} | |
function MD5_hash(data) { | |
var abcd, x, state, s; | |
var len, index, padLen, f, r; | |
var i, j, k; | |
var tmp; | |
state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]; | |
len = data.length; | |
index = len & 0x3f; | |
padLen = (index < 56) ? (56 - index) : (120 - index); | |
if(padLen > 0) { | |
data += "\\x80"; | |
for(i = 0; i < padLen - 1; i++) | |
data += "\\x00"; | |
} | |
data += MD5_pack(len * 8); | |
data += MD5_pack(0); | |
len += padLen + 8; | |
abcd = [0, 1, 2, 3]; | |
x = new Array(16); | |
s = new Array(4); | |
for(k = 0; k < len; k += 64) { | |
for(i = 0, j = k; i < 16; i++, j += 4) { | |
x[i] = data.charCodeAt(j) | | |
(data.charCodeAt(j + 1) << 8) | | |
(data.charCodeAt(j + 2) << 16) | | |
(data.charCodeAt(j + 3) << 24); | |
} | |
for(i = 0; i < 4; i++) | |
s[i] = state[i]; | |
for(i = 0; i < 4; i++) { | |
f = MD5_round[i][0]; | |
r = MD5_round[i][1]; | |
for(j = 0; j < 16; j++) { | |
MD5_apply_round(x, s, f, abcd, r[j]); | |
tmp = abcd[0]; | |
abcd[0] = abcd[3]; | |
abcd[3] = abcd[2]; | |
abcd[2] = abcd[1]; | |
abcd[1] = tmp; | |
} | |
} | |
for(i = 0; i < 4; i++) { | |
state[i] += s[i]; | |
state[i] = MD5_number(state[i]); | |
} | |
} | |
return MD5_pack(state[0]) + | |
MD5_pack(state[1]) + | |
MD5_pack(state[2]) + | |
MD5_pack(state[3]); | |
} | |
function MD5_hexhash(data) { | |
var i, out, c; | |
var bit128; | |
bit128 = MD5_hash(data); | |
out = ""; | |
for(i = 0; i < 16; i++) { | |
c = bit128.charCodeAt(i); | |
out += "0123456789abcdef".charAt((c>>4) & 0xf); | |
out += "0123456789abcdef".charAt(c & 0xf); | |
} | |
return out; | |
} | |
return MD5_hexhash; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From http://membres-liglab.imag.fr/donsez/cours/exemplescourstechnoweb/js_securehash/md5.js | |
/* | |
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message | |
* Digest Algorithm, as defined in RFC 1321. | |
* Copyright (C) Paul Johnston 1999 - 2000. | |
* Updated by Greg Holt 2000 - 2001. | |
* See http://pajhome.org.uk/site/legal.html for details. | |
*/ | |
md5_johnston = (function(){ | |
/* | |
* Convert a 32-bit number to a hex string with ls-byte first | |
*/ | |
var hex_chr = "0123456789abcdef"; | |
function rhex(num) | |
{ | |
str = ""; | |
for(j = 0; j <= 3; j++) | |
str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + | |
hex_chr.charAt((num >> (j * 8)) & 0x0F); | |
return str; | |
} | |
/* | |
* Convert a string to a sequence of 16-word blocks, stored as an array. | |
* Append padding bits and the length, as described in the MD5 standard. | |
*/ | |
function str2blks_MD5(str) | |
{ | |
nblk = ((str.length + 8) >> 6) + 1; | |
blks = new Array(nblk * 16); | |
for(i = 0; i < nblk * 16; i++) blks[i] = 0; | |
for(i = 0; i < str.length; i++) | |
blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8); | |
blks[i >> 2] |= 0x80 << ((i % 4) * 8); | |
blks[nblk * 16 - 2] = str.length * 8; | |
return blks; | |
} | |
/* | |
* Add integers, wrapping at 2^32. This uses 16-bit operations internally | |
* to work around bugs in some JS interpreters. | |
*/ | |
function add(x, y) | |
{ | |
var lsw = (x & 0xFFFF) + (y & 0xFFFF); | |
var msw = (x >> 16) + (y >> 16) + (lsw >> 16); | |
return (msw << 16) | (lsw & 0xFFFF); | |
} | |
/* | |
* Bitwise rotate a 32-bit number to the left | |
*/ | |
function rol(num, cnt) | |
{ | |
return (num << cnt) | (num >>> (32 - cnt)); | |
} | |
/* | |
* These functions implement the basic operation for each round of the | |
* algorithm. | |
*/ | |
function cmn(q, a, b, x, s, t) | |
{ | |
return add(rol(add(add(a, q), add(x, t)), s), b); | |
} | |
function ff(a, b, c, d, x, s, t) | |
{ | |
return cmn((b & c) | ((~b) & d), a, b, x, s, t); | |
} | |
function gg(a, b, c, d, x, s, t) | |
{ | |
return cmn((b & d) | (c & (~d)), a, b, x, s, t); | |
} | |
function hh(a, b, c, d, x, s, t) | |
{ | |
return cmn(b ^ c ^ d, a, b, x, s, t); | |
} | |
function ii(a, b, c, d, x, s, t) | |
{ | |
return cmn(c ^ (b | (~d)), a, b, x, s, t); | |
} | |
/* | |
* Take a string and return the hex representation of its MD5. | |
*/ | |
function calcMD5(str) | |
{ | |
x = str2blks_MD5(str); | |
a = 1732584193; | |
b = -271733879; | |
c = -1732584194; | |
d = 271733878; | |
for(i = 0; i < x.length; i += 16) | |
{ | |
olda = a; | |
oldb = b; | |
oldc = c; | |
oldd = d; | |
a = ff(a, b, c, d, x[i+ 0], 7 , -680876936); | |
d = ff(d, a, b, c, x[i+ 1], 12, -389564586); | |
c = ff(c, d, a, b, x[i+ 2], 17, 606105819); | |
b = ff(b, c, d, a, x[i+ 3], 22, -1044525330); | |
a = ff(a, b, c, d, x[i+ 4], 7 , -176418897); | |
d = ff(d, a, b, c, x[i+ 5], 12, 1200080426); | |
c = ff(c, d, a, b, x[i+ 6], 17, -1473231341); | |
b = ff(b, c, d, a, x[i+ 7], 22, -45705983); | |
a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416); | |
d = ff(d, a, b, c, x[i+ 9], 12, -1958414417); | |
c = ff(c, d, a, b, x[i+10], 17, -42063); | |
b = ff(b, c, d, a, x[i+11], 22, -1990404162); | |
a = ff(a, b, c, d, x[i+12], 7 , 1804603682); | |
d = ff(d, a, b, c, x[i+13], 12, -40341101); | |
c = ff(c, d, a, b, x[i+14], 17, -1502002290); | |
b = ff(b, c, d, a, x[i+15], 22, 1236535329); | |
a = gg(a, b, c, d, x[i+ 1], 5 , -165796510); | |
d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632); | |
c = gg(c, d, a, b, x[i+11], 14, 643717713); | |
b = gg(b, c, d, a, x[i+ 0], 20, -373897302); | |
a = gg(a, b, c, d, x[i+ 5], 5 , -701558691); | |
d = gg(d, a, b, c, x[i+10], 9 , 38016083); | |
c = gg(c, d, a, b, x[i+15], 14, -660478335); | |
b = gg(b, c, d, a, x[i+ 4], 20, -405537848); | |
a = gg(a, b, c, d, x[i+ 9], 5 , 568446438); | |
d = gg(d, a, b, c, x[i+14], 9 , -1019803690); | |
c = gg(c, d, a, b, x[i+ 3], 14, -187363961); | |
b = gg(b, c, d, a, x[i+ 8], 20, 1163531501); | |
a = gg(a, b, c, d, x[i+13], 5 , -1444681467); | |
d = gg(d, a, b, c, x[i+ 2], 9 , -51403784); | |
c = gg(c, d, a, b, x[i+ 7], 14, 1735328473); | |
b = gg(b, c, d, a, x[i+12], 20, -1926607734); | |
a = hh(a, b, c, d, x[i+ 5], 4 , -378558); | |
d = hh(d, a, b, c, x[i+ 8], 11, -2022574463); | |
c = hh(c, d, a, b, x[i+11], 16, 1839030562); | |
b = hh(b, c, d, a, x[i+14], 23, -35309556); | |
a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060); | |
d = hh(d, a, b, c, x[i+ 4], 11, 1272893353); | |
c = hh(c, d, a, b, x[i+ 7], 16, -155497632); | |
b = hh(b, c, d, a, x[i+10], 23, -1094730640); | |
a = hh(a, b, c, d, x[i+13], 4 , 681279174); | |
d = hh(d, a, b, c, x[i+ 0], 11, -358537222); | |
c = hh(c, d, a, b, x[i+ 3], 16, -722521979); | |
b = hh(b, c, d, a, x[i+ 6], 23, 76029189); | |
a = hh(a, b, c, d, x[i+ 9], 4 , -640364487); | |
d = hh(d, a, b, c, x[i+12], 11, -421815835); | |
c = hh(c, d, a, b, x[i+15], 16, 530742520); | |
b = hh(b, c, d, a, x[i+ 2], 23, -995338651); | |
a = ii(a, b, c, d, x[i+ 0], 6 , -198630844); | |
d = ii(d, a, b, c, x[i+ 7], 10, 1126891415); | |
c = ii(c, d, a, b, x[i+14], 15, -1416354905); | |
b = ii(b, c, d, a, x[i+ 5], 21, -57434055); | |
a = ii(a, b, c, d, x[i+12], 6 , 1700485571); | |
d = ii(d, a, b, c, x[i+ 3], 10, -1894986606); | |
c = ii(c, d, a, b, x[i+10], 15, -1051523); | |
b = ii(b, c, d, a, x[i+ 1], 21, -2054922799); | |
a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359); | |
d = ii(d, a, b, c, x[i+15], 10, -30611744); | |
c = ii(c, d, a, b, x[i+ 6], 15, -1560198380); | |
b = ii(b, c, d, a, x[i+13], 21, 1309151649); | |
a = ii(a, b, c, d, x[i+ 4], 6 , -145523070); | |
d = ii(d, a, b, c, x[i+11], 10, -1120210379); | |
c = ii(c, d, a, b, x[i+ 2], 15, 718787259); | |
b = ii(b, c, d, a, x[i+ 9], 21, -343485551); | |
a = add(a, olda); | |
b = add(b, oldb); | |
c = add(c, oldc); | |
d = add(d, oldd); | |
} | |
return rhex(a) + rhex(b) + rhex(c) + rhex(d); | |
} | |
return calcMD5; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From: https://raw.github.com/wbond/md5-js/master/md5.js | |
/*! | |
* Joseph Myer's md5() algorithm wrapped in a self-invoked function to prevent | |
* global namespace polution, modified to hash unicode characters as UTF-8. | |
* | |
* Copyright 1999-2010, Joseph Myers, Paul Johnston, Greg Holt, Will Bond <[email protected]> | |
* http://www.myersdaily.org/joseph/javascript/md5-text.html | |
* http://pajhome.org.uk/crypt/md5 | |
* | |
* Released under the BSD license | |
* http://www.opensource.org/licenses/bsd-license | |
*/ | |
md5_meyers = (function() { | |
function md5cycle(x, k) { | |
var a = x[0], b = x[1], c = x[2], d = x[3]; | |
a = ff(a, b, c, d, k[0], 7, -680876936); | |
d = ff(d, a, b, c, k[1], 12, -389564586); | |
c = ff(c, d, a, b, k[2], 17, 606105819); | |
b = ff(b, c, d, a, k[3], 22, -1044525330); | |
a = ff(a, b, c, d, k[4], 7, -176418897); | |
d = ff(d, a, b, c, k[5], 12, 1200080426); | |
c = ff(c, d, a, b, k[6], 17, -1473231341); | |
b = ff(b, c, d, a, k[7], 22, -45705983); | |
a = ff(a, b, c, d, k[8], 7, 1770035416); | |
d = ff(d, a, b, c, k[9], 12, -1958414417); | |
c = ff(c, d, a, b, k[10], 17, -42063); | |
b = ff(b, c, d, a, k[11], 22, -1990404162); | |
a = ff(a, b, c, d, k[12], 7, 1804603682); | |
d = ff(d, a, b, c, k[13], 12, -40341101); | |
c = ff(c, d, a, b, k[14], 17, -1502002290); | |
b = ff(b, c, d, a, k[15], 22, 1236535329); | |
a = gg(a, b, c, d, k[1], 5, -165796510); | |
d = gg(d, a, b, c, k[6], 9, -1069501632); | |
c = gg(c, d, a, b, k[11], 14, 643717713); | |
b = gg(b, c, d, a, k[0], 20, -373897302); | |
a = gg(a, b, c, d, k[5], 5, -701558691); | |
d = gg(d, a, b, c, k[10], 9, 38016083); | |
c = gg(c, d, a, b, k[15], 14, -660478335); | |
b = gg(b, c, d, a, k[4], 20, -405537848); | |
a = gg(a, b, c, d, k[9], 5, 568446438); | |
d = gg(d, a, b, c, k[14], 9, -1019803690); | |
c = gg(c, d, a, b, k[3], 14, -187363961); | |
b = gg(b, c, d, a, k[8], 20, 1163531501); | |
a = gg(a, b, c, d, k[13], 5, -1444681467); | |
d = gg(d, a, b, c, k[2], 9, -51403784); | |
c = gg(c, d, a, b, k[7], 14, 1735328473); | |
b = gg(b, c, d, a, k[12], 20, -1926607734); | |
a = hh(a, b, c, d, k[5], 4, -378558); | |
d = hh(d, a, b, c, k[8], 11, -2022574463); | |
c = hh(c, d, a, b, k[11], 16, 1839030562); | |
b = hh(b, c, d, a, k[14], 23, -35309556); | |
a = hh(a, b, c, d, k[1], 4, -1530992060); | |
d = hh(d, a, b, c, k[4], 11, 1272893353); | |
c = hh(c, d, a, b, k[7], 16, -155497632); | |
b = hh(b, c, d, a, k[10], 23, -1094730640); | |
a = hh(a, b, c, d, k[13], 4, 681279174); | |
d = hh(d, a, b, c, k[0], 11, -358537222); | |
c = hh(c, d, a, b, k[3], 16, -722521979); | |
b = hh(b, c, d, a, k[6], 23, 76029189); | |
a = hh(a, b, c, d, k[9], 4, -640364487); | |
d = hh(d, a, b, c, k[12], 11, -421815835); | |
c = hh(c, d, a, b, k[15], 16, 530742520); | |
b = hh(b, c, d, a, k[2], 23, -995338651); | |
a = ii(a, b, c, d, k[0], 6, -198630844); | |
d = ii(d, a, b, c, k[7], 10, 1126891415); | |
c = ii(c, d, a, b, k[14], 15, -1416354905); | |
b = ii(b, c, d, a, k[5], 21, -57434055); | |
a = ii(a, b, c, d, k[12], 6, 1700485571); | |
d = ii(d, a, b, c, k[3], 10, -1894986606); | |
c = ii(c, d, a, b, k[10], 15, -1051523); | |
b = ii(b, c, d, a, k[1], 21, -2054922799); | |
a = ii(a, b, c, d, k[8], 6, 1873313359); | |
d = ii(d, a, b, c, k[15], 10, -30611744); | |
c = ii(c, d, a, b, k[6], 15, -1560198380); | |
b = ii(b, c, d, a, k[13], 21, 1309151649); | |
a = ii(a, b, c, d, k[4], 6, -145523070); | |
d = ii(d, a, b, c, k[11], 10, -1120210379); | |
c = ii(c, d, a, b, k[2], 15, 718787259); | |
b = ii(b, c, d, a, k[9], 21, -343485551); | |
x[0] = add32(a, x[0]); | |
x[1] = add32(b, x[1]); | |
x[2] = add32(c, x[2]); | |
x[3] = add32(d, x[3]); | |
} | |
function cmn(q, a, b, x, s, t) { | |
a = add32(add32(a, q), add32(x, t)); | |
return add32((a << s) | (a >>> (32 - s)), b); | |
} | |
function ff(a, b, c, d, x, s, t) { | |
return cmn((b & c) | ((~b) & d), a, b, x, s, t); | |
} | |
function gg(a, b, c, d, x, s, t) { | |
return cmn((b & d) | (c & (~d)), a, b, x, s, t); | |
} | |
function hh(a, b, c, d, x, s, t) { | |
return cmn(b ^ c ^ d, a, b, x, s, t); | |
} | |
function ii(a, b, c, d, x, s, t) { | |
return cmn(c ^ (b | (~d)), a, b, x, s, t); | |
} | |
function md51(s) { | |
// Converts the string to UTF-8 "bytes" when necessary | |
if (/[\x80-\xFF]/.test(s)) { | |
s = unescape(encodeURI(s)); | |
} | |
txt = ''; | |
var n = s.length, state = [1732584193, -271733879, -1732584194, 271733878], i; | |
for (i = 64; i <= s.length; i += 64) { | |
md5cycle(state, md5blk(s.substring(i - 64, i))); | |
} | |
s = s.substring(i - 64); | |
var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
for (i = 0; i < s.length; i++) | |
tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3); | |
tail[i >> 2] |= 0x80 << ((i % 4) << 3); | |
if (i > 55) { | |
md5cycle(state, tail); | |
for (i = 0; i < 16; i++) tail[i] = 0; | |
} | |
tail[14] = n * 8; | |
md5cycle(state, tail); | |
return state; | |
} | |
function md5blk(s) { /* I figured global was faster. */ | |
var md5blks = [], i; /* Andy King said do it this way. */ | |
for (i = 0; i < 64; i += 4) { | |
md5blks[i >> 2] = s.charCodeAt(i) + | |
(s.charCodeAt(i + 1) << 8) + | |
(s.charCodeAt(i + 2) << 16) + | |
(s.charCodeAt(i + 3) << 24); | |
} | |
return md5blks; | |
} | |
var hex_chr = '0123456789abcdef'.split(''); | |
function rhex(n) { | |
var s = '', j = 0; | |
for (; j < 4; j++) | |
s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + | |
hex_chr[(n >> (j * 8)) & 0x0F]; | |
return s; | |
} | |
function hex(x) { | |
for (var i = 0; i < x.length; i++) | |
x[i] = rhex(x[i]); | |
return x.join(''); | |
} | |
/* this function is much faster, so if possible we use it. Some IEs are the | |
only ones I know of that need the idiotic second function, generated by an | |
if clause. */ | |
function add32(a, b) { | |
return (a + b) & 0xFFFFFFFF; | |
} | |
function md5(s) { | |
return hex(md51(s)); | |
}; | |
if (md5('hello') != '5d41402abc4b2a76b9719d911017c592') { | |
function add32(x, y) { | |
var lsw = (x & 0xFFFF) + (y & 0xFFFF), | |
msw = (x >> 16) + (y >> 16) + (lsw >> 16); | |
return (msw << 16) | (lsw & 0xFFFF); | |
} | |
} | |
return md5; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From https://raw.github.com/garycourt/murmurhash-js/master/murmurhash3_gc.js | |
/** | |
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) | |
* | |
* @author <a href="mailto:[email protected]">Gary Court</a> | |
* @see http://github.com/garycourt/murmurhash-js | |
* @author <a href="mailto:[email protected]">Austin Appleby</a> | |
* @see http://sites.google.com/site/murmurhash/ | |
* | |
* @param {string} key ASCII only | |
* @param {number} seed Positive integer only | |
* @return {number} 32-bit positive integer hash | |
*/ | |
function murmurhash3_32_gc(key, seed) { | |
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i; | |
remainder = key.length & 3; // key.length % 4 | |
bytes = key.length - remainder; | |
h1 = seed; | |
c1 = 0xcc9e2d51; | |
c2 = 0x1b873593; | |
i = 0; | |
while (i < bytes) { | |
k1 = | |
((key.charCodeAt(i) & 0xff)) | | |
((key.charCodeAt(++i) & 0xff) << 8) | | |
((key.charCodeAt(++i) & 0xff) << 16) | | |
((key.charCodeAt(++i) & 0xff) << 24); | |
++i; | |
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff; | |
k1 = (k1 << 15) | (k1 >>> 17); | |
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff; | |
h1 ^= k1; | |
h1 = (h1 << 13) | (h1 >>> 19); | |
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff; | |
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16)); | |
} | |
k1 = 0; | |
switch (remainder) { | |
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16; | |
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8; | |
case 1: k1 ^= (key.charCodeAt(i) & 0xff); | |
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff; | |
k1 = (k1 << 15) | (k1 >>> 17); | |
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff; | |
h1 ^= k1; | |
} | |
h1 ^= key.length; | |
h1 ^= h1 >>> 16; | |
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; | |
h1 ^= h1 >>> 13; | |
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; | |
h1 ^= h1 >>> 16; | |
return h1 >>> 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var q = d3.scale.quantize().range( d3.range(26).map(function(i){ return String.fromCharCode(97+i); }) ) | |
, strings = d3.range(100).map(function(i){ return d3.range(8).map(function(){ return q(Math.random()); }).join(''); }) | |
; | |
function it(fn, cxt){ | |
return function(x){ return fn.call(cxt != null ? cxt : this, x); }; | |
} | |
console.log('strings', strings) | |
JSLitmus.test('md5_izumo', function(){ | |
return strings.map(it(md5_izumo)); | |
}); | |
JSLitmus.test('md5_johnston', function(){ | |
return strings.map(it(md5_johnston)); | |
}); | |
JSLitmus.test('md5_meyers', function(){ | |
return strings.map(it(md5_meyers)); | |
}); | |
JSLitmus.test('murmurhash', function(){ | |
return strings.map(it(murmurhash3_32_gc)); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment