jQuery md5加密插件jQuery.md5.js用法示例
程序员文章站
2022-06-19 23:25:27
本文实例讲述了jquery md5加密插件jquery.md5.js用法。分享给大家供大家参考,具体如下:
使用方法:
$.(md5("你想要加密的字符串"))...
本文实例讲述了jquery md5加密插件jquery.md5.js用法。分享给大家供大家参考,具体如下:
使用方法:
$.(md5("你想要加密的字符串"));
jquery.md5.js插件代码:
/** * jquery md5 hash algorithm function * * <code> * calculate the md5 hash of a string * string $.md5 ( string str ) * </code> * * calculates the md5 hash of str using the » rsa data security, inc. md5 message-digest algorithm, and returns that hash. * md5 (message-digest algorithm 5) is a widely-used cryptographic hash function with a 128-bit hash value. md5 has been employed in a wide variety of security applications, and is also commonly used to check the integrity of data. the generated hash is also non-reversable. data cannot be retrieved from the message digest, the digest uniquely identifies the data. * md5 was developed by professor ronald l. rivest in 1994. its 128 bit (16 byte) message digest makes it a faster implementation than sha-1. * this script is used to process a variable length message into a fixed-length output of 128 bits using the md5 algorithm. it is fully compatible with utf-8 encoding. it is very useful when u want to transfer encrypted passwords over the internet. if you plan using utf-8 encoding in your project don't forget to set the page encoding to utf-8 (content-type meta tag). * this function orginally get from the webtoolkit and rewrite for using as the jquery plugin. * * example * code * <code> * $.md5("i'm persian."); * </code> * result * <code> * "b8c901d0f02223f9761016cfff9d68df" * </code> * * @alias muhammad hussein fattahizadeh < muhammad [at] semnanweb [dot] com > * @link http://www.semnanweb.com/jquery-plugin/md5.html * @see http://www.webtoolkit.info/ * @license http://www.gnu.org/licenses/gpl.html [gnu general public license] * @param {jquery} {md5:function(string)) * @return string */ (function($){ var rotateleft = function(lvalue, ishiftbits) { return (lvalue << ishiftbits) | (lvalue >>> (32 - ishiftbits)); } var addunsigned = function(lx, ly) { var lx4, ly4, lx8, ly8, lresult; lx8 = (lx & 0x80000000); ly8 = (ly & 0x80000000); lx4 = (lx & 0x40000000); ly4 = (ly & 0x40000000); lresult = (lx & 0x3fffffff) + (ly & 0x3fffffff); if (lx4 & ly4) return (lresult ^ 0x80000000 ^ lx8 ^ ly8); if (lx4 | ly4) { if (lresult & 0x40000000) return (lresult ^ 0xc0000000 ^ lx8 ^ ly8); else return (lresult ^ 0x40000000 ^ lx8 ^ ly8); } else { return (lresult ^ lx8 ^ ly8); } } var f = function(x, y, z) { return (x & y) | ((~ x) & z); } var g = function(x, y, z) { return (x & z) | (y & (~ z)); } var h = function(x, y, z) { return (x ^ y ^ z); } var i = function(x, y, z) { return (y ^ (x | (~ z))); } var ff = function(a, b, c, d, x, s, ac) { a = addunsigned(a, addunsigned(addunsigned(f(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; var gg = function(a, b, c, d, x, s, ac) { a = addunsigned(a, addunsigned(addunsigned(g(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; var hh = function(a, b, c, d, x, s, ac) { a = addunsigned(a, addunsigned(addunsigned(h(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; var ii = function(a, b, c, d, x, s, ac) { a = addunsigned(a, addunsigned(addunsigned(i(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; var converttowordarray = function(string) { var lwordcount; var lmessagelength = string.length; var lnumberofwordstempone = lmessagelength + 8; var lnumberofwordstemptwo = (lnumberofwordstempone - (lnumberofwordstempone % 64)) / 64; var lnumberofwords = (lnumberofwordstemptwo + 1) * 16; var lwordarray = array(lnumberofwords - 1); var lbyteposition = 0; var lbytecount = 0; while (lbytecount < lmessagelength) { lwordcount = (lbytecount - (lbytecount % 4)) / 4; lbyteposition = (lbytecount % 4) * 8; lwordarray[lwordcount] = (lwordarray[lwordcount] | (string.charcodeat(lbytecount) << lbyteposition)); lbytecount++; } lwordcount = (lbytecount - (lbytecount % 4)) / 4; lbyteposition = (lbytecount % 4) * 8; lwordarray[lwordcount] = lwordarray[lwordcount] | (0x80 << lbyteposition); lwordarray[lnumberofwords - 2] = lmessagelength << 3; lwordarray[lnumberofwords - 1] = lmessagelength >>> 29; return lwordarray; }; var wordtohex = function(lvalue) { var wordtohexvalue = "", wordtohexvaluetemp = "", lbyte, lcount; for (lcount = 0; lcount <= 3; lcount++) { lbyte = (lvalue >>> (lcount * 8)) & 255; wordtohexvaluetemp = "0" + lbyte.tostring(16); wordtohexvalue = wordtohexvalue + wordtohexvaluetemp.substr(wordtohexvaluetemp.length - 2, 2); } return wordtohexvalue; }; var utf8encode = function(string) { string = string.replace(/\x0d\x0a/g, "\x0a"); var output = ""; for (var n = 0; n < string.length; n++) { var c = string.charcodeat(n); if (c < 128) { output += string.fromcharcode(c); } else if ((c > 127) && (c < 2048)) { output += string.fromcharcode((c >> 6) | 192); output += string.fromcharcode((c & 63) | 128); } else { output += string.fromcharcode((c >> 12) | 224); output += string.fromcharcode(((c >> 6) & 63) | 128); output += string.fromcharcode((c & 63) | 128); } } return output; }; $.extend({ md5: function(string) { var x = array(); var k, aa, bb, cc, dd, a, b, c, d; var s11=7, s12=12, s13=17, s14=22; var s21=5, s22=9 , s23=14, s24=20; var s31=4, s32=11, s33=16, s34=23; var s41=6, s42=10, s43=15, s44=21; string = utf8encode(string); x = converttowordarray(string); a = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; d = 0x10325476; for (k = 0; k < x.length; k += 16) { aa = a; bb = b; cc = c; dd = d; a = ff(a, b, c, d, x[k+0], s11, 0xd76aa478); d = ff(d, a, b, c, x[k+1], s12, 0xe8c7b756); c = ff(c, d, a, b, x[k+2], s13, 0x242070db); b = ff(b, c, d, a, x[k+3], s14, 0xc1bdceee); a = ff(a, b, c, d, x[k+4], s11, 0xf57c0faf); d = ff(d, a, b, c, x[k+5], s12, 0x4787c62a); c = ff(c, d, a, b, x[k+6], s13, 0xa8304613); b = ff(b, c, d, a, x[k+7], s14, 0xfd469501); a = ff(a, b, c, d, x[k+8], s11, 0x698098d8); d = ff(d, a, b, c, x[k+9], s12, 0x8b44f7af); c = ff(c, d, a, b, x[k+10], s13, 0xffff5bb1); b = ff(b, c, d, a, x[k+11], s14, 0x895cd7be); a = ff(a, b, c, d, x[k+12], s11, 0x6b901122); d = ff(d, a, b, c, x[k+13], s12, 0xfd987193); c = ff(c, d, a, b, x[k+14], s13, 0xa679438e); b = ff(b, c, d, a, x[k+15], s14, 0x49b40821); a = gg(a, b, c, d, x[k+1], s21, 0xf61e2562); d = gg(d, a, b, c, x[k+6], s22, 0xc040b340); c = gg(c, d, a, b, x[k+11], s23, 0x265e5a51); b = gg(b, c, d, a, x[k+0], s24, 0xe9b6c7aa); a = gg(a, b, c, d, x[k+5], s21, 0xd62f105d); d = gg(d, a, b, c, x[k+10], s22, 0x2441453); c = gg(c, d, a, b, x[k+15], s23, 0xd8a1e681); b = gg(b, c, d, a, x[k+4], s24, 0xe7d3fbc8); a = gg(a, b, c, d, x[k+9], s21, 0x21e1cde6); d = gg(d, a, b, c, x[k+14], s22, 0xc33707d6); c = gg(c, d, a, b, x[k+3], s23, 0xf4d50d87); b = gg(b, c, d, a, x[k+8], s24, 0x455a14ed); a = gg(a, b, c, d, x[k+13], s21, 0xa9e3e905); d = gg(d, a, b, c, x[k+2], s22, 0xfcefa3f8); c = gg(c, d, a, b, x[k+7], s23, 0x676f02d9); b = gg(b, c, d, a, x[k+12], s24, 0x8d2a4c8a); a = hh(a, b, c, d, x[k+5], s31, 0xfffa3942); d = hh(d, a, b, c, x[k+8], s32, 0x8771f681); c = hh(c, d, a, b, x[k+11], s33, 0x6d9d6122); b = hh(b, c, d, a, x[k+14], s34, 0xfde5380c); a = hh(a, b, c, d, x[k+1], s31, 0xa4beea44); d = hh(d, a, b, c, x[k+4], s32, 0x4bdecfa9); c = hh(c, d, a, b, x[k+7], s33, 0xf6bb4b60); b = hh(b, c, d, a, x[k+10], s34, 0xbebfbc70); a = hh(a, b, c, d, x[k+13], s31, 0x289b7ec6); d = hh(d, a, b, c, x[k+0], s32, 0xeaa127fa); c = hh(c, d, a, b, x[k+3], s33, 0xd4ef3085); b = hh(b, c, d, a, x[k+6], s34, 0x4881d05); a = hh(a, b, c, d, x[k+9], s31, 0xd9d4d039); d = hh(d, a, b, c, x[k+12], s32, 0xe6db99e5); c = hh(c, d, a, b, x[k+15], s33, 0x1fa27cf8); b = hh(b, c, d, a, x[k+2], s34, 0xc4ac5665); a = ii(a, b, c, d, x[k+0], s41, 0xf4292244); d = ii(d, a, b, c, x[k+7], s42, 0x432aff97); c = ii(c, d, a, b, x[k+14], s43, 0xab9423a7); b = ii(b, c, d, a, x[k+5], s44, 0xfc93a039); a = ii(a, b, c, d, x[k+12], s41, 0x655b59c3); d = ii(d, a, b, c, x[k+3], s42, 0x8f0ccc92); c = ii(c, d, a, b, x[k+10], s43, 0xffeff47d); b = ii(b, c, d, a, x[k+1], s44, 0x85845dd1); a = ii(a, b, c, d, x[k+8], s41, 0x6fa87e4f); d = ii(d, a, b, c, x[k+15], s42, 0xfe2ce6e0); c = ii(c, d, a, b, x[k+6], s43, 0xa3014314); b = ii(b, c, d, a, x[k+13], s44, 0x4e0811a1); a = ii(a, b, c, d, x[k+4], s41, 0xf7537e82); d = ii(d, a, b, c, x[k+11], s42, 0xbd3af235); c = ii(c, d, a, b, x[k+2], s43, 0x2ad7d2bb); b = ii(b, c, d, a, x[k+9], s44, 0xeb86d391); a = addunsigned(a, aa); b = addunsigned(b, bb); c = addunsigned(c, cc); d = addunsigned(d, dd); } var tempvalue = wordtohex(a) + wordtohex(b) + wordtohex(c) + wordtohex(d); return tempvalue.tolowercase(); } }); })(jquery);
或者点击此处本站下载jquer.md5.js源码。
简单测试示例:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>www.jb51.net jquery md5加密</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript" src="jquery.md5.js"></script> <script type="text/javascript"> alert($.md5("www.jb51.net")); </script> </head> <body> </body> </html>
带入jquery.md5.js到上述示例中,使用在线html/css/javascript代码运行工具:http://tools.jb51.net/code/htmljsrun,运行代码可得到如下结果:
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery常用插件及用法总结》、《jquery扩展技巧总结》、《jquery切换特效与技巧总结》、《jquery遍历算法与技巧总结》、《jquery常见经典特效汇总》、《jquery动画与特效用法总结》及《jquery选择器用法总结》
希望本文所述对大家jquery程序设计有所帮助。
下一篇: 广东步入“大数据”时代