您好,欢迎访问一九零五行业门户网

JavaScript实现的MD5算法完整实例_javascript技巧

本文实例讲述了javascript实现md5算法的方法。分享给大家供大家参考,具体如下:
/**** md5 (message-digest algorithm)* http://www.webtoolkit.info/***/var md5 = function (string) { function rotateleft(lvalue, ishiftbits) { return (lvalue>(32-ishiftbits)); } function addunsigned(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); } } function f(x,y,z) { return (x & y) | ((~x) & z); } function g(x,y,z) { return (x & z) | (y & (~z)); } function h(x,y,z) { return (x ^ y ^ z); } function i(x,y,z) { return (y ^ (x | (~z))); } function ff(a,b,c,d,x,s,ac) { a = addunsigned(a, addunsigned(addunsigned(f(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; function gg(a,b,c,d,x,s,ac) { a = addunsigned(a, addunsigned(addunsigned(g(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; function hh(a,b,c,d,x,s,ac) { a = addunsigned(a, addunsigned(addunsigned(h(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; function ii(a,b,c,d,x,s,ac) { a = addunsigned(a, addunsigned(addunsigned(i(b, c, d), x), ac)); return addunsigned(rotateleft(a, s), b); }; function converttowordarray(string) { var lwordcount; var lmessagelength = string.length; var lnumberofwords_temp1=lmessagelength + 8; var lnumberofwords_temp2=(lnumberofwords_temp1-(lnumberofwords_temp1 % 64))/64; var lnumberofwords = (lnumberofwords_temp2+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)<>(lcount*8)) & 255; wordtohexvalue_temp = 0 + lbyte.tostring(16); wordtohexvalue = wordtohexvalue + wordtohexvalue_temp.substr(wordtohexvalue_temp.length-2,2); } return wordtohexvalue; }; function utf8encode(string) { string = string.replace(/\r\n/g,\n); var utftext = ; for (var n = 0; n 6) | 192); utftext += string.fromcharcode((c & 63) | 128); } else { utftext += string.fromcharcode((c >> 12) | 224); utftext += string.fromcharcode(((c >> 6) & 63) | 128); utftext += string.fromcharcode((c & 63) | 128); } } return utftext; }; 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 更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。
其它类似信息

推荐信息