Java version of MD5

xiaoxiao2021-03-06  107

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.

* See 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 first

* /

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 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

BLKS [I] = 0;

}

For (i = 0; 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, Wraping AT 2 ^ 32

* /

Private Int Add (int X, int y)

{

RETURN ((x & 0x7fffffff) (Y & 0x7FFFFFF)) ^ (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));

}

/ *

.

* /

Private int CMN (int Q, Int A, int B, INT X, INT S, INT T)

{

RETURN ADD (ADD (Add (ADD (AD (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

{

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], 12, 0xe8c7b756);

C = ff (C, D, A, B, X [i 2], 17, 0x242070dB);

B = ff (B, C, D, A, X [i 3], 22, 0xC1BDCEEE);

A = ff (A, B, C, D, X [i 4], 7, 0xF57c0FAF);

D = ff (D, A, B, C, X [i 5], 12, 0x4787C62A);

C = ff (C, D, A, B, X [i 6], 17, 0xA8304613);

B = ff (B, C, D, A, X [i 7], 22, 0xFD469501);

A = ff (A, B, C, D, X [i 8], 7, 0x698098d8);

D = ff (D, A, B, C, X [i 9], 12, 0x8B44F7AF);

C = ff (C, D, A, B, X [i 10], 17, 0xFFFFFF5BB1);

B = ff (B, C, D, A, X [i 11], 22, 0x895cd7be);

A = ff (A, B, C, D, X [i 12], 7, 0x6b901122); D = FF (D, A, B, C, X [i 13], 12, 0xFD987193);

C = ff (C, D, A, B, X [i 14], 17, 0xA679438E);

B = ff (B, C, D, A, X [i 15], 22, 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], 14, 0x265E5A51);

B = GG (B, C, D, A, X [i 0], 20, 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], 14, 0xD8A1E681);

B = GG (B, C, D, A, X [i 4], 20, 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], 14, 0xF4D50D87);

B = GG (B, C, D, A, X [i 8], 20, 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], 14, 0x676F02D9);

B = GG (B, C, D, A, X [i 12], 20, 0x8D2A4C8A);

A = HH (A, B, C, D, X [i 5], 4, 0xFFFFA3942);

D = HH (D, A, B, C, X [i 8], 11, 0x8771F681);

C = HH (C, D, A, B, X [i 11], 16, 0x6d9d6122);

B = HH (B, C, D, A, X [i 14], 23, 0xFDE5380C);

A = HH (A, B, C, D, X [i 1], 4, 0xA4Beea44);

D = HH (D, A, B, C, X [i 4], 11, 0x4 bdecfa9);

C = HH (C, D, A, B, X [i 7], 16, 0xF6bb4b60);

B = HH (B, C, D, A, X [i 10], 23, 0xBebfbc70);

A = HH (A, B, C, D, X [i 13], 4, 0x289B7EC6);

D = HH (D, A, B, C, X [i 0], 11, 0xEAA127FA);

C = HH (C, D, A, B, X [i 3], 16, 0xD4ef3085);

B = HH (B, C, D, A, X [i 6], 23, 0x04881D05);

A = HH (A, B, C, D, X [i 9], 4, 0xD9D4D039); D = HH (D, A, B, C, X [i 12], 11, 0xE6DB99E5);

C = HH (C, D, A, B, X [i 15], 16, 0x1fa27cf8);

B = HH (B, C, D, A, X [i 2], 23, 0xC4AC5665);

A = II (A, B, C, D, X [i 0], 6, 0xF4292244);

D = II (D, A, B, C, X [i 7], 10, 0x432AFF97);

C = II (C, D, A, B, X [i 14], 15, 0xAb9423A7);

B = II (B, C, D, A, X [i 5], 21, 0xFC93A039);

A = II (A, B, C, D, X [i 12], 6, 0x6555B59C3);

D = II (D, A, B, C, X [i 3], 10, 0x8F0ccc92);

C = II (C, D, A, B, X [i 10], 15, 0xffeff47d);

B = II (B, C, D, A, X [i 1], 21, 0x85845DD1);

A = II (A, B, C, D, X [i 8], 6, 0x6fa87e4f);

D = II (D, A, B, C, X [i 15], 10, 0xFE2CE6E0);

C = II (C, D, A, B, X [i 6], 15, 0xA3014314);

B = II (B, C, D, A, X [i 13], 21, 0x4e0811a1);

A = II (A, B, C, D, X [i 4], 6, 0xF7537E82);

D = II (D, A, B, C, X [i 11], 10, 0xBD3AF235);

C = II (C, D, A, B, X [i 2], 15, 0x2AD7D2BB);

B = II (B, C, D, A, X [i 9], 21, 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);

}

}

The above is just the specific implementation of the algorithm. When we truly trial, we often use the JDK to bring the algorithm:

Package myreuse.common.util;

Import sun.misc.base64encoder; import java.security.MPort Java.Security.NOSUCHALGORITHMEXCEPTION;

public abstract class MessageDigester {static MessageDigest SHA1; static MessageDigest MD5; static {try {SHA1 = MessageDigest.getInstance ( "SHA1"); MD5 = MessageDigest.getInstance ( "MD5");} catch (NoSuchAlgorithmException e) {e.printStackTrace ( }

} Public static Byte [] Sha1 (String MSG) {Return Sha1.digest (msg.getbytes ());} public static Byte [] MD5 (String MSG) {Return Md5.digest (msg.getbytes ());}

Private static base64encoder base64encoder = new base64encoder ();

Public static string hex (byte [] b) {stringbuffer result = new stringbuffer (b.Length); for (int i = 0; i

Public static string base64 (byte [] b) {string result = base64encoder.Encode (b); int i = result.indexof ('='); if (i> -1) Result = result.substring (0, i) Return Result;}

Public Static String Base64SHA1 (String MSG) {Return Base64 (SHA1 (MSG));}

Public Static String Base64MD5 (String MSG) {RETURN BASE64 (MD5 (MSG));}

Public Static String HexSha1 (STRING MSG) {Return HEX (SHA1 (MSG));

Public Static String Hexmd5 (String MSG) {Return HEX (MD5 (MSG));

}

Note if (i> -1) Result = result.substring (0, i); this sentence, JDK algorithm is when making Base64 encoding

Several one after the automatic complement, and there is a difference with our base64 implementation, so I cut out several pieces of it.

转载请注明原文地址:https://www.9cbs.com/read-103120.html

New Post(0)