首页 > 编程地带 > MD5 Js加密函数

MD5 Js加密函数

2007年10月1日 发表评论 阅读评论
<br /><script><br />/*<br /> * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message<br /> * Digest Algorithm, as defined in RFC 1321.<br /> * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.<br /> * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet<br /> * Distributed under the BSD License<br /> * See http://pajhome.org.uk/crypt/md5 for more info.<br /> */<br /><br />/*<br /> * Configurable variables. You may need to tweak these to be compatible with<br /> * the server-side, but the defaults work in most cases.<br /> */<br />var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */<br />var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */<br />var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */<br /><br />/*<br /> * These are the functions you&#039;ll usually want to call<br /> * They take string arguments and return either hex or base-64 encoded strings<br /> */<br />function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}<br />function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}<br />function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}<br />function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }<br />function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }<br />function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }<br /><br />/*<br /> * Perform a simple self-test to see if the VM is working<br /> */<br />function md5_vm_test()<br />{<br />  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";<br />}<br /><br />/*<br /> * Calculate the MD5 of an array of little-endian words, and a bit length<br /> */<br />function core_md5(x, len)<br />{<br />  /* append padding */<br />  x&#91;len >> 5&#93; &#124;= 0x80 << ((len) % 32);<br />  x&#91;(((len + 64) >>> 9) << 4) + 14&#93; = len;<br /><br />  var a =  1732584193;<br />  var b = -271733879;<br />  var c = -1732584194;<br />  var d =  271733878;<br /><br />  for(var i = 0; i < x.length; i += 16)<br />  {<br />    var olda = a;<br />    var oldb = b;<br />    var oldc = c;<br />    var oldd = d;<br /><br />    a = md5_ff(a, b, c, d, x&#91;i+ 0&#93;, 7 , -680876936);<br />    d = md5_ff(d, a, b, c, x&#91;i+ 1&#93;, 12, -389564586);<br />    c = md5_ff(c, d, a, b, x&#91;i+ 2&#93;, 17,  606105819);<br />    b = md5_ff(b, c, d, a, x&#91;i+ 3&#93;, 22, -1044525330);<br />    a = md5_ff(a, b, c, d, x&#91;i+ 4&#93;, 7 , -176418897);<br />    d = md5_ff(d, a, b, c, x&#91;i+ 5&#93;, 12,  1200080426);<br />    c = md5_ff(c, d, a, b, x&#91;i+ 6&#93;, 17, -1473231341);<br />    b = md5_ff(b, c, d, a, x&#91;i+ 7&#93;, 22, -45705983);<br />    a = md5_ff(a, b, c, d, x&#91;i+ 8&#93;, 7 ,  1770035416);<br />    d = md5_ff(d, a, b, c, x&#91;i+ 9&#93;, 12, -1958414417);<br />    c = md5_ff(c, d, a, b, x&#91;i+10&#93;, 17, -42063);<br />    b = md5_ff(b, c, d, a, x&#91;i+11&#93;, 22, -1990404162);<br />    a = md5_ff(a, b, c, d, x&#91;i+12&#93;, 7 ,  1804603682);<br />    d = md5_ff(d, a, b, c, x&#91;i+13&#93;, 12, -40341101);<br />    c = md5_ff(c, d, a, b, x&#91;i+14&#93;, 17, -1502002290);<br />    b = md5_ff(b, c, d, a, x&#91;i+15&#93;, 22,  1236535329);<br /><br />    a = md5_gg(a, b, c, d, x&#91;i+ 1&#93;, 5 , -165796510);<br />    d = md5_gg(d, a, b, c, x&#91;i+ 6&#93;, 9 , -1069501632);<br />    c = md5_gg(c, d, a, b, x&#91;i+11&#93;, 14,  643717713);<br />    b = md5_gg(b, c, d, a, x&#91;i+ 0&#93;, 20, -373897302);<br />    a = md5_gg(a, b, c, d, x&#91;i+ 5&#93;, 5 , -701558691);<br />    d = md5_gg(d, a, b, c, x&#91;i+10&#93;, 9 ,  38016083);<br />    c = md5_gg(c, d, a, b, x&#91;i+15&#93;, 14, -660478335);<br />    b = md5_gg(b, c, d, a, x&#91;i+ 4&#93;, 20, -405537848);<br />    a = md5_gg(a, b, c, d, x&#91;i+ 9&#93;, 5 ,  568446438);<br />    d = md5_gg(d, a, b, c, x&#91;i+14&#93;, 9 , -1019803690);<br />    c = md5_gg(c, d, a, b, x&#91;i+ 3&#93;, 14, -187363961);<br />    b = md5_gg(b, c, d, a, x&#91;i+ 8&#93;, 20,  1163531501);<br />    a = md5_gg(a, b, c, d, x&#91;i+13&#93;, 5 , -1444681467);<br />    d = md5_gg(d, a, b, c, x&#91;i+ 2&#93;, 9 , -51403784);<br />    c = md5_gg(c, d, a, b, x&#91;i+ 7&#93;, 14,  1735328473);<br />    b = md5_gg(b, c, d, a, x&#91;i+12&#93;, 20, -1926607734);<br /><br />    a = md5_hh(a, b, c, d, x&#91;i+ 5&#93;, 4 , -378558);<br />    d = md5_hh(d, a, b, c, x&#91;i+ 8&#93;, 11, -2022574463);<br />    c = md5_hh(c, d, a, b, x&#91;i+11&#93;, 16,  1839030562);<br />    b = md5_hh(b, c, d, a, x&#91;i+14&#93;, 23, -35309556);<br />    a = md5_hh(a, b, c, d, x&#91;i+ 1&#93;, 4 , -1530992060);<br />    d = md5_hh(d, a, b, c, x&#91;i+ 4&#93;, 11,  1272893353);<br />    c = md5_hh(c, d, a, b, x&#91;i+ 7&#93;, 16, -155497632);<br />    b = md5_hh(b, c, d, a, x&#91;i+10&#93;, 23, -1094730640);<br />    a = md5_hh(a, b, c, d, x&#91;i+13&#93;, 4 ,  681279174);<br />    d = md5_hh(d, a, b, c, x&#91;i+ 0&#93;, 11, -358537222);<br />    c = md5_hh(c, d, a, b, x&#91;i+ 3&#93;, 16, -722521979);<br />    b = md5_hh(b, c, d, a, x&#91;i+ 6&#93;, 23,  76029189);<br />    a = md5_hh(a, b, c, d, x&#91;i+ 9&#93;, 4 , -640364487);<br />    d = md5_hh(d, a, b, c, x&#91;i+12&#93;, 11, -421815835);<br />    c = md5_hh(c, d, a, b, x&#91;i+15&#93;, 16,  530742520);<br />    b = md5_hh(b, c, d, a, x&#91;i+ 2&#93;, 23, -995338651);<br /><br />    a = md5_ii(a, b, c, d, x&#91;i+ 0&#93;, 6 , -198630844);<br />    d = md5_ii(d, a, b, c, x&#91;i+ 7&#93;, 10,  1126891415);<br />    c = md5_ii(c, d, a, b, x&#91;i+14&#93;, 15, -1416354905);<br />    b = md5_ii(b, c, d, a, x&#91;i+ 5&#93;, 21, -57434055);<br />    a = md5_ii(a, b, c, d, x&#91;i+12&#93;, 6 ,  1700485571);<br />    d = md5_ii(d, a, b, c, x&#91;i+ 3&#93;, 10, -1894986606);<br />    c = md5_ii(c, d, a, b, x&#91;i+10&#93;, 15, -1051523);<br />    b = md5_ii(b, c, d, a, x&#91;i+ 1&#93;, 21, -2054922799);<br />    a = md5_ii(a, b, c, d, x&#91;i+ 8&#93;, 6 ,  1873313359);<br />    d = md5_ii(d, a, b, c, x&#91;i+15&#93;, 10, -30611744);<br />    c = md5_ii(c, d, a, b, x&#91;i+ 6&#93;, 15, -1560198380);<br />    b = md5_ii(b, c, d, a, x&#91;i+13&#93;, 21,  1309151649);<br />    a = md5_ii(a, b, c, d, x&#91;i+ 4&#93;, 6 , -145523070);<br />    d = md5_ii(d, a, b, c, x&#91;i+11&#93;, 10, -1120210379);<br />    c = md5_ii(c, d, a, b, x&#91;i+ 2&#93;, 15,  718787259);<br />    b = md5_ii(b, c, d, a, x&#91;i+ 9&#93;, 21, -343485551);<br /><br />    a = safe_add(a, olda);<br />    b = safe_add(b, oldb);<br />    c = safe_add(c, oldc);<br />    d = safe_add(d, oldd);<br />  }<br />  return Array(a, b, c, d);<br /><br />}<br /><br />/*<br /> * These functions implement the four basic operations the algorithm uses.<br /> */<br />function md5_cmn(q, a, b, x, s, t)<br />{<br />  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);<br />}<br />function md5_ff(a, b, c, d, x, s, t)<br />{<br />  return md5_cmn((b & c) &#124; ((~b) & d), a, b, x, s, t);<br />}<br />function md5_gg(a, b, c, d, x, s, t)<br />{<br />  return md5_cmn((b & d) &#124; (c & (~d)), a, b, x, s, t);<br />}<br />function md5_hh(a, b, c, d, x, s, t)<br />{<br />  return md5_cmn(b ^ c ^ d, a, b, x, s, t);<br />}<br />function md5_ii(a, b, c, d, x, s, t)<br />{<br />  return md5_cmn(c ^ (b &#124; (~d)), a, b, x, s, t);<br />}<br /><br />/*<br /> * Calculate the HMAC-MD5, of a key and some data<br /> */<br />function core_hmac_md5(key, data)<br />{<br />  var bkey = str2binl(key);<br />  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);<br /><br />  var ipad = Array(16), opad = Array(16);<br />  for(var i = 0; i < 16; i++)<br />  {<br />    ipad&#91;i&#93; = bkey&#91;i&#93; ^ 0x36363636;<br />    opad&#91;i&#93; = bkey&#91;i&#93; ^ 0x5C5C5C5C;<br />  }<br /><br />  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);<br />  return core_md5(opad.concat(hash), 512 + 128);<br />}<br /><br />/*<br /> * Add integers, wrapping at 2^32. This uses 16-bit operations internally<br /> * to work around bugs in some JS interpreters.<br /> */<br />function safe_add(x, y)<br />{<br />  var lsw = (x & 0xFFFF) + (y & 0xFFFF);<br />  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);<br />  return (msw << 16) &#124; (lsw & 0xFFFF);<br />}<br /><br />/*<br /> * Bitwise rotate a 32-bit number to the left.<br /> */<br />function bit_rol(num, cnt)<br />{<br />  return (num << cnt) &#124; (num >>> (32 - cnt));<br />}<br /><br />/*<br /> * Convert a string to an array of little-endian words<br /> * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.<br /> */<br />function str2binl(str)<br />{<br />  var bin = Array();<br />  var mask = (1 << chrsz) - 1;<br />  for(var i = 0; i < str.length * chrsz; i += chrsz)<br />    bin&#91;i>>5&#93; &#124;= (str.charCodeAt(i / chrsz) & mask) << (i%32);<br />  return bin;<br />}<br /><br />/*<br /> * Convert an array of little-endian words to a string<br /> */<br />function binl2str(bin)<br />{<br />  var str = "";<br />  var mask = (1 << chrsz) - 1;<br />  for(var i = 0; i < bin.length * 32; i += chrsz)<br />    str += String.fromCharCode((bin&#91;i>>5&#93; >>> (i % 32)) & mask);<br />  return str;<br />}<br /><br />/*<br /> * Convert an array of little-endian words to a hex string.<br /> */<br />function binl2hex(binarray)<br />{<br />  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";<br />  var str = "";<br />  for(var i = 0; i < binarray.length * 4; i++)<br />  {<br />    str += hex_tab.charAt((binarray&#91;i>>2&#93; >> ((i%4)*8+4)) & 0xF) +<br />           hex_tab.charAt((binarray&#91;i>>2&#93; >> ((i%4)*8  )) & 0xF);<br />  }<br />  return str;<br />}<br /><br />/*<br /> * Convert an array of little-endian words to a base-64 string<br /> */<br />function binl2b64(binarray)<br />{<br />  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";<br />  var str = "";<br />  for(var i = 0; i < binarray.length * 4; i += 3)<br />  {<br />    var triplet = (((binarray&#91;i   >> 2&#93; >> 8 * ( i   %4)) & 0xFF) << 16)<br />                &#124; (((binarray&#91;i+1 >> 2&#93; >> 8 * ((i+1)%4)) & 0xFF) << 8 )<br />                &#124;  ((binarray&#91;i+2 >> 2&#93; >> 8 * ((i+2)%4)) & 0xFF);<br />    for(var j = 0; j < 4; j++)<br />    {<br />      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;<br />      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);<br />    }<br />  }<br />  return str;<br />}<br />String.prototype.trim = function()<br />{<br />    return this.replace(/(^&#92;s*)&#124;(&#92;s*$)/g, "");<br />}<br />var lastInput;<br />function md5(text) {<br />    text=text.trim();<br />    var reg=/^&#91;0-9a-f&#93;{16}$&#124;^&#91;0-9a-f&#93;{32}$/;<br />    if (reg.test(text))<br />    {<br />        ctl00_ContentPlaceHolder1_LabelResult.innerHTML=&#039;&#039;;<br />    }<br />    else<br />    {<br />     var ret = hex_md5(text);<br />     ctl00_ContentPlaceHolder1_LabelResult.innerHTML = &#039;MD5(&#039;+ text +&#039;,32) = &#039;+ ret+&#039;<br>&#039; ;<br />     ctl00_ContentPlaceHolder1_LabelResult.innerHTML += &#039;MD5(&#039;+ text +&#039;,16) = &#039;+ ret.substr(8,16);<br />     lastInput=text;<br /> }<br /> <br />}<br />function CheckInput()<br />{<br /><br /><br />    var text=document.all&#91;"ctl00_ContentPlaceHolder1_TextBoxq"&#93;.value.toLowerCase();<br />    text=text.trim();<br />    var reg=/^&#91;0-9a-f&#93;{16}$&#124;^&#91;0-9a-f&#93;{32}$/;<br />    if (reg.test(text))<br />    {<br />        if (lastInput+   ""   !=   "undefined" )<br />        {<br />            var result=hex_md5(lastInput);<br />            if (result.indexOf(text)>=0)<br />            {<br />                ctl00_ContentPlaceHolder1_LabelResult.innerHTML=lastInput;<br />                return false;<br />            }<br />        }<br />        else<br />        {<br />            var lastResult=ctl00_ContentPlaceHolder1_LabelResult.innerHTML;<br />            var result=hex_md5(lastResult);<br />            if (result.indexOf(text)>=0)<br />            {<br />                ctl00_ContentPlaceHolder1_LabelResult.innerHTML=lastResult;<br />                return false;<br />            }           <br />        }<br />       <br />       <br />        ctl00_ContentPlaceHolder1_LabelResult.innerHTML="正在查找......";<br />        return true;<br />    }<br />    else<br />    {<br />        md5(text);<br />        return false;<br />    }<br />       <br />}<br /></script><br />    <center><br />        <h1>MD5在线加密</h1><br><br />    </center><br /><br />    <table align = "center"  border = "0" cellpadding = "3" cellspacing = "0" width = "560"><br />        <tr><br />            <td colspan = "2" style = "padding-right: 6px; padding-left: 6px; padding-bottom: 3px;<br />                                                padding-top: 3px; height: 28px"><br />                <font style = "line-height: 11pt"><br /><br />                <input name = "ctl00$ContentPlaceHolder1$TextBoxq" type = "text"<br />                id = "ctl00_ContentPlaceHolder1_TextBoxq" onKeyUp = "md5(this.value);" style = "width:540px;"/> <br />                   </font><br />            </td><br />        </tr><br />    </table><br /><br />    <table align = "center" bgcolor = "#dddddd" border = "0" cellpadding = "0" cellspacing = "1" width = "560"><br />        <tr><br />            <td bgcolor = "#eeeeee" style = "padding-right: 16px; padding-left: 8px; padding-bottom: 15px;<br />                                    line-height: 14pt; padding-top: 10px; text-align: left; height: 82px;"<br />                valign = "top"><br />                <span style = "text-align: left"> <span id = "Label1" style = "color: #0000c0"></span> 加密结果: <br /><br />                <br /><br /><br />                <span id = "ctl00_ContentPlaceHolder1_LabelResult"></span><br /><br />                <br /> </span><br /><br />                <div><br />                     <br />                </div><br />            </td><br />        </tr><br />    </table>

转载请注明:woyigui's blog [http://www.woyigui.cn/]
本文标题:MD5 Js加密函数
本文地址:http://www.woyigui.cn/2007/10/01/md5js%E5%8A%A0%E5%AF%86%E5%87%BD%E6%95%B0/

分类: 编程地带 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*