MD5 Algorithm JAVA_BEAN
MD5 Algorithm JAVA_BEAN
Package org.waityou.news.util;
Import java.lang.reflect .*;
/************************************************* SUMMARY
* Md5 class implements RSA Data Security, Inc. Submitted to the IETF RFC1321 the MD5 message-digest algorithm.
************************************************** ****************************/
(Public class MD5
/ *
* Below these S11-S44 is actually a 4 x 4 matrix, in the realization of the original C # define is used to achieve, and here they realize become static
* The final is that the read-only, cut in the same space, a process shared among multiple Instance
* /
Static final int S11 = 7;
Static final int S12 = 12;
Static final int S13 = 17;
Static final int S14 = 22;
Static final int S21 = 5;
Static final int S22 = 9;
Static final int S23 = 14;
Static final int S24 = 20;
Static final int S31 = 4;
Static final int S32 = 11;
Static final int S33 = 16;
Static final int S34 = 23;
Static final int S41 = 6;
Static final int S42 = 10;
Static final int S43 = 15;
Static final int S44 = 21;
Static final byte [] = (PADDING -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0);
/ *
* Below the three members of MD5 is used in the computation of the three core data, in the realization of the original C, which is defined by the structure of the MD5_CTX
*
* /
Private long state [] = new long [4]; / / state (ABCD)
Private long [] = new long count [2] / / number of bits, modulo 2 ^ 64 (lsb
/ / First)
Private byte buffer [] = new byte [64]; / / input buffer
/ *
* DigestHexStr MD5 is the only member of the public, is the latest of the results of a 16-band ASCII said.
* /
Public String digestHexStr;
/ *
* Digest, is the latest of the results of a 2-band internal said that the 128-bit MD5 value.
* /
Digest private byte [] = new byte [16];
/ *
* GetMD5ofStr category is the most important public MD5, you want to import parameter is a string transform MD5
* End transformation is the return of the results of this result is achieved digestHexStr from members of the public.
* /
Public String getMD5ofStr (String inbuf) (
Md5Init ();
Md5Update (inbuf.getBytes (), inbuf.length ());
Md5Final ();
DigestHexStr = "";
For (int i = 0; i <16; i + +) (
DigestHexStr + = byteHEX (digest [i]);
)
Return digestHexStr;
)
/ / This is the standard MD5 this class constructor is JavaBean call for a public parameters of the structure and did not function
Public MD5 () (
Md5Init ();
Return;
)
/ * Initialize md5Init is a function of the core initialization variables into standard magic number * /
Private void md5Init () (
Count [0] = 0L;
Count [1] = 0L;
/ / / * Load magic initialization constants.
State [0] = 0×67452301L;
State [1] = 0xefcdab89L;
State [2] = 0×98badcfeL;
State [3] = 0×10325476L;
Return;
)
/ *
* F, G, H and I are four basic MD5 function, in the original C MD5 realize, because they are
* Simple-bit computing, the efficiency may be considered for the achievement of their Acer has become the java, we put them into private achieve, maintain the original name of the C.
* /
Private long F (long x, long y, long z) (
Return (x & y) | ((~ x) & z);
)
Private long G (long x, long y, long z) (
Return (x & z) | (y & (~ z));
)
Private long H (long x, long y, long z) (
Return x ^ y ^ z;
)
Private long I (long x, long y, long z) (
Return y ^ (x | (~ z));
)
/ *
* FF, GG, HH, and II will be called F, G, H, I conducted closer to transform FF, GG, HH, and II transformations for
* Rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* Recomputation.
* /
Private long FF (long a, long b, long c, long d, long x, long s, long ac) (
F = a + (b, c, d) + x + ac;
A = ((int) a <<s) | ((int) a>>> (32 - s));
= A + b;
Return a;
)
Private long GG (long a, long b, long c, long d, long x, long s, long ac) (
G = a + (b, c, d) + x + ac;
A = ((int) a <<s) | ((int) a>>> (32 - s));
= A + b;
Return a;
)
Private long HH (long a, long b, long c, long d, long x, long s, long ac) (
H = a + (b, c, d) + x + ac;
A = ((int) a <<s) | ((int) a>>> (32 - s));
= A + b;
Return a;
)
Private long II (long a, long b, long c, long d, long x, long s, long ac) (
I = a + (b, c, d) + x + ac;
A = ((int) a <<s) | ((int) a>>> (32 - s));
= A + b;
Return a;
)
/ *
* MD5 is the main md5Update calculation process, inbuf is to transform strings of bytes, inputlen is the length of this
* Function from getMD5ofStr call, call call md5init needed before, it's designed private
* /
Private void md5Update (inbuf byte [], int inputLen) (
Int i, index, partLen;
Byte [] = new byte block [64];
Index = (int) (count [0]>>> 3) & 0×3F;
/ / / * Update number of bits * /
If ((count + [0] = (inputLen <<3)) <(inputLen <<3))
Count [1] + +;
Count [1] + = (inputLen>>> 29);
PartLen = 64 - index;
/ / Transform as many times as possible.
If (inputLen> = partLen) (
Md5Memcpy (buffer, inbuf, index, 0, partLen);
Md5Transform (buffer);
For (i = partLen; i + 63 <inputLen; i + = 64) (
Md5Memcpy (block, inbuf, 0, i, 64);
Md5Transform (block);
)
Index = 0;
) Else
I = 0;
/ / / * * Buffer remaining input /
Md5Memcpy (buffer, inbuf, index, i, inputLen - i);
)
/ *
* Md5Final organize and fill in output
* /
Private void md5Final () (
Byte [] bits = new byte [8];
Int index, padLen;
/ / / * Save number of bits * /
Encode (bits, count, 8);
/ / / * Pad out to 56 mod 64.
Index = (int) (count [0]>>> 3) & 0×3f;
PadLen = (index <56) - (56 - index): (120 - index);
Md5Update (PADDING, padLen);
/ / / * Append length (before padding) * /
Md5Update (bits, 8);
/ / / * Store state in digest * /
Encode (digest, state, 16);
)
/ *
* Md5Memcpy the internal use of a byte array block copy function, input from the beginning of the len inpos length
* Bytes copied to the output location of the beginning of outpos
* /
Private void md5Memcpy (byte [] output, input byte [], int outpos, int inpos,
Int len) (
Int i;
For (i = 0; i <len; i + +)
Output [outpos + i] = input [inpos + i];
)
/ *
* Md5Transform is MD5 core transformation process, md5Update call, block is the original byte block
* /
Private void md5Transform (byte block []) (
Long a state [0] = b = state [1], c = state [2], d = state [3];
Long x = [] [16] new long;
Decode (x, block, 64);
/ * Round 1 * /
A = FF (a, b, c, d, x [0], S11, 0xd76aa478L) / * 1 * /
D = FF (d, a, b, c, x [1], S12, 0xe8c7b756L) / * 2 * /
C = FF (c, d, a, b, x [2], S13, 0×242070dbL) / * 3 * /
B = FF (b, c, d, a, x [3], S14, 0xc1bdceeeL) / * 4 * /
A = FF (a, b, c, d, x [4], S11, 0xf57c0fafL) / * 5 * /
D = FF (d, a, b, c, x [5], S12, 0×4787c62aL) / * 6 * /
C = FF (c, d, a, b, x [6], S13, 0xa8304613L) / * * /
B = FF (b, c, d, a, x [7], S14, 0xfd469501L) / * 8 * /
A = FF (a, b, c, d, x [8], S11, 0×698098d8L) / * 9 * /
D = FF (d, a, b, c, x [9], S12, 0×8b44f7afL) / * 10 * /
C = FF (c, d, a, b, x [10], S13, 0xffff5bb1L) / * 11 * /
B = FF (b, c, d, a, x [11], S14, 0×895cd7beL) / * 12 * /
A = FF (a, b, c, d, x [12], S11, 0×6b901122L) / * 13 * /
D = FF (d, a, b, c, x [13], S12, 0xfd987193L) / * 14 * /
C = FF (c, d, a, b, x [14], S13, 0xa679438eL) / * 15 * /
B = FF (b, c, d, a, x [15], S14, 0×49b40821L) / * 16 * /
/ * Round 2 * /
A = GG (a, b, c, d, x [1], S21, 0xf61e2562L) / * 17 * /
D = GG (d, a, b, c, x [6], S22, 0xc040b340L) / * 18 * /
C = GG (c, d, a, b, x [11], S23, 0×265e5a51L) / * 19 * /
B = GG (b, c, d, a, x [0], S24, 0xe9b6c7aaL) / * 20 * /
A = GG (a, b, c, d, x [5], S21, 0xd62f105dL) / * 21 * /
D = GG (d, a, b, c, x [10], S22, 0×2441453L) / * 22 * /
C = GG (c, d, a, b, x [15], S23, 0xd8a1e681L) / * 23 * /
B = GG (b, c, d, a, x [4], S24, 0xe7d3fbc8L) / * 24 * /
A = GG (a, b, c, d, x [9], S21, 0×21e1cde6L) / * 25 * /
D = GG (d, a, b, c, x [14], S22, 0xc33707d6L) / * 26 * /
C = GG (c, d, a, b, x [3], S23, 0xf4d50d87L) / * 27 * /
B = GG (b, c, d, a, x [8], S24, 0×455a14edL) / * 28 * /
A = GG (a, b, c, d, x [13], S21, 0xa9e3e905L) / * 29 * /
D = GG (d, a, b, c, x [2], S22, 0xfcefa3f8L) / * 30 * /
C = GG (c, d, a, b, x [7], S23, 0×676f02d9L) / * 31 * /
B = GG (b, c, d, a, x [12], S24, 0×8d2a4c8aL) / * 32 * /
/ * Round 3 * /
A = HH (a, b, c, d, x [5], S31, 0xfffa3942L) / * 33 * /
D = HH (d, a, b, c, x [8], S32, 0×8771f681L) / * 34 * /
C = HH (c, d, a, b, x [11], S33, 0×6d9d6122L) / * 35 * /
B = HH (b, c, d, a, x [14], S34, 0xfde5380cL) / * 36 * /
A = HH (a, b, c, d, x [1], S31, 0xa4beea44L) / * 37 * /
D = HH (d, a, b, c, x [4], S32, 0×4bdecfa9L) / * 38 * /
C = HH (c, d, a, b, x [7], S33, 0xf6bb4b60L) / * 39 * /
B = HH (b, c, d, a, x [10], S34, 0xbebfbc70L) / * 40 * /
A = HH (a, b, c, d, x [13], S31, 0×289b7ec6L) / * 41 * /
D = HH (d, a, b, c, x [0], S32, 0xeaa127faL) / * 42 * /
C = HH (c, d, a, b, x [3], S33, 0xd4ef3085L) / * 43 * /
B = HH (b, c, d, a, x [6], S34, 0×4881d05L) / * 44 * /
A = HH (a, b, c, d, x [9], S31, 0xd9d4d039L) / * 45 * /
D = HH (d, a, b, c, x [12], S32, 0xe6db99e5L) / * 46 * /
C = HH (c, d, a, b, x [15], S33, 0×1fa27cf8L) / * 47 * /
B = HH (b, c, d, a, x [2], S34, 0xc4ac5665L) / * 48 * /
/ * Round 4 * /
A = II (a, b, c, d, x [0], S41, 0xf4292244L) / * 49 * /
D = II (d, a, b, c, x [7], S42, 0×432aff97L) / * 50 * /
C = II (c, d, a, b, x [14], S43, 0xab9423a7L) / * 51 * /
B = II (b, c, d, a, x [5], S44, 0xfc93a039L) / * 52 * /
A = II (a, b, c, d, x [12], S41, 0×655b59c3L) / * 53 * /
D = II (d, a, b, c, x [3], S42, 0×8f0ccc92L) / * 54 * /
C = II (c, d, a, b, x [10], S43, 0xffeff47dL) / * 55 * /
B = II (b, c, d, a, x [1], S44, 0×85845dd1L) / * 56 * /
A = II (a, b, c, d, x [8], S41, 0×6fa87e4fL) / * 57 * /
D = II (d, a, b, c, x [15], S42, 0xfe2ce6e0L) / * 58 * /
C = II (c, d, a, b, x [6], S43, 0xa3014314L) / * 59 * /
B = II (b, c, d, a, x [13], S44, 0×4e0811a1L) / * 60 * /
A = II (a, b, c, d, x [4], S41, 0xf7537e82L) / * 61 * /
D = II (d, a, b, c, x [11], S42, 0xbd3af235L) / * 62 * /
C = II (c, d, a, b, x [2], S43, 0×2ad7d2bbL) / * 63 * /
B = II (b, c, d, a, x [9], S44, 0xeb86d391L) / * 64 * /
State [0] = a +;
State [1] + = b;
State [2] + = c;
State [3] + = d;
)
/ *
* Encode the long array in order demolition of a byte array, because of the long java type 64-bit, 32-bit low only demolished to accommodate the use of realization of the original C
* /
Private void Encode (byte [] output, long input [], int len) (
Int i, j;
For (i = 0, j = 0, j <len; i + +, j + = 4) (
Output [j] = (byte) (input [i] & 0xffL);
Output [j + 1] = (byte) ((input [i]>>>
& 0xffL);
Output [j + 2] = (byte) ((input [i]>>> 16) & 0xffL);
Output [j + 3] = (byte) ((input [i]>>> 24) & 0xffL);
)
)
/ *
* Decode the byte array in order of a long array, because of the long type java is 64 bit,
* Only low of 32 bit, 32-bit high-cleared to adapt to the use of achieving the original C
* /
Private void Decode (long [] output, input byte [], int len) (
Int i, j;
For (i = 0, j = 0, j <len; i + +, j + = 4)
Output [i] = b2iu (input [j]) | (b2iu (input [j + 1]) <<8)
| (B2iu (input [j + 2]) <<16) | (b2iu (input [j + 3]) <<24);
Return;
)
/ *
* B2iu is, I wrote a byte to not consider positive and negative, in accordance with the principles of the "l-bit" procedures, not because of java unsigned computing
* /
Public static long b2iu (byte b) (
Return b <0? B & 0×7F + 128: b;
)
/ *
* ByteHEX (), used to a few types of byte hexadecimal convert the ASCII representation,
* Because the java byte in the toString unable to achieve this, we have not in the C language sprintf (outbuf, "% 02X", ib)
* /
Public static String byteHEX (byte ib) (
Digit char [] = ('0 ','1','2 ','3','4 'and'5','6 ','7','8 ','9', 'A',
'B', 'C', 'D', 'E', 'F');
Ob char [] = new char [2];
Ob Digit [0] = [(ib>>> 4) & 0X0F];
Ob Digit [1] = [ib & 0X0F];
String s = new String (ob);
Return s;
)
Public static void main (String args []) (
MD5 m = new MD5 ();
If (Array.getLength (args) == 0) (file: / / If there are no parameters, the implementation of standards Test Suite
System.out.println ( "MD5 Test suite:");
System.out.println ( "MD5 (\" 850817 \ "):" + m.getMD5ofStr ( "850817"));
System.out.println ( "MD5 (\" \ "):" + m.getMD5ofStr (""));
System.out.println ( "MD5 (\" a \ "):" + m.getMD5ofStr ( "a"));
System.out.println ( "MD5 (\" abc \ "):" + m.getMD5ofStr ( "abc"));
System.out.println ( "MD5 (\" message digest \ "):"
+ M.getMD5ofStr ( "message digest"));
System.out.println ( "MD5 (\" abcdefghijklmnopqrstuvwxyz \ "):"
+ M.getMD5ofStr ( "abcdefghijklmnopqrstuvwxyz"));
System.out
. Println ( "MD5 (\" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 \ "):"
+ M
. GetMD5ofStr ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
) Else
System.out
. Println ( "MD5 (" + + args [0] ") =" + m.getMD5ofStr (args [0]));
)
)
Posted on 2005-12-31 16:28 ZengChang Reading (62) Comments (1) edit their collections quoted Category: Learning Notes






