public class MD5 {
private 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
};
private static final int S11 = 7;
private static final int S12 = 12;
private static final int S13 = 17;
private static final int S14 = 22;
private static final int S21 = 5;
private static final int S22 = 9;
private static final int S23 = 14;
private static final int S24 = 20;
private static final int S31 = 4;
private static final int S32 = 11;
private static final int S33 = 16;
private static final int S34 = 23;
private static final int S41 = 6;
private static final int S42 = 10;
private static final int S43 = 15;
private static final int S44 = 21;
private long[] state = new long[4]; // state(ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64
private byte[] buffer = new byte[64]; // input buffer
private String digestHexStr;
private byte[] digest = new byte[16];
public MD5() {
md5Init();
return;
}
public String getMD5ofString(byte[] inbuf, int buflen) {
getMD5ofByte(inbuf, buflen);
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
public byte[] getMD5ofByte(byte[] inbuf, int buflen) {
md5Init();
md5Update(inbuf, buflen);
md5Final();
return digest;
}
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
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));
}
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += (F(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) {
a += (G(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) {
a += (H(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) {
a += (I(b, c, d) + x + ac);
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private void md5Update(byte[] inbuf, int inputLen) {
int i;
int index;
int partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// 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);
}
private void md5Final() {
byte[] bits = new byte[8];
MD5摘要算法源码
最新推荐文章于 2024-03-21 10:45:38 发布