有很多MD5 JavaScript实现。 有人知道哪一个是最先进的,修复最多的,最快的吗?

我需要它来做这个工具。


当前回答

在选择库时,也要看它是否支持现代框架(如Bower),是否通过jslint,是否支持JQuery插件模型或模块系统(如AMD/RequireJS),以及是否正在积极开发中,是否有超过1个贡献者。有几个选项可以满足这些附加条件的一部分或全部:

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.

来自CryptoJS的例子:

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

在http://jsperf.com/md5-shootout/7上有上述库之间的性能比较。在我的机器上,当前的测试(不可否认是旧的)表明,如果您正在寻找速度,Spark MD5是您的最佳选择(普通JKM代码也是如此)。然而,如果你正在寻找更全面的库,那么CryptoJS是你最好的选择,尽管它比Spark MD5慢79%。然而,我想CryptoJS最终会达到同样的速度,因为它是一个更活跃的项目。

其他回答

Node.js有内置的支持

const crypto = require('crypto')
crypto.createHash('md5').update('hello world').digest('hex')

上面的代码段为字符串hello world计算MD5十六进制字符串

这种解决方案的优点是不需要安装额外的库。

我认为内置解决方案应该是最快的。如果不是,我们应该为Node.js项目创建issue/PR。

如果应用程序的性能受到MD5的Javascript实现的限制,那么您确实做错了什么。考虑架构变化(提示:减少使用MD5)

这是另一个比我的快25%的 前一个 : D

function MD5(r) { var o, e, n, f = [ -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426, -1473231341, -45705983, 1770035416, -1958414417, -42063, -1990404162, 1804603682, -40341101, -1502002290, 1236535329, -165796510, -1069501632, 643717713, -373897302, -701558691, 38016083, -660478335, -405537848, 568446438, -1019803690, -187363961, 1163531501, -1444681467, -51403784, 1735328473, -1926607734, -378558, -2022574463, 1839030562, -35309556, -1530992060, 1272893353, -155497632, -1094730640, 681279174, -358537222, -722521979, 76029189, -640364487, -421815835, 530742520, -995338651, -198630844, 1126891415, -1416354905, -57434055, 1700485571, -1894986606, -1051523, -2054922799, 1873313359, -30611744, -1560198380, 1309151649, -145523070, -1120210379, 718787259, -343485551 ], t = [ o = 1732584193, e = 4023233417, ~o, ~e ], c = [], a = unescape(encodeURI(r)) + "\u0080", d = a.length; for (r = --d / 4 + 2 | 15, c[--r] = 8 * d; ~d; ) c[d >> 2] |= a.charCodeAt(d) << 8 * d--; for (i = a = 0; i < r; i += 16) { for (d = t; 64 > a; d = [ n = d[3], o + ((n = d[0] + [ o & e | ~o & n, n & o | ~n & e, o ^ e ^ n, e ^ (o | ~n) ][d = a >> 4] + f[a] + ~~c[i | 15 & [ a, 5 * a + 1, 3 * a + 5, 7 * a ][d]]) << (d = [ 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21 ][4 * d + a++ % 4]) | n >>> -d), o, e ]) o = 0 | d[1], e = d[2]; for (a = 4; a; ) t[--a] += d[a]; } for (r = ""; 32 > a; ) r += (t[a >> 3] >> 4 * (1 ^ a++) & 15).toString(16); return r; } <!DOCTYPE html> <html> <body onload="md5.value=MD5(a.value);"> <form oninput="md5.value=MD5(a.value)">Enter String: <input type="string" id="a" name="a" value="https://www.zibri.org"></br></br>MD5:<output id="md5" name="md5" for="a"></output> </form> </body> </html>

更快的哈希应该可以通过在显卡上计算(在WebGL中实现哈希算法),正如关于SHA256的讨论:是否有可能在浏览器中使用用户的视频卡来计算SHA256哈希值?使用WebGL还是Flash?

我听说Joseph的Myers实现非常快。此外,他还有一篇关于Javascript优化的长篇文章,描述了他在编写实现时学到的东西。对于任何对性能javascript感兴趣的人来说,这是一本很好的读物。

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

他的MD5实现可以在这里找到