📄 md5outputstream.java
字号:
import java.io.IOException;
import java.io.OutputStream;
public class MD5OutputStream extends OutputStream
{
public String byte2hex(byte data[])
{
StringBuffer result = new StringBuffer();
for(int i = 0; i < data.length; i++)
{
char c = (char)(data[i] >> 4 & 0xf);
if(c > '\t')
c = (char)((c - 10) + 97);
else
c += '0';
result.append(c);
c = (char)(data[i] & 0xf);
if(c > '\t')
c = (char)((c - 10) + 97);
else
c += '0';
result.append(c);
}
return result.toString();
}
private OutputStream os_;
private static final int size = 16;
private int buf[];
private long i;
private byte in[];
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;
public MD5OutputStream()
{
os_ = null;
buf = new int[4];
in = new byte[64];
init();
}
public MD5OutputStream(OutputStream os)
{
os_ = null;
buf = new int[4];
in = new byte[64];
os_ = os;
init();
}
private static final int F(int x, int y, int z)
{
return x & y | ~x & z;
}
private static final int FF(int a, int b, int c, int d, int x, int s, int ac)
{
a += F(b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a += b;
return a;
}
private static final int G(int x, int y, int z)
{
return x & z | y & ~z;
}
private static final int GG(int a, int b, int c, int d, int x, int s, int ac)
{
a += G(b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a += b;
return a;
}
private static final int H(int x, int y, int z)
{
return x ^ y ^ z;
}
private static final int HH(int a, int b, int c, int d, int x, int s, int ac)
{
a += H(b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a += b;
return a;
}
private static final int I(int x, int y, int z)
{
return y ^ (x | ~z);
}
private static final int II(int a, int b, int c, int d, int x, int s, int ac)
{
a += I(b, c, d) + x + ac;
a = ROTATE_LEFT(a, s);
a += b;
return a;
}
private static final int ROTATE_LEFT(int x, int n)
{
return x << n | x >>> 32 - n;
}
private void Transform(int in[])
{
int a = buf[0];
int b = buf[1];
int c = buf[2];
int d = buf[3];
a = FF(a, b, c, d, in[0], 7, 0xd76aa478);
d = FF(d, a, b, c, in[1], 12, 0xe8c7b756);
c = FF(c, d, a, b, in[2], 17, 0x242070db);
b = FF(b, c, d, a, in[3], 22, 0xc1bdceee);
a = FF(a, b, c, d, in[4], 7, 0xf57c0faf);
d = FF(d, a, b, c, in[5], 12, 0x4787c62a);
c = FF(c, d, a, b, in[6], 17, 0xa8304613);
b = FF(b, c, d, a, in[7], 22, 0xfd469501);
a = FF(a, b, c, d, in[8], 7, 0x698098d8);
d = FF(d, a, b, c, in[9], 12, 0x8b44f7af);
c = FF(c, d, a, b, in[10], 17, -42063);
b = FF(b, c, d, a, in[11], 22, 0x895cd7be);
a = FF(a, b, c, d, in[12], 7, 0x6b901122);
d = FF(d, a, b, c, in[13], 12, 0xfd987193);
c = FF(c, d, a, b, in[14], 17, 0xa679438e);
b = FF(b, c, d, a, in[15], 22, 0x49b40821);
a = GG(a, b, c, d, in[1], 5, 0xf61e2562);
d = GG(d, a, b, c, in[6], 9, 0xc040b340);
c = GG(c, d, a, b, in[11], 14, 0x265e5a51);
b = GG(b, c, d, a, in[0], 20, 0xe9b6c7aa);
a = GG(a, b, c, d, in[5], 5, 0xd62f105d);
d = GG(d, a, b, c, in[10], 9, 0x2441453);
c = GG(c, d, a, b, in[15], 14, 0xd8a1e681);
b = GG(b, c, d, a, in[4], 20, 0xe7d3fbc8);
a = GG(a, b, c, d, in[9], 5, 0x21e1cde6);
d = GG(d, a, b, c, in[14], 9, 0xc33707d6);
c = GG(c, d, a, b, in[3], 14, 0xf4d50d87);
b = GG(b, c, d, a, in[8], 20, 0x455a14ed);
a = GG(a, b, c, d, in[13], 5, 0xa9e3e905);
d = GG(d, a, b, c, in[2], 9, 0xfcefa3f8);
c = GG(c, d, a, b, in[7], 14, 0x676f02d9);
b = GG(b, c, d, a, in[12], 20, 0x8d2a4c8a);
a = HH(a, b, c, d, in[5], 4, 0xfffa3942);
d = HH(d, a, b, c, in[8], 11, 0x8771f681);
c = HH(c, d, a, b, in[11], 16, 0x6d9d6122);
b = HH(b, c, d, a, in[14], 23, 0xfde5380c);
a = HH(a, b, c, d, in[1], 4, 0xa4beea44);
d = HH(d, a, b, c, in[4], 11, 0x4bdecfa9);
c = HH(c, d, a, b, in[7], 16, 0xf6bb4b60);
b = HH(b, c, d, a, in[10], 23, 0xbebfbc70);
a = HH(a, b, c, d, in[13], 4, 0x289b7ec6);
d = HH(d, a, b, c, in[0], 11, 0xeaa127fa);
c = HH(c, d, a, b, in[3], 16, 0xd4ef3085);
b = HH(b, c, d, a, in[6], 23, 0x4881d05);
a = HH(a, b, c, d, in[9], 4, 0xd9d4d039);
d = HH(d, a, b, c, in[12], 11, 0xe6db99e5);
c = HH(c, d, a, b, in[15], 16, 0x1fa27cf8);
b = HH(b, c, d, a, in[2], 23, 0xc4ac5665);
a = II(a, b, c, d, in[0], 6, 0xf4292244);
d = II(d, a, b, c, in[7], 10, 0x432aff97);
c = II(c, d, a, b, in[14], 15, 0xab9423a7);
b = II(b, c, d, a, in[5], 21, 0xfc93a039);
a = II(a, b, c, d, in[12], 6, 0x655b59c3);
d = II(d, a, b, c, in[3], 10, 0x8f0ccc92);
c = II(c, d, a, b, in[10], 15, 0xffeff47d);
b = II(b, c, d, a, in[1], 21, 0x85845dd1);
a = II(a, b, c, d, in[8], 6, 0x6fa87e4f);
d = II(d, a, b, c, in[15], 10, 0xfe2ce6e0);
c = II(c, d, a, b, in[6], 15, 0xa3014314);
b = II(b, c, d, a, in[13], 21, 0x4e0811a1);
a = II(a, b, c, d, in[4], 6, 0xf7537e82);
d = II(d, a, b, c, in[11], 10, 0xbd3af235);
c = II(c, d, a, b, in[2], 15, 0x2ad7d2bb);
b = II(b, c, d, a, in[9], 21, 0xeb86d391);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
public byte[] digest()
{
byte buffer[] = new byte[16];
finish();
System.arraycopy(in, 0, buffer, 0, 16);
init();
return buffer;
}
private void finish()
{
int in2[] = new int[16];
in2[14] = (int)(this.i & -1L);
in2[15] = (int)(this.i >>> 32 & -1L);
int mdi = (int)(this.i >> 3 & 63L);
int padLen = mdi >= 56 ? 120 - mdi : 56 - mdi;
update(PADDING, 0, padLen);
int i = 0;
for(int ii = 0; i < 14; ii += 4)
{
in2[i] = (in[ii + 3] & 0xff) << 24 | (in[ii + 2] & 0xff) << 16 | (in[ii + 1] & 0xff) << 8 | in[ii] & 0xff;
i++;
}
Transform(in2);
i = 0;
for(int ii = 0; i < 4; ii += 4)
{
in[ii] = (byte)(buf[i] & 0xff);
in[ii + 1] = (byte)(buf[i] >> 8 & 0xff);
in[ii + 2] = (byte)(buf[i] >> 16 & 0xff);
in[ii + 3] = (byte)(buf[i] >> 24 & 0xff);
i++;
}
}
public OutputStream getOutputStream()
{
return os_;
}
private void init()
{
i = 0L;
buf[0] = 0x67452301;
buf[1] = 0xefcdab89;
buf[2] = 0x98badcfe;
buf[3] = 0x10325476;
for(int l = 0; l < 64; l++)
in[l] = 0;
}
public void setOutputStream(OutputStream os)
{
os_ = os;
}
protected void update(byte data)
{
byte temp[] = {
data
};
update(temp, 0, temp.length);
}
public void update(byte buffer[], int start, int length)
{
int mdi = (int)(this.i >> 3 & 63L);
this.i += length << 3;
while(length-- != 0)
{
in[mdi++] = buffer[start++];
if(mdi == 64)
{
int in2[] = new int[16];
int i = 0;
for(int ii = 0; i < 16; ii += 4)
{
in2[i] = (in[ii + 3] & 0xff) << 24 | (in[ii + 2] & 0xff) << 16 | (in[ii + 1] & 0xff) << 8 | in[ii] & 0xff;
i++;
}
Transform(in2);
mdi = 0;
}
}
}
public void write(int b)
throws IOException
{
if(os_ != null)
os_.write(b);
update((byte)b);
}
public void write(byte b[], int off, int len)
throws IOException
{
if(os_ != null)
os_.write(b, off, len);
update(b, off, len);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -