MD5摘要算法源码



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值