JS 实现 md5 哈希算法
2022年8月28日大约 7 分钟
JS 实现
js原生里并没有类似其他语言的md5加密函数,需要我们针对md5加密算法自己来扩充。
当然,这种常用的加密库网上肯定是有的。今天就介绍下我用的一款md5加密库。文末有md5.js下载链接。
方法
md5.js共有md5的6种加密方法:
hex_md5(value)
b64_md5(value)
str_md5(value)
hex_hmac_md5(key, data)
b64_hmac_md5(key, data)
str_hmac_md5(key, data)
MD5加密优缺点:
1,用js对私密信息加密可避免在网络中输入明文信息,被他人截取数据包而造成数据泄露。
2,避免缓存中自动缓存密码。比如在使用火狐浏览器登陆时,输入的用户名及密码自动缓存后,下次登陆无需输入密码就可实现登录,这样就给别人留下了漏洞,当别人使用你的电脑登陆那么你的密码就泄露了。
使用js加密时,缓存的加密后的密文,用密文做密码登录是不成功的,即使泄露也是泄露的密文,对密码不会造成威胁。
缺点是: 每次登陆时都要手动输入密码,比较麻烦。
3,使用js加密,减少了服务器加密时的资源消耗,从理论上提高了服务器的性能。为了安全,很有必要在做服务端的加密,无论从理论还是实际,两道门比一道门要安全些,至少给攻击者造成了一个障碍。
使用
var code = "123456";
var username = "123456";
var password = "123456";
var str1 = hex_md5("123456"); // e10adc3949ba59abbe56e057f20f883e 我们常用的是这种
var str2 = b64_md5("123456");
var str3 = str_md5("123456");
var str4 = hex_hmac_md5(code,code); // 30ce71a73bdd908c3955a90e8f7429ef
var str5 = b64_hmac_md5(username,username); // MM5xpzvdkIw5VakOj3Qp7w
var str6 = str_hmac_md5(password,password);
源码
/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}
/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> 5] |= 0x80 >> 9) 16) bkey = core_md5(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i > 16) + (y >> 16) + (lsw >> 16);
return (msw >> (32 - cnt));
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = (1 >5] |= (str.charCodeAt(i / chrsz) & mask) >5] >>> (i % 32)) & mask);
return str;
}
/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i >2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for(var i = 0; i > 2] >> 8 * ( i %4)) & 0xFF) > 2] >> 8 * ((i+1)%4)) & 0xFF) > 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}
小结
经常整理归纳。
如何把这个 js 文件放在 CDN 上?
开源项目
https://github.com/blueimp/JavaScript-MD5
https://github.com/satazor/js-spark-md5
参考资料
贡献者
binbin.hou