如何在 JavaScript/Node.js 中生成 MD5 文件哈希?
Posted
技术标签:
【中文标题】如何在 JavaScript/Node.js 中生成 MD5 文件哈希?【英文标题】:How to generate an MD5 file hash in JavaScript/Node.js? 【发布时间】:2021-11-02 01:17:54 【问题描述】:如何为这段代码写functionToGenerateMD5hash
?我已经有fileVideo
,我需要通过点击按钮将对应的md5哈希发送到服务器。
$("#someButton").click(function()
var fr = new FileReader();
fr.onload = function(e)
string md5 = functionToGenerateMD5hash(e.target.result);
// send md5 here
;
fr.readAsArrayBuffer(fileVideo);
另外,你能用它在 Node.js 中生成 MD5 哈希吗?
【问题讨论】:
这是一个***示例:***.com/questions/768268/… 什么例子?我找不到任何关于如何使用 js 获取文件的 md5 哈希的示例。 How to calculate md5 hash of a file using javascript 的可能重复项 ... 并强烈相关:[如何在 Javascript 中为非常大的文件生成校验和并转换为 64 位而不会使 RAM 溢出? ](***.com/q/51987434/514235) 【参考方案1】:如果您不想使用库或其他东西,可以使用这种原生 javascript 方法:
var MD5 = function(d)var r = M(V(Y(X(d),8*d.length)));return r.toLowerCase();function M(d)for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return ffunction X(d)for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _function V(d)for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _function Y(d,_)d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16)var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7],22,-45705983),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+8],7,1770035416),f,r,d[n+9],12,-1958414417),m,f,d[n+10],17,-42063),i,m,d[n+11],22,-1990404162),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+12],7,1804603682),f,r,d[n+13],12,-40341101),m,f,d[n+14],17,-1502002290),i,m,d[n+15],22,1236535329),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+1],5,-165796510),f,r,d[n+6],9,-1069501632),m,f,d[n+11],14,643717713),i,m,d[n+0],20,-373897302),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+5],5,-701558691),f,r,d[n+10],9,38016083),m,f,d[n+15],14,-660478335),i,m,d[n+4],20,-405537848),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+9],5,568446438),f,r,d[n+14],9,-1019803690),m,f,d[n+3],14,-187363961),i,m,d[n+8],20,1163531501),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+13],5,-1444681467),f,r,d[n+2],9,-51403784),m,f,d[n+7],14,1735328473),i,m,d[n+12],20,-1926607734),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+5],4,-378558),f,r,d[n+8],11,-2022574463),m,f,d[n+11],16,1839030562),i,m,d[n+14],23,-35309556),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+1],4,-1530992060),f,r,d[n+4],11,1272893353),m,f,d[n+7],16,-155497632),i,m,d[n+10],23,-1094730640),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+13],4,681279174),f,r,d[n+0],11,-358537222),m,f,d[n+3],16,-722521979),i,m,d[n+6],23,76029189),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+9],4,-640364487),f,r,d[n+12],11,-421815835),m,f,d[n+15],16,530742520),i,m,d[n+2],23,-995338651),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+0],6,-198630844),f,r,d[n+7],10,1126891415),m,f,d[n+14],15,-1416354905),i,m,d[n+5],21,-57434055),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+12],6,1700485571),f,r,d[n+3],10,-1894986606),m,f,d[n+10],15,-1051523),i,m,d[n+1],21,-2054922799),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+8],6,1873313359),f,r,d[n+15],10,-30611744),m,f,d[n+6],15,-1560198380),i,m,d[n+13],21,1309151649),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+4],6,-145523070),f,r,d[n+11],10,-1120210379),m,f,d[n+2],15,718787259),i,m,d[n+9],21,-343485551),m=safe_add(m,h),f=safe_add(f,t),r=safe_add(r,g),i=safe_add(i,e)return Array(m,f,r,i)function md5_cmn(d,_,m,f,r,i)return safe_add(bit_rol(safe_add(safe_add(_,d),safe_add(f,i)),r),m)function md5_ff(d,_,m,f,r,i,n)return md5_cmn(_&m|~_&f,d,_,r,i,n)function md5_gg(d,_,m,f,r,i,n)return md5_cmn(_&f|m&~f,d,_,r,i,n)function md5_hh(d,_,m,f,r,i,n)return md5_cmn(_^m^f,d,_,r,i,n)function md5_ii(d,_,m,f,r,i,n)return md5_cmn(m^(_|~f),d,_,r,i,n)function safe_add(d,_)var m=(65535&d)+(65535&_);return(d>>16)+(_>>16)+(m>>16)<<16|65535&mfunction bit_rol(d,_)return d<<_|d>>>32-_
/** NORMAL words**/
var value = 'test';
var result = MD5(value);
document.body.innerhtml = 'hash - normal words: ' + result;
/** NON ENGLISH words**/
value = 'מבחן'
//unescape() can be deprecated for the new browser versions
result = MD5(unescape(encodeURIComponent(value)));
document.body.innerHTML += '<br><br>hash - non english words: ' + result;
对于非英语单词,您可能需要使用unescape() 和encodeURIComponent() 方法。
【讨论】:
有没有办法以增量方式进行,即 100 MB 文件分为 100 x 1 MB 块,然后组合它们的校验和。你也有这样的 SHA256 算法吗?例如。 ***.com/a/8888429/514235 unicode字符串的解决方案:unescape(encodeURIComponent('unicode string'))
@mir88 你能分享一个例子吗?【参考方案2】:
CryptoJS 是一个可以生成 md5 哈希值的加密库:
与脚本标签一起使用:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/md5.js"></script>
<script>
var hash = CryptoJS.MD5("Message");
alert(hash);
</script>
也可以使用 ES6:
npm install crypto-js
import MD5 from "crypto-js/md5";
console.log(MD5("Message").toString());
您也可以使用模块化导入:
var MD5 = require("crypto-js/md5");
console.log(MD5("Message").toString());
Github:https://github.com/brix/crypto-js CDN:https://cdnjs.com/libraries/crypto-js
【讨论】:
@RamenChef 我不隶属于图书馆,我发现它很有用,显然其他人也这样做,从赞成的数量来看。 不再可用。 @VansuitaJr。你提出这个主张的依据是什么?虽然他放在 src 属性中的 URL 已损坏并且需要更新,但该项目的链接仍然有效。他们链接到一个带有 md5.js 文件的crypto-js zip ***.com/questions/37187554/…【参考方案3】:您可以使用crypto-js。
我还建议使用 SHA256,而不是 MD5。
通过 NPM 安装 crypto-js:
npm install crypto-js
您也可以使用 CDN 并引用 JS file。
然后要显示 MD5 和 SHA256 哈希,您可以执行以下操作:
<script type="text/javascript">
var md5Hash = CryptoJS.MD5("Test");
var sha256Hash = CryptoJS.SHA256("Test1");
console.log(md5Hash.toString());
console.log(sha256Hash.toString());
</script>
工作示例位于此处,JSFiddle
还有其他 JS 函数会生成 MD5
哈希,如下所述。
http://www.myersdaily.org/joseph/javascript/md5-text.html
http://pajhome.org.uk/crypt/md5/md5.html
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)
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;
/* there needs to be support for Unicode here,
* unless we pretend that we can redefine the MD-5
* algorithm for multi-byte characters (perhaps
* by adding every four 16-bit characters and
* shortening the sum to 32 bits). Otherwise
* I suggest performing MD-5 as if every character
* was two bytes--e.g., 0040 0025 = @%--but then
* how will an ordinary MD-5 sum be matched?
* There is no way to standardize text to something
* like UTF-8 before transformation; speed cost is
* utterly prohibitive. The JavaScript standard
* itself needs to look at this: it should start
* providing access to strings as preformed UTF-8
* 8-bit unsigned value arrays.
*/
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('');
function md5(s)
return hex(md51(s));
/* 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;
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);
然后简单的使用MD5函数,如下图:
alert(md5("Test string"));
另一个工作JS Fiddle here
【讨论】:
但是这些函数使用字符串,我有一个大文件(视频)的 ArrayBuffer。也感谢推荐,但我需要 md5 而不是密码,只需要文件哈希总和。 有时候 md5 也可以。例如,如果你想限制评论垃圾邮件,让 JS 用 CSRF 令牌的 md5sum 替换 CSRF 令牌。没有 JS 引擎的机器人将无法使用。 如果您也解决标题中提出的问题,并添加如何计算不一定在文件系统上的文件的 md5 哈希值,这可能会很有用,例如一个您拥有其 HTTP URL 的文件 并非一切都必须是“安全的”,伙计们。所有最初的问题都试图完成的是从文件中创建一个唯一的散列 - 为什么假设安全性甚至是一个考虑因素?如果目标是简单地优化一个文件与另一个文件的识别怎么办......将一些散列负载放在客户端上。对我来说,这似乎是对 MD5 的完全有效使用。 @jordancpaul - 我为 MD5 提供了一个答案,并且还提供了一个使用 SHA256 的安全替代方案。虽然我用 MD5 示例回答了 OP 的原始问题,但推荐更安全的解决方案并提供示例是一种很好的做法。希望它可以帮助这个问题的未来读者。无论 OP 是否考虑了安全性,他们都可以根据答案中的示例自行决定哪种哈希算法是解决其问题的最佳解决方案。【参考方案4】:作为当代替代方案,现在有一个客户端加密标准。这样做的好处是可以被浏览器自己优化。
取自文档中的示例:
async function sha256(message)
// encode as UTF-8
const msgBuffer = new TextEncoder('utf-8').encode(message);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
sha256('abc').then(hash => console.log(hash));
(async function()
const hash = await sha256('abc');
());
MD5 可能不受支持,但 SHA-256、SHA-384 和 SHA-512 等。
而且这些可能也可以在服务器端进行计算。
这里有一些关于使用的文档:https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
和跨浏览器兼容性:https://caniuse.com/#feat=cryptography
【讨论】:
这是现在的正确答案(md5 不再安全)。 我非常想使用这个 API,但我使用虚拟主机(如 my.project.test)而不是普通的“localhost”。它不被视为安全上下文,因此不起作用。除了在开发系统上使用 https 之外,还有其他解决方案吗? 可以创建自签名证书以在本地使用,因此有my.project.test,我建议研究这个方向。 要使用 SubtleCrypto api,您需要处于安全上下文中,当您不需要 https 时,以 .localhost 结尾的域是安全的。 MDN Secure_Contexts【参考方案5】:这是this nice implementation的简化和缩小版本(约3.5k):
function md5(d)return rstr2hex(binl2rstr(binl_md5(rstr2binl(d),8*d.length)))function rstr2hex(d)for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return ffunction rstr2binl(d)for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _function binl2rstr(d)for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _function binl_md5(d,_)d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16)var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+4],7,-176418897),f,r,d[n+5],12,1200080426),m,f,d[n+6],17,-1473231341),i,m,d[n+7],22,-45705983),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+8],7,1770035416),f,r,d[n+9],12,-1958414417),m,f,d[n+10],17,-42063),i,m,d[n+11],22,-1990404162),r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+12],7,1804603682),f,r,d[n+13],12,-40341101),m,f,d[n+14],17,-1502002290),i,m,d[n+15],22,1236535329),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+1],5,-165796510),f,r,d[n+6],9,-1069501632),m,f,d[n+11],14,643717713),i,m,d[n+0],20,-373897302),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+5],5,-701558691),f,r,d[n+10],9,38016083),m,f,d[n+15],14,-660478335),i,m,d[n+4],20,-405537848),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+9],5,568446438),f,r,d[n+14],9,-1019803690),m,f,d[n+3],14,-187363961),i,m,d[n+8],20,1163531501),r=md5_gg(r,i=md5_gg(i,m=md5_gg(m,f,r,i,d[n+13],5,-1444681467),f,r,d[n+2],9,-51403784),m,f,d[n+7],14,1735328473),i,m,d[n+12],20,-1926607734),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+5],4,-378558),f,r,d[n+8],11,-2022574463),m,f,d[n+11],16,1839030562),i,m,d[n+14],23,-35309556),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+1],4,-1530992060),f,r,d[n+4],11,1272893353),m,f,d[n+7],16,-155497632),i,m,d[n+10],23,-1094730640),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+13],4,681279174),f,r,d[n+0],11,-358537222),m,f,d[n+3],16,-722521979),i,m,d[n+6],23,76029189),r=md5_hh(r,i=md5_hh(i,m=md5_hh(m,f,r,i,d[n+9],4,-640364487),f,r,d[n+12],11,-421815835),m,f,d[n+15],16,530742520),i,m,d[n+2],23,-995338651),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+0],6,-198630844),f,r,d[n+7],10,1126891415),m,f,d[n+14],15,-1416354905),i,m,d[n+5],21,-57434055),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+12],6,1700485571),f,r,d[n+3],10,-1894986606),m,f,d[n+10],15,-1051523),i,m,d[n+1],21,-2054922799),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+8],6,1873313359),f,r,d[n+15],10,-30611744),m,f,d[n+6],15,-1560198380),i,m,d[n+13],21,1309151649),r=md5_ii(r,i=md5_ii(i,m=md5_ii(m,f,r,i,d[n+4],6,-145523070),f,r,d[n+11],10,-1120210379),m,f,d[n+2],15,718787259),i,m,d[n+9],21,-343485551),m=safe_add(m,h),f=safe_add(f,t),r=safe_add(r,g),i=safe_add(i,e)return Array(m,f,r,i)function md5_cmn(d,_,m,f,r,i)return safe_add(bit_rol(safe_add(safe_add(_,d),safe_add(f,i)),r),m)function md5_ff(d,_,m,f,r,i,n)return md5_cmn(_&m|~_&f,d,_,r,i,n)function md5_gg(d,_,m,f,r,i,n)return md5_cmn(_&f|m&~f,d,_,r,i,n)function md5_hh(d,_,m,f,r,i,n)return md5_cmn(_^m^f,d,_,r,i,n)function md5_ii(d,_,m,f,r,i,n)return md5_cmn(m^(_|~f),d,_,r,i,n)function safe_add(d,_)var m=(65535&d)+(65535&_);return(d>>16)+(_>>16)+(m>>16)<<16|65535&mfunction bit_rol(d,_)return d<<_|d>>>32-_
该函数将返回一个大写的 32 个字符的字符串,例如 098F6BCD4621D373CADE4E832627B4F6
。
对于简单的字母数字字符串,可以这样运行:
md5('test')
如果您需要完整的 unicode 支持,您可以在传递字符串之前调用 unescape 和 encodeURIComponent:
md5(unescape(encodeURIComponent('?')))
【讨论】:
对于非英文文本,此实现会生成错误的哈希 @OlegGrishko "剥离 utf-8 转换"【参考方案6】:const crypto = require('crypto')
var filename = './file.ext'
md5Sum = crypto.createHash('md5')
s = fs.createReadStream(filename)
s.on('data', function(chunk)
md5Sum.update(chunk)
)
s.on('end', function()
console.log(md5Sum.digest('hex') + ' ' + filename)
)
【讨论】:
虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。【参考方案7】:为 @valdeci 和 @Derzu 版本添加了 Unicode 支持。 它也可用于对 JSON 对象进行 md5 化处理。 添加了与 IE11 及以下版本的不兼容 :)
var md5 = (function()
var MD5 = function (d)
return M(V(Y(X(d), 8 * d.length)))
function M (d)
for (var _, m = '0123456789abcdef', f = '', r = 0; r < d.length; r++)
_ = d.charCodeAt(r)
f += m.charAt(_ >>> 4 & 15) + m.charAt(15 & _)
return f
function X (d)
for (var _ = Array(d.length >> 2), m = 0; m < _.length; m++)
_[m] = 0
for (m = 0; m < 8 * d.length; m += 8)
_[m >> 5] |= (255 & d.charCodeAt(m / 8)) << m % 32
return _
function V (d)
for (var _ = '', m = 0; m < 32 * d.length; m += 8) _ += String.fromCharCode(d[m >> 5] >>> m % 32 & 255)
return _
function Y (d, _)
d[_ >> 5] |= 128 << _ % 32
d[14 + (_ + 64 >>> 9 << 4)] = _
for (var m = 1732584193, f = -271733879, r = -1732584194, i = 271733878, n = 0; n < d.length; n += 16)
var h = m
var t = f
var g = r
var e = i
f = md5ii(f = md5ii(f = md5ii(f = md5ii(f = md5hh(f = md5hh(f = md5hh(f = md5hh(f = md5gg(f = md5gg(f = md5gg(f = md5gg(f = md5ff(f = md5ff(f = md5ff(f = md5ff(f, r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 0], 7, -680876936), f, r, d[n + 1], 12, -389564586), m, f, d[n + 2], 17, 606105819), i, m, d[n + 3], 22, -1044525330), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 4], 7, -176418897), f, r, d[n + 5], 12, 1200080426), m, f, d[n + 6], 17, -1473231341), i, m, d[n + 7], 22, -45705983), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 8], 7, 1770035416), f, r, d[n + 9], 12, -1958414417), m, f, d[n + 10], 17, -42063), i, m, d[n + 11], 22, -1990404162), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 12], 7, 1804603682), f, r, d[n + 13], 12, -40341101), m, f, d[n + 14], 17, -1502002290), i, m, d[n + 15], 22, 1236535329), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 1], 5, -165796510), f, r, d[n + 6], 9, -1069501632), m, f, d[n + 11], 14, 643717713), i, m, d[n + 0], 20, -373897302), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 5], 5, -701558691), f, r, d[n + 10], 9, 38016083), m, f, d[n + 15], 14, -660478335), i, m, d[n + 4], 20, -405537848), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 9], 5, 568446438), f, r, d[n + 14], 9, -1019803690), m, f, d[n + 3], 14, -187363961), i, m, d[n + 8], 20, 1163531501), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 13], 5, -1444681467), f, r, d[n + 2], 9, -51403784), m, f, d[n + 7], 14, 1735328473), i, m, d[n + 12], 20, -1926607734), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 5], 4, -378558), f, r, d[n + 8], 11, -2022574463), m, f, d[n + 11], 16, 1839030562), i, m, d[n + 14], 23, -35309556), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 1], 4, -1530992060), f, r, d[n + 4], 11, 1272893353), m, f, d[n + 7], 16, -155497632), i, m, d[n + 10], 23, -1094730640), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 13], 4, 681279174), f, r, d[n + 0], 11, -358537222), m, f, d[n + 3], 16, -722521979), i, m, d[n + 6], 23, 76029189), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 9], 4, -640364487), f, r, d[n + 12], 11, -421815835), m, f, d[n + 15], 16, 530742520), i, m, d[n + 2], 23, -995338651), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 0], 6, -198630844), f, r, d[n + 7], 10, 1126891415), m, f, d[n + 14], 15, -1416354905), i, m, d[n + 5], 21, -57434055), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 12], 6, 1700485571), f, r, d[n + 3], 10, -1894986606), m, f, d[n + 10], 15, -1051523), i, m, d[n + 1], 21, -2054922799), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 8], 6, 1873313359), f, r, d[n + 15], 10, -30611744), m, f, d[n + 6], 15, -1560198380), i, m, d[n + 13], 21, 1309151649), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 4], 6, -145523070), f, r, d[n + 11], 10, -1120210379), m, f, d[n + 2], 15, 718787259), i, m, d[n + 9], 21, -343485551)
m = safeadd(m, h)
f = safeadd(f, t)
r = safeadd(r, g)
i = safeadd(i, e)
return [m, f, r, i]
function md5cmn (d, _, m, f, r, i)
return safeadd(bitrol(safeadd(safeadd(_, d), safeadd(f, i)), r), m)
function md5ff (d, _, m, f, r, i, n)
return md5cmn(_ & m | ~_ & f, d, _, r, i, n)
function md5gg (d, _, m, f, r, i, n)
return md5cmn(_ & f | m & ~f, d, _, r, i, n)
function md5hh (d, _, m, f, r, i, n)
return md5cmn(_ ^ m ^ f, d, _, r, i, n)
function md5ii (d, _, m, f, r, i, n)
return md5cmn(m ^ (_ | ~f), d, _, r, i, n)
function safeadd (d, _)
var m = (65535 & d) + (65535 & _)
return (d >> 16) + (_ >> 16) + (m >> 16) << 16 | 65535 & m
function bitrol (d, _)
return d << _ | d >>> 32 - _
function MD5Unicode(buffer)
if (!(buffer instanceof Uint8Array))
buffer = new TextEncoder().encode(typeof buffer==='string' ? buffer : JSON.stringify(buffer));
var binary = [];
var bytes = new Uint8Array(buffer);
for (var i = 0, il = bytes.byteLength; i < il; i++)
binary.push(String.fromCharCode(bytes[i]));
return MD5(binary.join(''));
return MD5Unicode;
)();
md5('òàùèéì€'); // aeb60a568b5bf37d397dde71dacf5519
md5( prop1: 'òàùèéì€', prop2: 123 ); // 4334a47d3304a628e5b376bce203adbe
【讨论】:
我可以确认这可以使用现代的 create-react-app 并希望与 Gravatar 集成。谢谢你,现在我不需要图书馆了!【参考方案8】:这是 @valdeci 答案的 JSLint 兼容版本。
var MD5 = function (d)
return M(V(Y(X(d), 8 * d.length)))
function M (d)
for (var _, m = '0123456789ABCDEF', f = '', r = 0; r < d.length; r++)
_ = d.charCodeAt(r)
f += m.charAt(_ >>> 4 & 15) + m.charAt(15 & _)
return f
function X (d)
for (var _ = Array(d.length >> 2), m = 0; m < _.length; m++)
_[m] = 0
for (m = 0; m < 8 * d.length; m += 8)
_[m >> 5] |= (255 & d.charCodeAt(m / 8)) << m % 32
return _
function V (d)
for (var _ = '', m = 0; m < 32 * d.length; m += 8) _ += String.fromCharCode(d[m >> 5] >>> m % 32 & 255)
return _
function Y (d, _)
d[_ >> 5] |= 128 << _ % 32
d[14 + (_ + 64 >>> 9 << 4)] = _
for (var m = 1732584193, f = -271733879, r = -1732584194, i = 271733878, n = 0; n < d.length; n += 16)
var h = m
var t = f
var g = r
var e = i
f = md5ii(f = md5ii(f = md5ii(f = md5ii(f = md5hh(f = md5hh(f = md5hh(f = md5hh(f = md5gg(f = md5gg(f = md5gg(f = md5gg(f = md5ff(f = md5ff(f = md5ff(f = md5ff(f, r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 0], 7, -680876936), f, r, d[n + 1], 12, -389564586), m, f, d[n + 2], 17, 606105819), i, m, d[n + 3], 22, -1044525330), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 4], 7, -176418897), f, r, d[n + 5], 12, 1200080426), m, f, d[n + 6], 17, -1473231341), i, m, d[n + 7], 22, -45705983), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 8], 7, 1770035416), f, r, d[n + 9], 12, -1958414417), m, f, d[n + 10], 17, -42063), i, m, d[n + 11], 22, -1990404162), r = md5ff(r, i = md5ff(i, m = md5ff(m, f, r, i, d[n + 12], 7, 1804603682), f, r, d[n + 13], 12, -40341101), m, f, d[n + 14], 17, -1502002290), i, m, d[n + 15], 22, 1236535329), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 1], 5, -165796510), f, r, d[n + 6], 9, -1069501632), m, f, d[n + 11], 14, 643717713), i, m, d[n + 0], 20, -373897302), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 5], 5, -701558691), f, r, d[n + 10], 9, 38016083), m, f, d[n + 15], 14, -660478335), i, m, d[n + 4], 20, -405537848), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 9], 5, 568446438), f, r, d[n + 14], 9, -1019803690), m, f, d[n + 3], 14, -187363961), i, m, d[n + 8], 20, 1163531501), r = md5gg(r, i = md5gg(i, m = md5gg(m, f, r, i, d[n + 13], 5, -1444681467), f, r, d[n + 2], 9, -51403784), m, f, d[n + 7], 14, 1735328473), i, m, d[n + 12], 20, -1926607734), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 5], 4, -378558), f, r, d[n + 8], 11, -2022574463), m, f, d[n + 11], 16, 1839030562), i, m, d[n + 14], 23, -35309556), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 1], 4, -1530992060), f, r, d[n + 4], 11, 1272893353), m, f, d[n + 7], 16, -155497632), i, m, d[n + 10], 23, -1094730640), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 13], 4, 681279174), f, r, d[n + 0], 11, -358537222), m, f, d[n + 3], 16, -722521979), i, m, d[n + 6], 23, 76029189), r = md5hh(r, i = md5hh(i, m = md5hh(m, f, r, i, d[n + 9], 4, -640364487), f, r, d[n + 12], 11, -421815835), m, f, d[n + 15], 16, 530742520), i, m, d[n + 2], 23, -995338651), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 0], 6, -198630844), f, r, d[n + 7], 10, 1126891415), m, f, d[n + 14], 15, -1416354905), i, m, d[n + 5], 21, -57434055), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 12], 6, 1700485571), f, r, d[n + 3], 10, -1894986606), m, f, d[n + 10], 15, -1051523), i, m, d[n + 1], 21, -2054922799), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 8], 6, 1873313359), f, r, d[n + 15], 10, -30611744), m, f, d[n + 6], 15, -1560198380), i, m, d[n + 13], 21, 1309151649), r = md5ii(r, i = md5ii(i, m = md5ii(m, f, r, i, d[n + 4], 6, -145523070), f, r, d[n + 11], 10, -1120210379), m, f, d[n + 2], 15, 718787259), i, m, d[n + 9], 21, -343485551)
m = safeadd(m, h)
f = safeadd(f, t)
r = safeadd(r, g)
i = safeadd(i, e)
return [m, f, r, i]
function md5cmn (d, _, m, f, r, i)
return safeadd(bitrol(safeadd(safeadd(_, d), safeadd(f, i)), r), m)
function md5ff (d, _, m, f, r, i, n)
return md5cmn(_ & m | ~_ & f, d, _, r, i, n)
function md5gg (d, _, m, f, r, i, n)
return md5cmn(_ & f | m & ~f, d, _, r, i, n)
function md5hh (d, _, m, f, r, i, n)
return md5cmn(_ ^ m ^ f, d, _, r, i, n)
function md5ii (d, _, m, f, r, i, n)
return md5cmn(m ^ (_ | ~f), d, _, r, i, n)
function safeadd (d, _)
var m = (65535 & d) + (65535 & _)
return (d >> 16) + (_ >> 16) + (m >> 16) << 16 | 65535 & m
function bitrol (d, _)
return d << _ | d >>> 32 - _
var value = 'test';
var result = MD5(value);
document.body.innerHTML = 'MD5: ' + result;
【讨论】:
【参考方案9】:可以在这个要点上找到它的功能 https://github.com/blueimp/JavaScript-MD5/blob/master/js/md5.js
/*
* JavaScript MD5
* https://github.com/blueimp/JavaScript-MD5
*
* Copyright 2011, Sebastian Tschan
* https://blueimp.net
*
* Licensed under the MIT license:
* https://opensource.org/licenses/MIT
*
* Based on
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/* global define */
/* eslint-disable strict */
;(function ($)
'use strict'
/**
* Add integers, wrapping at 2^32.
* This uses 16-bit operations internally to work around bugs in interpreters.
*
* @param number x First integer
* @param number y Second integer
* @returns number Sum
*/
function safeAdd(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.
*
* @param number num 32-bit number
* @param number cnt Rotation count
* @returns number Rotated number
*/
function bitRotateLeft(num, cnt)
return (num << cnt) | (num >>> (32 - cnt))
/**
* Basic operation the algorithm uses.
*
* @param number q q
* @param number a a
* @param number b b
* @param number x x
* @param number s s
* @param number t t
* @returns number Result
*/
function md5cmn(q, a, b, x, s, t)
return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
/**
* Basic operation the algorithm uses.
*
* @param number a a
* @param number b b
* @param number c c
* @param number d d
* @param number x x
* @param number s s
* @param number t t
* @returns number Result
*/
function md5ff(a, b, c, d, x, s, t)
return md5cmn((b & c) | (~b & d), a, b, x, s, t)
/**
* Basic operation the algorithm uses.
*
* @param number a a
* @param number b b
* @param number c c
* @param number d d
* @param number x x
* @param number s s
* @param number t t
* @returns number Result
*/
function md5gg(a, b, c, d, x, s, t)
return md5cmn((b & d) | (c & ~d), a, b, x, s, t)
/**
* Basic operation the algorithm uses.
*
* @param number a a
* @param number b b
* @param number c c
* @param number d d
* @param number x x
* @param number s s
* @param number t t
* @returns number Result
*/
function md5hh(a, b, c, d, x, s, t)
return md5cmn(b ^ c ^ d, a, b, x, s, t)
/**
* Basic operation the algorithm uses.
*
* @param number a a
* @param number b b
* @param number c c
* @param number d d
* @param number x x
* @param number s s
* @param number t t
* @returns number Result
*/
function md5ii(a, b, c, d, x, s, t)
return md5cmn(c ^ (b | ~d), a, b, x, s, t)
/**
* Calculate the MD5 of an array of little-endian words, and a bit length.
*
* @param Array x Array of little-endian words
* @param number len Bit length
* @returns Array<number> MD5 Array
*/
function binlMD5(x, len)
/* append padding */
x[len >> 5] |= 0x80 << len % 32
x[(((len + 64) >>> 9) << 4) + 14] = len
var i
var olda
var oldb
var oldc
var oldd
var a = 1732584193
var b = -271733879
var c = -1732584194
var d = 271733878
for (i = 0; i < x.length; i += 16)
olda = a
oldb = b
oldc = c
oldd = d
a = md5ff(a, b, c, d, x[i], 7, -680876936)
d = md5ff(d, a, b, c, x[i + 1], 12, -389564586)
c = md5ff(c, d, a, b, x[i + 2], 17, 606105819)
b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330)
a = md5ff(a, b, c, d, x[i + 4], 7, -176418897)
d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426)
c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341)
b = md5ff(b, c, d, a, x[i + 7], 22, -45705983)
a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416)
d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417)
c = md5ff(c, d, a, b, x[i + 10], 17, -42063)
b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162)
a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682)
d = md5ff(d, a, b, c, x[i + 13], 12, -40341101)
c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290)
b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329)
a = md5gg(a, b, c, d, x[i + 1], 5, -165796510)
d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632)
c = md5gg(c, d, a, b, x[i + 11], 14, 643717713)
b = md5gg(b, c, d, a, x[i], 20, -373897302)
a = md5gg(a, b, c, d, x[i + 5], 5, -701558691)
d = md5gg(d, a, b, c, x[i + 10], 9, 38016083)
c = md5gg(c, d, a, b, x[i + 15], 14, -660478335)
b = md5gg(b, c, d, a, x[i + 4], 20, -405537848)
a = md5gg(a, b, c, d, x[i + 9], 5, 568446438)
d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690)
c = md5gg(c, d, a, b, x[i + 3], 14, -187363961)
b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501)
a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467)
d = md5gg(d, a, b, c, x[i + 2], 9, -51403784)
c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473)
b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734)
a = md5hh(a, b, c, d, x[i + 5], 4, -378558)
d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463)
c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562)
b = md5hh(b, c, d, a, x[i + 14], 23, -35309556)
a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060)
d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353)
c = md5hh(c, d, a, b, x[i + 7], 16, -155497632)
b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640)
a = md5hh(a, b, c, d, x[i + 13], 4, 681279174)
d = md5hh(d, a, b, c, x[i], 11, -358537222)
c = md5hh(c, d, a, b, x[i + 3], 16, -722521979)
b = md5hh(b, c, d, a, x[i + 6], 23, 76029189)
a = md5hh(a, b, c, d, x[i + 9], 4, -640364487)
d = md5hh(d, a, b, c, x[i + 12], 11, -421815835)
c = md5hh(c, d, a, b, x[i + 15], 16, 530742520)
b = md5hh(b, c, d, a, x[i + 2], 23, -995338651)
a = md5ii(a, b, c, d, x[i], 6, -198630844)
d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415)
c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905)
b = md5ii(b, c, d, a, x[i + 5], 21, -57434055)
a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571)
d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606)
c = md5ii(c, d, a, b, x[i + 10], 15, -1051523)
b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799)
a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359)
d = md5ii(d, a, b, c, x[i + 15], 10, -30611744)
c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380)
b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649)
a = md5ii(a, b, c, d, x[i + 4], 6, -145523070)
d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379)
c = md5ii(c, d, a, b, x[i + 2], 15, 718787259)
b = md5ii(b, c, d, a, x[i + 9], 21, -343485551)
a = safeAdd(a, olda)
b = safeAdd(b, oldb)
c = safeAdd(c, oldc)
d = safeAdd(d, oldd)
return [a, b, c, d]
/**
* Convert an array of little-endian words to a string
*
* @param Array<number> input MD5 Array
* @returns string MD5 string
*/
function binl2rstr(input)
var i
var output = ''
var length32 = input.length * 32
for (i = 0; i < length32; i += 8)
output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff)
return output
/**
* Convert a raw string to an array of little-endian words
* Characters >255 have their high-byte silently ignored.
*
* @param string input Raw input string
* @returns Array<number> Array of little-endian words
*/
function rstr2binl(input)
var i
var output = []
output[(input.length >> 2) - 1] = undefined
for (i = 0; i < output.length; i += 1)
output[i] = 0
var length8 = input.length * 8
for (i = 0; i < length8; i += 8)
output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32
return output
/**
* Calculate the MD5 of a raw string
*
* @param string s Input string
* @returns string Raw MD5 string
*/
function rstrMD5(s)
return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
/**
* Calculates the HMAC-MD5 of a key and some data (raw strings)
*
* @param string key HMAC key
* @param string data Raw input string
* @returns string Raw MD5 string
*/
function rstrHMACMD5(key, data)
var i
var bkey = rstr2binl(key)
var ipad = []
var opad = []
var hash
ipad[15] = opad[15] = undefined
if (bkey.length > 16)
bkey = binlMD5(bkey, key.length * 8)
for (i = 0; i < 16; i += 1)
ipad[i] = bkey[i] ^ 0x36363636
opad[i] = bkey[i] ^ 0x5c5c5c5c
hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)
return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
/**
* Convert a raw string to a hex string
*
* @param string input Raw input string
* @returns string Hex encoded string
*/
function rstr2hex(input)
var hexTab = '0123456789abcdef'
var output = ''
var x
var i
for (i = 0; i < input.length; i += 1)
x = input.charCodeAt(i)
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
return output
/**
* Encode a string as UTF-8
*
* @param string input Input string
* @returns string UTF8 string
*/
function str2rstrUTF8(input)
return unescape(encodeURIComponent(input))
/**
* Encodes input string as raw MD5 string
*
* @param string s Input string
* @returns string Raw MD5 string
*/
function rawMD5(s)
return rstrMD5(str2rstrUTF8(s))
/**
* Encodes input string as Hex encoded string
*
* @param string s Input string
* @returns string Hex encoded string
*/
function hexMD5(s)
return rstr2hex(rawMD5(s))
/**
* Calculates the raw HMAC-MD5 for the given key and data
*
* @param string k HMAC key
* @param string d Input string
* @returns string Raw MD5 string
*/
function rawHMACMD5(k, d)
return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
/**
* Calculates the Hex encoded HMAC-MD5 for the given key and data
*
* @param string k HMAC key
* @param string d Input string
* @returns string Raw MD5 string
*/
function hexHMACMD5(k, d)
return rstr2hex(rawHMACMD5(k, d))
/**
* Calculates MD5 value for a given string.
* If a key is provided, calculates the HMAC-MD5 value.
* Returns a Hex encoded string unless the raw argument is given.
*
* @param string string Input string
* @param string [key] HMAC key
* @param boolean [raw] Raw output switch
* @returns string MD5 output
*/
function md5(string, key, raw)
if (!key)
if (!raw)
return hexMD5(string)
return rawMD5(string)
if (!raw)
return hexHMACMD5(key, string)
return rawHMACMD5(key, string)
if (typeof define === 'function' && define.amd)
define(function ()
return md5
)
else if (typeof module === 'object' && module.exports)
module.exports = md5
else
$.md5 = md5
)(this)
【讨论】:
【参考方案10】:您可以使用轻量级库pure-md5。 只有 4.7kb。
$("#file-dialog").change(function()
handleFiles(this.files);
);
function handleFiles(files)
for (var index = 0; index < files.length; index++)
var file = files[index];
var fileReader = new FileReader();
fileReader.onload = function(e)
$('body').append(`
<div style="margin-top: 2rem;">
<span>$file.name: </span>
<span>$(md5(e.target.result))</span>
</div>
`);
fileReader.readAsText(file, 'utf-8');
body
font-family: sans-serif;
<script src="https://unpkg.com/pure-md5@0.1.9/lib/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="file-dialog" multiple="true" accept="image/*">
【讨论】:
【参考方案11】:你可以使用crypto-js。
要使用crypto-js,你需要先加载core.js,然后再加载md5.js。
网址列表在这里https://cdnjs.com/libraries/crypto-js
cryptojs 也可以在此处https://code.google.com/archive/p/crypto-js/downloads 以 zip 形式获得
2013 年回答者“amal”有一个答案,与此类似,但 a)他与 md5.js 的链接不再有效 b)他没有事先加载 core.js,这是必要的。
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/md5.js"></script>
<script>
var hash = CryptoJS.MD5("Message");
console.log(hash);
</script>
</head>
<body>
</body>
</html>
【讨论】:
我真的应该包含获取文件的代码..我有它我打算添加它的地方。另外,请记住,即使那样,它也只能获取从 http 请求返回的文件,该文件通常是缓存文件。 (尽管在某些网络浏览器中,您可以告诉他们禁用缓存。在 chrome 中,只需在检查器中简单地勾选一个框,当然大多数用户不会勾选)以上是关于如何在 JavaScript/Node.js 中生成 MD5 文件哈希?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Javascript/Node.js 中访问函数的结果 [重复]
Solana - 如何从 JavaScript / Node.js 中的本地密钥对获取帐户?
如何在 JavaScript node.js 中将 xml 文件解析为数组
如何使用 Node.js/JavaScript 合并两个按键值匹配的数组值