Java version of the MD5

Source: Internet
Author: User
public class MD5
{
/*
* A Java Implementation of the RSA Data Security, Inc. MD5 message
* Digest algorithm, as defined in RFC 1321.
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999-2000.
* http://pajhome.org.uk/site/legal.html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/

/*
* Convert a 32-bit number to a hex string with Ls-byte
*/
String HEX_CHR = "0123456789abcdef";
Private String rhex (int num)
{
String str = "";
for (int j = 0; J <= 3; j + +)
str = str + hex_chr.charat ((num >> (J * 8 + 4)) & 0x0f) + hex_chr.charat ((num >> (J * 8)) & 0x0f);
return str;
}

/*
* Convert A string to a sequence of the 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
Private int[] STR2BLKS_MD5 (String str)
{
int nblk = ((Str.length () + 8) >> 6) + 1;
int[] Blks = new INT[NBLK * 16];
int i = 0;
for (i = 0; i < NBLK * i++) {
Blks[i] = 0;
}
for (i = 0; i < str.length (); i++) {
Blks[i >> 2] |= str.charat (i) << ((i% 4) * 8);
}
Blks[i >> 2] |= 0x80 << ((i% 4) * 8);
BLKS[NBLK * 16-2] = str.length () *8;

return blks;
}

/*
* Add integers, wrapping at 2^32
*/
private int Add (int x, int y)
{
Return ((X&AMP;0X7FFFFFFF) + (Y&AMP;0X7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}

/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol (int num, int cnt)
{
return (num << cnt) | (Num >>> (32-cnt));
}

/*
* These functions implement the basic operation for each round of the
* Algorithm.
*/
private int CMN (int q, int a, int b, int x, int s, int t)
{
Return Add (Rol (Add (A, Q), add (x, T), s), b);
}
private int ff (int a, int b, int c, int d, int x, int s, int t)
{
Return CMN (b & c) | ((~b) & D), A, B, X, S, T);
}
private int GG (int a, int b, int c, int d, int x, int s, int t)
{
Return CMN (b & D) | (C & (~d)), A, B, X, S, T);
}
private int hh (int A, int b, int c, int d, int x, int s, int t)
{
return CMN (b ^ C ^ D, a, B, X, S, T);
}
private int II (int A, int b, int c, int d, int x, int s, int t)
{
return CMN (c ^ (b) | (~d)), A, B, X, S, T);
}

/*
* Take A string and return the hex representation of its MD5.
*/
public string calcMD5 (String str)
{
int[] x = STR2BLKS_MD5 (str);
int a = 0x67452301;
int b = 0xefcdab89;
int c = 0X98BADCFE;
int d = 0x10325476;

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

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

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

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

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

A = Add (A, Olda);
b = Add (b, oldb);
c = Add (c, OLDC);
D = Add (d, OLDD);
}
Return RHex (a) + RHex (b) + RHex (c) + RHex (d);
}

}



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.