MD5加密的javascript实现例子

来源:互联网 发布:软件专利范例 编辑:程序博客网 时间:2024/05/30 05:30
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
/* MD5 message-digest algorithm - javascript
' modification history:
' 1.0 16-feb-2001 - phil fresle (sales@frez.co.uk) - initial version (vb/asp code)
' 1.0 21-feb-2001 - enrico mosanghini (erik504@yahoo.com) - javascript porting
*/
function MD5(smessage) {
function rotateleft(lvalue, ishiftbits) { return (lvalue<<ishiftbits) | (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(smessage) {
var lwordcount;
var lmessagelength = smessage.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] | (smessage.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;
}
function wordtohex(lvalue) {
var wordtohexvalue="",wordtohexvalue_temp="",lbyte,lcount;
for (lcount = 0;lcount<=3;lcount++) {
lbyte = (lvalue>>>(lcount*8)) & 255;
wordtohexvalue_temp = "0" + lbyte.tostring(16);
wordtohexvalue = wordtohexvalue + wordtohexvalue_temp.substr(wordtohexvalue_temp.length-2,2);
}
return wordtohexvalue;
}
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;
// steps 1 and 2. append padding bits and length and convert to words
x = converttowordarray(smessage);
// step 3. initialise
a = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; d = 0x10325476;
// step 4. process the message in 16-word blocks
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);
}
// step 5. output the 128 bit digest
var temp= wordtohex(a)+wordtohex(b)+wordtohex(c)+wordtohex(d);
return temp.tolowercase();
}
<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>