JS MD5 Encryption Code

Source: Internet
Author: User
Tags md5 md5 encryption

function MD5 (instring) {
var hexcase = 0; /* Hex output format. 0-lowercase; 1-uppercase *
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance * *

 /*
  * These are the functions U ll usually want to call
  * They take string arguments and R Eturn either hex or BASE-64 encoded strings
  */
 function hex_md5 (s)     {return rstr2he X (RSTR_MD5 (Str2rstr_utf8 (s)));
 function b64_md5 (s)     {return rstr2b64 (RSTR_MD5 (Str2rstr_utf8 (s)));
 function ANY_MD5 (S, e) {return Rstr2any (RSTR_MD5 (Str2rstr_utf8 (s)), e);}
 function hex_hmac_md5 (k, D)
   {return Rstr2hex rstr_hmac_md5 (Str2rstr_utf8 (k), Str2rstr_ UTF8 (d)));
 function b64_hmac_md5 (k, D)
   {return rstr2b64 rstr_hmac_md5 (Str2rstr_utf8 (k), Str2rstr_ UTF8 (d)));
 function any_hmac_md5 (k, D, E)
   {return rstr2any rstr_hmac_md5 (Str2rstr_utf8 (k), Str2rstr_ UTF8 (d)), E); }

/*
* Perform a simple self-test to, if the VM is working
*/
function Md5_vm_test ()
{
Return HEX_MD5 ("abc"). toLowerCase () = = "900150983cd24fb0d6963f7d28e17f72";
}

/*
* Calculate the MD5 of a raw string
*/
function Rstr_md5 (s)
{
Return Binl2rstr (BINL_MD5 (Rstr2binl (s), s.length * 8));
}

/*
* Calculate the HMAC-MD5, a key and some data (raw strings)
*/
function Rstr_hmac_md5 (key, data)
{
var bkey = rstr2binl (key);
if (Bkey.length >) bkey = Binl_md5 (Bkey, key.length * 8);

var ipad = array, Opad = Array (16);
for (var i = 0; i < i++)
{
Ipad[i] = bkey[i] ^ 0x36363636;
Opad[i] = bkey[i] ^ 0x5c5c5c5c;
}

var hash = binl_md5 (rstr2binl (data), Ipad.concat + data.length * 8);
Return Binl2rstr (BINL_MD5 (Opad.concat (hash), 512 + 128));
}

/*
* Convert a raw string to a hex string
*/
function Rstr2hex (input)
{
try {hexcase} catch (e) {hexcase=0}
var hex_tab = hexcase? "0123456789ABCDEF": "0123456789abcdef";
var output = "";
var x;
for (var i = 0; i < input.length; i++)
{
x = Input.charcodeat (i);
Output + + Hex_tab.charat ((x >>> 4) & 0x0f)
+ Hex_tab.charat (x & 0x0f);
}
return output;
}

/*


* Convert a raw string to a base-64 string


*/


function rstr2b64 (input)


{


try {B64pad} catch (e) {b64pad= ';}


var tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";


var output = "";


var len = input.length;


for (var i = 0; i &lt; len; i = 3)


{


var triplet = (Input.charcodeat (i) &lt;&lt; 16)


| (i + 1 &lt; len Input.charcodeat (i+1) &lt;&lt; 8:0)


| (i + 2 &lt; len Input.charcodeat (i+2): 0);


for (var j = 0; J &lt; 4; J + +)


{


if (I * 8 + J * 6 &gt; Input.length * 8) output + + = B64pad;


else output + = Tab.charat ((triplet &gt;&gt;&gt; 6* (3-j)) &amp; 0x3F);


}


}


return output;


}

/*
* Convert a raw string to an arbitrary string encoding
*/
function Rstr2any (input, encoding)
{
var divisor = encoding.length;
var i, J, Q, X, quotient;

/* Convert to an array of 16-bit Big-endian values, forming the dividend * *
var dividend = Array (Math.ceil (INPUT.LENGTH/2));
for (i = 0; i < dividend.length; i++)
{
Dividend[i] = (Input.charcodeat (i * 2) << 8) | Input.charcodeat (i * 2 + 1);
}

/*


* Repeatedly perform a long division. The binary array forms the dividend,


* The length of the encoding is the divisor. Once computed, the quotient


* Forms the dividend for the next step. All remainders are stored for later


* use.


*/


var full_length = Math.ceil (Input.length * 8/


(Math.log (encoding.length)/Math.log (2)));


var remainders = Array (full_length);


for (j = 0; J &lt; Full_length; J + +)


{


quotient = Array ();


x = 0;


for (i = 0; i &lt; dividend.length; i++)


{


x = (x &lt;&lt;) + dividend[i];


Q = Math.floor (x/divisor);


x = q * DIVISOR;


if (Quotient.length &gt; 0 | | | q &gt; 0)


Quotient[quotient.length] = q;


}


REMAINDERS[J] = x;


Dividend = quotient;


}

* Convert the remainders to the output string */
var output = "";
for (i = remainders.length-1 i >= 0; i--)
Output + + Encoding.charat (remainders[i]);

return output;
}

/*
* Encode a string as Utf-8.
* For efficiency, this assumes the input is valid utf-16.
*/
function Str2rstr_utf8 (input)
{
var output = "";
var i =-1;
var x, y;

while (++i < input.length)
{
/* Decode utf-16 Surrogate Pairs * *
x = Input.charcodeat (i);
y = i + 1 < input.length? Input.charcodeat (i + 1): 0;
if (0xd800 <= x && x <= 0xDBFF && 0xdc00 <= y && y <= 0xDFFF)
{
x = 0x10000 + ((x & 0X03FF) << ten) + (Y & 0x03ff);
i++;
}

/* Encode OUTPUT as Utf-8 * *


if (x &lt;= 0x7F)


Output + + string.fromcharcode (x);


else if (x &lt;= 0x7ff)


Output + + string.fromcharcode (0xc0 | ((x &gt;&gt;&gt; 6) &amp; 0x1F),


0x80 | (x &amp; 0x3F));


else if (x &lt;= 0xFFFF)


Output + + string.fromcharcode (0xe0 | ((x &gt;&gt;&gt;) &amp; 0x0f),


0x80 | ((x &gt;&gt;&gt; 6) &amp; 0x3F),


0x80 | (x &amp; 0x3F));


else if (x &lt;= 0x1fffff)


Output + + string.fromcharcode (0xF0 | ((x &gt;&gt;&gt; a) &amp; 0x07),


0x80 | ((x &gt;&gt;&gt;) &amp; 0x3F),


0x80 | ((x &gt;&gt;&gt; 6) &amp; 0x3F),


0x80 | (x &amp; 0x3F));


}


return output;


}

/*
* Encode a string as utf-16
*/
function Str2rstr_utf16le (input)
{
var output = "";
for (var i = 0; i < input.length; i++)
Output + + String.fromCharCode (input.charcodeat (i) & 0xFF,
(Input.charcodeat (i) >>> 8) & 0xFF);
return output;
}

 function str2rstr_utf16be (input)
 {
   var output = "";
   for (var i = 0; i < input.length i++)
     output + = String.fromCharCode (input.ch Arcodeat (i) >>> 8) & 0xFF,
                                      input.charcodeat (i)         & 0xFF);
   return output;
 }

 /*
  * Convert a raw string to an array of Little-endian words
  * Characters >255 have thei R High-byte silently ignored.
  */
 function rstr2binl (input)
 {
   var output = Array (Input.length >> 2 );
   for (var i = 0; i < output.length i++)
     output[i] = 0;
   F or (var i = 0; i < input.length * 8; i + + 8)
     output[i>>5] |= (input.charcodeat (I/8) & 0xFF) << (i%32);
   return output;
 }

/*
* Convert An array of Little-endian words to a string
*/
function Binl2rstr (input)
{
var output = "";
for (var i = 0; i < input.length * i + + 8)
Output + + String.fromCharCode ((input[i>>5] >>> (i%)) & 0xFF);
return output;
}

/*
* Calculate The MD5 of an array of Little-endian words, and a bit length.
*/
function Binl_md5 (x, Len)
{
/* Append padding * *
X[len >> 5] |= 0x80 << ((len)% 32);
X[((len +) >>> 9) << 4) + [] = Len;

var a = 1732584193;
var b =-271733879;
var c =-1732584194;
var d = 271733878;

for (var i = 0; i < x.length i + + 16)
{
var Olda = A;
var oldb = b;
var oldc = c;
var oldd = D;

A = Md5_ff (A, B, C, D, x[i+ 0], 7,-680876936);


D = md5_ff (d, a, B, C, x[i+ 1], 12,-389564586);


c = md5_ff (c, D, a, B, x[i+ 2], 17, 606105819);


b = md5_ff (b, C, D, A, x[i+ 3], 22,-1044525330);


A = Md5_ff (A, B, C, D, x[i+ 4], 7,-176418897);


D = md5_ff (d, a, B, C, x[i+ 5], 12, 1200080426);


c = md5_ff (c, D, a, B, x[i+ 6], 17,-1473231341);


b = md5_ff (b, C, D, A, x[i+ 7], 22,-45705983);


A = Md5_ff (A, B, C, D, x[i+ 8], 7, 1770035416);


D = md5_ff (d, a, B, C, x[i+ 9], 12,-1958414417);


c = md5_ff (c, D, a, B, x[i+10], 17,-42063);


b = md5_ff (b, C, D, A, x[i+11], 22,-1990404162);


A = Md5_ff (A, B, C, D, X[i+12], 7, 1804603682);


D = md5_ff (d, a, B, C, x[i+13], 12,-40341101);


c = md5_ff (c, D, a, B, x[i+14], 17,-1502002290);


b = md5_ff (b, C, D, A, x[i+15], 22, 1236535329);

A = Md5_gg (A, B, C, D, x[i+ 1], 5,-165796510);


D = Md5_gg (d, a, B, C, x[i+ 6], 9,-1069501632);


c = Md5_gg (c, D, a, B, x[i+11], 14, 643717713);


b = Md5_gg (b, C, D, A, x[i+ 0], 20,-373897302);


A = Md5_gg (A, B, C, D, x[i+ 5], 5,-701558691);


D = Md5_gg (d, a, B, C, x[i+10], 9, 38016083);


c = Md5_gg (c, D, a, B, x[i+15], 14,-660478335);


b = Md5_gg (b, C, D, A, x[i+ 4], 20,-405537848);


A = Md5_gg (A, B, C, D, x[i+ 9], 5, 568446438);


D = Md5_gg (d, a, B, C, x[i+14], 9,-1019803690);


c = Md5_gg (c, D, a, B, x[i+ 3], 14,-187363961);


b = Md5_gg (b, C, D, A, x[i+ 8], 20, 1163531501);


A = Md5_gg (A, B, C, D, X[i+13], 5,-1444681467);


D = Md5_gg (d, a, B, C, x[i+ 2], 9,-51403784);


c = Md5_gg (c, D, a, B, x[i+ 7], 14, 1735328473);


b = Md5_gg (b, C, D, A, x[i+12], 20,-1926607734);

A = Md5_hh (A, B, C, D, x[i+ 5], 4,-378558);


D = md5_hh (d, a, B, C, x[i+ 8], 11,-2022574463);


c = md5_hh (c, D, a, B, x[i+11], 16, 1839030562);


b = md5_hh (b, C, D, A, x[i+14], 23,-35309556);


A = Md5_hh (A, B, C, D, x[i+ 1], 4,-1530992060);


D = md5_hh (d, a, B, C, x[i+ 4], 11, 1272893353);


c = md5_hh (c, D, a, B, x[i+ 7], 16,-155497632);


b = md5_hh (b, C, D, A, x[i+10], 23,-1094730640);


A = Md5_hh (A, B, C, D, X[i+13], 4, 681279174);


D = md5_hh (d, a, B, C, x[i+ 0], 11,-358537222);


c = md5_hh (c, D, a, B, x[i+ 3], 16,-722521979);


b = md5_hh (b, C, D, A, x[i+ 6], 23, 76029189);


A = Md5_hh (A, B, C, D, x[i+ 9], 4,-640364487);


D = md5_hh (d, a, B, C, x[i+12], 11,-421815835);


c = md5_hh (c, D, a, B, x[i+15], 16, 530742520);


b = md5_hh (b, C, D, A, x[i+ 2], 23,-995338651);

A = Md5_ii (A, B, C, D, x[i+ 0], 6,-198630844);


D = Md5_ii (d, a, B, C, x[i+ 7], 10, 1126891415);


c = Md5_ii (c, D, a, B, x[i+14], 15,-1416354905);


b = Md5_ii (b, C, D, A, x[i+ 5], 21,-57434055);


A = Md5_ii (A, B, C, D, X[i+12], 6, 1700485571);


D = Md5_ii (d, a, B, C, x[i+ 3], 10,-1894986606);


c = Md5_ii (c, D, a, B, x[i+10], 15,-1051523);


b = Md5_ii (b, C, D, A, x[i+ 1], 21,-2054922799);


A = Md5_ii (A, B, C, D, x[i+ 8], 6, 1873313359);


D = Md5_ii (d, a, B, C, x[i+15], 10,-30611744);


c = Md5_ii (c, D, a, B, x[i+ 6], 15,-1560198380);


b = Md5_ii (b, C, D, A, x[i+13], 21, 1309151649);


A = Md5_ii (A, B, C, D, x[i+ 4], 6,-145523070);


D = Md5_ii (d, a, B, C, x[i+11], 10,-1120210379);


c = Md5_ii (c, D, a, B, x[i+ 2], 15, 718787259);


b = Md5_ii (b, C, D, A, x[i+ 9], 21,-343485551);

A = Safe_add (A, Olda);
b = Safe_add (b, oldb);
c = Safe_add (c, OLDC);
D = Safe_add (d, OLDD);
}
Return Array (A, B, C, D);
}

 /*
  * These functions implement the four basic operations the algorithm uses.
  */
 f Unction MD5_CMN (q, A, B, X, S, t)
 {
   return safe_add Bit_rol (Safe_add (Safe_add (A, Q), Safe_add (x , T)), s), b);
 }
 function md5_ff (A, B, C, D, X, S, t)
 {
   return md5_cmn ((b & c) | ((~b) & D), A, B, X, S, T);
 }
 function Md5_gg (A, B, C, D, X, S, t)
 {
   return md5_cmn ((b & D) | (C & (~d)), A, B, X, S, T);
 }
 function md5_hh (A, B, C, D, X, S, t)
 {
   return md5_cmn (b ^ C ^ D, a, B, X, S, t);
&NBSP}
 function Md5_ii (A, B, C, D, X, S, t)
 {
   return md5_cmn (c ^ B | (~d)), A, B, X, S, T);
 }

/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* To work around bugs in some JS interpreters.
*/
function Safe_add (x, y)
{
var LSW = (x & 0xFFFF) + (Y & 0xFFFF);
var MSW = (x >>) + (y >>) + (LSW >> 16);
Return (MSW << 16) | (LSW & 0xFFFF);
}

/*
* Bitwise rotate a 32-bit number to the left.
*/
function Bit_rol (num, CNT)
{
return (num << cnt) | (Num >>> (32-cnt));
}

Return Hex_md5 (instring);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.