The implementation of MD5 algorithm in JS

Source: Internet
Author: User
Tags add array md5 pow return string
js| algorithm

<script language= "JavaScript" >
<!--
/**********************************************************

* Md5.js
*
* A JavaScript Implementation of the RSA Data Security, Inc. MD5
* Message-digest algorithm.
*
* Copyright (C) Paul Johnston 1999. Distributed under the LGPL.
***********************************************************/

* To convert strings to a list of ASCII values * *
var sascii = "!\" #$%& ' () *+,-./0123456789:;<=>? @ABCDEFGHIJKLMNOPQRSTUVWXYZ "
var sascii = Sascii + "[\\]^_ ' abcdefghijklmnopqrstuvwxyz{|}" ~";

/* Convert integer to hex String * *
var ShEx = "0123456789ABCDEF";
function Hex (i)
{
h = "";
for (j = 0; J <= 3; j + +)
{
H + + Shex.charat ((i >> (J * 8 + 4)) & 0x0f) +
Shex.charat ((I >> (J * 8)) & 0x0f);
}
return h;
}

/* Add, handling overflows correctly * *
function add (x, y)
{
Return ((X&0X7FFFFFFF) + (Y&0X7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}

/* MD5 rounds functions * *
function R1 (A, B, C, D, X, S, T)
{
Q = Add (Add (A, (B & C) | (~b & D)), add (X, T));
return Add (q << S) | (q >> (32-s)) & (Math.pow (2, S)-1), B);
}

function R2 (A, B, C, D, X, S, T)
{
Q = Add (Add (A, (B & D) | (C & ~d)), add (X, T));
return Add (q << S) | (q >> (32-s)) & (Math.pow (2, S)-1), B);
}

function R3 (A, B, C, D, X, S, T)
{
Q = Add (Add (A, B ^ C ^ D), add (X, T));
return Add (q << S) | (q >> (32-s)) & (Math.pow (2, S)-1), B);
}

function R4 (A, B, C, D, X, S, T)
{
Q = Add (Add (A, C ^ (B | ~d)), add (X, T));
return Add (q << S) | (q >> (32-s)) & (Math.pow (2, S)-1), B);
}

/* Main entry point * *
function CalcMD5 (SINP) {

/* Calculate length in machine words, including padding * *
Wlen = ((Sinp.length + 8) >> 6) + 1) << 4;
var X = new Array (Wlen);

/* Convert string to array of words * *
j = 4;
for (i = 0; (i * 4) < sinp.length; i++)
{
X[i] = 0;
for (j = 0; (J < 4) && ((j + i * 4) < sinp.length); J + +)
{
X[i] + + (Sascii.indexof (Sinp.charat (i * 4) + j)) << (J * 8);
}
}

/* Append padding bits and length * *
if (j = = 4)
{
x[i++] = 0x80;
}
Else
{
X[I-1] + + 0x80 << (J * 8);
}
for (; i < Wlen; i++) {x[i] = 0;}
X[WLEN-2] = sinp.length * 8;

/* hard-coded Initial values * *
A = 0x67452301;
b = 0xefcdab89;
c = 0x98badcfe;
D = 0x10325476;

/* Process each 16-word blocks in turn * *
for (i = 0; i < Wlen i + + 16) {
AO = A;
BO = b;
CO = C;
do = D;

A = R1 (A, B, C, D, x[i+ 0], 7, 0xd76aa478);
D = R1 (d, a, B, C, x[i+ 1], 0xe8c7b756);
C = R1 (c, D, a, B, x[i+ 2], 0x242070db);
B = R1 (b, C, D, A, x[i+ 3], 0xc1bdceee);
A = R1 (A, B, C, D, x[i+ 4], 7, 0XF57C0FAF);
D = R1 (d, a, B, C, x[i+ 5], 0x4787c62a);
C = R1 (c, D, a, B, x[i+ 6], 0xa8304613);
B = R1 (b, C, D, A, x[i+ 7], 0xfd469501);
A = R1 (A, B, C, D, x[i+ 8], 7, 0X698098D8);
D = R1 (d, a, B, C, x[i+ 9], 0X8B44F7AF);
C = R1 (c, D, a, B, x[i+10], 0XFFFF5BB1);
B = R1 (b, C, D, A, x[i+11], 0x895cd7be);
A = R1 (A, B, C, D, X[i+12], 7, 0x6b901122);
D = R1 (d, a, B, C, x[i+13], 0xfd987193);
C = R1 (c, D, a, B, x[i+14], 0xa679438e);
B = R1 (b, C, D, A, x[i+15], 0x49b40821);

A = R2 (A, B, C, D, x[i+ 1], 5, 0xf61e2562);
D = R2 (d, a, B, C, x[i+ 6], 9, 0xc040b340);
C = R2 (c, D, a, B, x[i+11], 0x265e5a51);
B = R2 (b, C, D, A, x[i+ 0], 0XE9B6C7AA);
A = R2 (A, B, C, D, x[i+ 5], 5, 0xd62f105d);
D = R2 (d, a, B, C, x[i+10], 9, 0x2441453);
C = R2 (c, D, a, B, x[i+15], 0xd8a1e681);
B = R2 (b, C, D, A, x[i+ 4], 0XE7D3FBC8);
A = R2 (A, B, C, D, x[i+ 9], 5, 0x21e1cde6);
D = R2 (d, a, B, C, x[i+14], 9, 0xc33707d6);
C = R2 (c, D, a, B, x[i+ 3], 0xf4d50d87);
B = R2 (b, C, D, A, x[i+ 8], 0x455a14ed);
A = R2 (A, B, C, D, X[i+13], 5, 0xa9e3e905);
D = R2 (d, a, B, C, x[i+ 2], 9, 0XFCEFA3F8);
C = R2 (c, D, a, B, x[i+ 7], 0x676f02d9);
B = R2 (b, C, D, A, x[i+12], 0x8d2a4c8a);

A = R3 (A, B, C, D, x[i+ 5], 4, 0xfffa3942);
D = R3 (d, a, B, C, x[i+ 8], one, 0x8771f681);
C = R3 (c, D, a, B, x[i+11], 0x6d9d6122);
B = R3 (b, C, D, A, x[i+14], 0xfde5380c);
A = R3 (A, B, C, D, x[i+ 1], 4, 0xa4beea44);
D = R3 (d, a, B, C, x[i+ 4], one, 0x4bdecfa9);
C = R3 (c, D, a, B, x[i+ 7], 0xf6bb4b60);
B = R3 (b, C, D, A, x[i+10], 0XBEBFBC70);
A = R3 (A, B, C, D, X[i+13], 4, 0X289B7EC6);
D = R3 (d, a, B, C, x[i+ 0], one, 0XEAA127FA);
C = R3 (c, D, a, B, x[i+ 3], 0xd4ef3085);
B = R3 (b, C, D, A, x[i+ 6], 0x4881d05);
A = R3 (A, B, C, D, x[i+ 9], 4, 0xd9d4d039);
D = R3 (d, a, B, C, x[i+12], one, 0xe6db99e5);
C = R3 (c, D, a, B, x[i+15], 0x1fa27cf8);
B = R3 (b, C, D, A, x[i+ 2], 0xc4ac5665);

A = R4 (A, B, C, D, x[i+ 0], 6, 0xf4292244);
D = R4 (d, a, B, C, x[i+ 7], 0x432aff97);
c = R4 (c, D, a, B, x[i+14], 0xab9423a7);
b = R4 (b, C, D, A, x[i+ 5], 0xfc93a039);
A = R4 (A, B, C, D, X[i+12], 6, 0X655B59C3);
D = R4 (d, a, B, C, x[i+ 3], 0x8f0ccc92);
c = R4 (c, D, a, B, x[i+10], 0xffeff47d);
b = R4 (b, C, D, A, x[i+ 1], 0X85845DD1);
A = R4 (A, B, C, D, x[i+ 8], 6, 0x6fa87e4f);
D = R4 (d, a, B, C, x[i+15], 0xfe2ce6e0);
c = R4 (c, D, a, B, x[i+ 6], 0xa3014314);
b = R4 (b, C, D, A, x[i+13], 0X4E0811A1);
A = R4 (A, B, C, D, x[i+ 4], 6, 0xf7537e82);
D = R4 (d, a, B, C, x[i+11], 0xbd3af235);
c = R4 (c, D, a, B, x[i+ 2], 0X2AD7D2BB);
b = R4 (b, C, D, A, x[i+ 9], 0xeb86d391);

A = Add (A, AO);
b = Add (b, BO);
c = Add (c, CO);
D = Add (d, do);
}
Return Hex (a) + hex (b) + hex (c) + hex (d);
}

Alert (Hex ("123456"))
-->
</script>



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.