JavaScript 中最快的 MD5 实现
有许多 MD5 JavaScript 实现。 有谁知道哪一个是最先进、修复错误最多且速度最快的?
我需要它来使用这个工具。
There are many MD5 JavaScript implementations out there.
Does anybody know which one is the most advanced, most bugfixed and fastest?
I need it for this tool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
我听说 Joseph 的 Myers 实施速度相当快。此外,他还有一个 关于 Javascript 优化的长篇文章描述了他在编写实现时学到的东西。对于任何对高性能 JavaScript 感兴趣的人来说,这都是一本很好的读物。
http:// /www.webreference.com/programming/javascript/jkm3/
他的 MD5 实现可以在 此处
I've heard Joseph's Myers implementation is quite fast. Additionally, he has a lengthy article on Javascript optimization describing what he learned while writing his implementation. It's a good read for anyone interested in performant javascript.
http://www.webreference.com/programming/javascript/jkm3/
His MD5 implementation can be found here
在这种情况下我建议你使用 CryptoJS。
基本上,CryptoJS 是一个不断增长的标准和安全加密算法的集合,使用最佳实践和模式在 JavaScript 中实现。它们速度很快,并且具有一致且简单的界面。
因此,如果您想计算密码字符串的 MD5 哈希值,请执行以下操作:
因此,此脚本会将密码字符串的哈希值发布到服务器。
有关其他哈希计算算法的更多信息和支持,您可以访问:
http://code.google。 com/p/crypto-js/
I would suggest you use CryptoJS in this case.
Basically CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.
So if you want to calculate the MD5 hash of your password string then do as follows:
So this script will post the hash of your password string to the server.
For further info and support on other hash calculating algorithms you can visit:
http://code.google.com/p/crypto-js/
我最喜欢的 md5
这个有 42 行长,横向可容纳 120 个字符,看起来不错。快吗?嗯 - 它足够快,并且与所有其他 JS 实现大致相同。但看着所有这些排列整齐的柱子却令人感到奇怪的满足。别否认。
另一个完全不相关的事实:它有 4000 字节长。这可能看起来毫无意义,但知道感觉很好。你看看它就知道了。你属于那些只知道的人的秘密俱乐部。生活永远不会一样。相信我。
另外,谢谢保罗·约翰斯顿和格雷格·霍尔特。
还有道格拉斯·亚当斯,所有的鱼。
Stackblitz 演示
My favorite md5
This one is 42 lines long, fits in 120 characters horizontally, and looks good. Is it fast? Well - it's fast enough and it's approximately the same as all other JS implementations. But it's just oddly satisfying to look at all those nicely aligned columns. Don't deny.
Another absolutely irrelevant fact: it's 4000 bytes long. It may seem pointless but it feels good to just know. You look at it and you know. You belong to the secret club of Those Who Just Know. Life will never be the same. Trust me.
Also, thank you Paul Johnston and Greg Holt.
And Douglas Adams, for all the fish.
Stackblitz demo
Node.js 具有内置支持
上面的代码片段计算字符串
hello world
的 MD5 十六进制字符串此解决方案的优点是您不需要安装额外的库。
我认为内置解决方案应该是最快的。如果没有,我们应该为 Node.js 项目创建问题/PR。
Node.js has built-in support
Code snippet above computes MD5 hex string for string
hello world
The advantage of this solution is you don't need to install additional library.
I think built in solution should be the fastest. If not, we should create issue/PR for the Node.js project.
在选择库时,除了正在积极开发中并拥有超过 1 个贡献者之外,查看它是否支持 Bower 等现代框架、通过 jslint、支持 JQuery 插件模型或 AMD/RequireJS 等模块系统也很重要。有几个选项可以满足部分或全部这些附加条件:
CryptoJS 的示例:
http://jsperf.com/md5-shootout/ 上有上述库之间的性能比较7.。在我的机器上,当前的测试(确实很旧)表明,如果您正在寻找速度,Spark MD5 是您最好的选择(纯 JKM 代码也是如此)。然而,如果您正在寻找更全面的库,那么 CryptoJS 是您最好的选择,尽管它比 Spark MD5 慢 79%。不过我想 CryptoJS 最终会达到相同的速度,因为它是一个更活跃的项目。
While selecting library it's also important to see if it supports modern frameworks such as Bower, passes jslint, supports plugin model for JQuery or module systems such as AMD/RequireJS in addition to being in active development and have more than 1 contributors. There are couple of options that satisfies some or all of these additional criteria:
Example from CryptoJS:
There is a performance comparison between above libraries at http://jsperf.com/md5-shootout/7. On my machine current tests (which are admittedly old) shows that if you are looking for speed Spark MD5 is your best bet (and so is plain JKM code). However if you looking for more comprehensive library then CryptoJS is your best bet although it is 79% slower than Spark MD5. However I would imagine CryptoJS would eventually achieve same speed as it is bit more active project.
截至 2020 年最快的 MD5 实现可能是用 WASM(Web Assembly)编写的。
hash-wasm 是一个在 WASM 中实现 MD5 哈希的库。
您可以在此处找到基准测试。
您可以使用 npm: 安装它,
或者仅添加脚本标记,
然后使用
hashwasm
全局变量。示例:
输出
As of 2020 the fastest MD5 implementation is probably written in WASM (Web Assembly).
hash-wasm is a library that implements MD5 hash in WASM.
You can find the benchmarks here.
You can either install it with npm:
or just add a script tag
then use the
hashwasm
global variable.Example:
outputs
我真的不记得我从哪里得到这个。我在网上到处搜索,我找到的唯一参考资料来自这篇文章。奇怪的。
如果有人有关于此实现起源的信息,请告诉我。
或者您也可以尝试这个新的,速度提高了 25%:https://stackoverflow.com/a/74186696/236062 :D
I really can't remember where did I get this from.. I searched everywhere on the net and the only references I find are from this post. Strange.
If anyone has infos about this implementation origin, let me know.
Or you can try this new one which is 25% faster: https://stackoverflow.com/a/74186696/236062 :D
我只需要支持支持类型化数组的 HTML5 浏览器(DataView、ArrayBuffer 等)
我想我采用了 Joseph Myers 代码并对其进行了修改以支持传入 Uint8Array。我没有注意到所有的改进,并且可能仍然有一些 char() 数组工件可以改进。我需要这个来添加到 PouchDB 项目中。
I only need to support HTML5 browsers that support typed arrays (DataView, ArrayBuffer, etc.)
I think I took the Joseph Myers code and modified it to support passing in a Uint8Array. I did not catch all the improvements, and there are still probably some char() array artifacts that can be improved on. I needed this for adding to the PouchDB project.
目前最快的 md5 实现(基于 Joseph Myers 的代码):
https://github.com/iReal/FastMD5< /a>
jsPerf 比较: http://jsperf.com/md5-shootout/63
Currently the fastest implementation of md5 (based on Joseph Myers' code):
https://github.com/iReal/FastMD5
jsPerf comparaison: http://jsperf.com/md5-shootout/63
我编写了测试来比较几种 JavaScript 哈希实现,包括这里提到的大多数 MD5 实现。
要运行测试,请转到 http://brillout.github.io/test- javascript-hash-implementations/ 稍等一下。
似乎 YaMD5 实现了 R.希尔的回答是最快的。
I wrote tests to compare several JavaScript hash implementations, including most MD5 implementations mentioned here.
To run the tests, go to http://brillout.github.io/test-javascript-hash-implementations/ and wait a bit.
It seems that the YaMD5 implementation of R. Hill's answer is the fastest.
令我困扰的是我找不到既快速又支持 Unicode 字符串的实现。
因此,我制作了一个支持 Unicode 字符串的版本,并且仍然显示速度更快(在撰写本文时)目前最快的 ascii-only-strings 实现:
https://github.com/gorhill/yamd5.js
基于 Joseph Myers 的代码,但使用 TypedArrays 以及其他改进。
It bothered me that I could not find an implementation which is both fast and support Unicode strings.
So I made one which supports Unicode strings and still shows as faster (at time of writing) than the currently fastest ascii-only-strings implementations:
https://github.com/gorhill/yamd5.js
Based on Joseph Myers' code, but uses TypedArrays, plus other improvements.
我们选择另一款,它比我的快 25%
上一篇
:D
Here we go with another one which is 25% faster than my
previous one
:D
js-md5 支持 UTF-8 字符串、数组、ArrayBuffer、AMD...
并且速度很快。 jsperf
js-md5 supports UTF-8 string, array, ArrayBuffer, AMD....
and fast. jsperf
这是 @dkellner 和 @Eonasdan 实现的 ES6 版本的 md5:
Here's the ES6 version of the md5 implementation by @dkellner and @Eonasdan:
也许这个包很有用
https://www.npmjs.com/package/pure-md5
Maybe this package was useful
https://www.npmjs.com/package/pure-md5
为什么不尝试http://phpjs.org/functions/md5/?
不幸的是,任何模拟脚本的性能都受到限制,但是这可以呈现真实的 md5 哈希值。尽管我建议不要使用 md5 作为密码,因为它是一个快速渲染的哈希值。
Why not try http://phpjs.org/functions/md5/?
Unfortunately performance is limited with any emulated script, however this can render real md5 hash. Although I would advice against using md5 for passwords, as it is a fast-rendered hash.
通过在显卡上计算(在 WebGL 中实现散列算法)应该可以实现更快的散列,正如有关 SHA256 的讨论:是否可以使用用户的视频卡在浏览器中计算 sha256 哈希值,例如使用 WebGL 还是 Flash?
Much faster hashing should be possible by calculating on graphic card (implement hashing algorithm in WebGL), as discussed there about SHA256: Is it possible to calculate sha256 hashes in the browser using the user's video card, eg. by using WebGL or Flash?
强烈推荐SparkMD5,它支持多种不同类型的数据获取md5和在大多数情况下都有良好的表现。与其他 md5 库相比,它的每周下载量最高。
Blob 数据例如:
在 Chrome v117 中,比 js-md5 更好。
Strongly recommend the SparkMD5, which is support many different type of data to get the md5 and with good performance in most situation. And it has the highest weekly downloads compare to other md5 libs.
Blob data for example:
In the Chrome v117, better than the js-md5.
您还可以检查我的 md5 实现。应该是大约。与上面发布的其他相同。不幸的是,性能受到内部循环的限制,无法进一步优化。
You could also check my md5 implementation. It should be approx. the same as the other posted above. Unfortunately, the performance is limited by the inner loop which is impossible to optimize more.
MD5 基准
测试的获胜者是 Javascript-MD5 在 https://jsben.ch/imzpt 或查看
jsfiddle.net/w731ed58/
The winner for the MD5 benchmark is Javascript-MD5
Test it on https://jsben.ch/imzpt or see on
jsfiddle.net/w731ed58/
如果您的应用程序的性能受到 MD5 的 Javascript 实现的限制,那么您确实做错了。考虑架构更改(提示:减少使用 MD5)
If the performance of your application is limited by a Javascript implementation of MD5, then you're really doing something wrong. Consider an architectural change (Hint: use MD5 less often)