📄 md5.java
字号:
for (i=start; i<end; i++) {
a[i] = val;
}
}
// In the C version, a call to MD5STEP is a macro-in-a-macro.
// In this Java version, we pass an Fcore object to represent the
// inner macro, and the MD5STEP() method performs the work of
// the outer macro. It would be good if this could all get
// inlined, but it would take a pretty aggressive compiler to
// inline away the dynamic method lookup made by MD5STEP to
// get to the Fcore.f function.
private abstract class Fcore {
abstract int f(int x, int y, int z);
}
private Fcore F1 = new Fcore() {
int f(int x, int y, int z) { return (z ^ (x & (y ^ z))); }};
private Fcore F2 = new Fcore() {
int f(int x, int y, int z) { return (y ^ (z & (x ^ y))); }};
private Fcore F3 = new Fcore() {
int f(int x, int y, int z) { return (x ^ y ^ z); }};
private Fcore F4 = new Fcore() {
int f(int x, int y, int z) { return (y ^ (x | ~z)); }};
private int MD5STEP(Fcore f, int w, int x, int y, int z, int data, int s) {
w += f.f(x, y, z) + data;
w = w<<s | w>>>(32-s);
w += x;
return w;
}
private void transform() {
/* load in[] byte array into an internal int array */
int i;
int[] inint = new int[16];
for (i=0; i<16; i++) {
inint[i] = GET_32BIT_LSB_FIRST(in, 4*i);
}
int a, b, c, d;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
a = MD5STEP(F1, a, b, c, d, inint[0] + 0xd76aa478, 7);
d = MD5STEP(F1, d, a, b, c, inint[1] + 0xe8c7b756, 12);
c = MD5STEP(F1, c, d, a, b, inint[2] + 0x242070db, 17);
b = MD5STEP(F1, b, c, d, a, inint[3] + 0xc1bdceee, 22);
a = MD5STEP(F1, a, b, c, d, inint[4] + 0xf57c0faf, 7);
d = MD5STEP(F1, d, a, b, c, inint[5] + 0x4787c62a, 12);
c = MD5STEP(F1, c, d, a, b, inint[6] + 0xa8304613, 17);
b = MD5STEP(F1, b, c, d, a, inint[7] + 0xfd469501, 22);
a = MD5STEP(F1, a, b, c, d, inint[8] + 0x698098d8, 7);
d = MD5STEP(F1, d, a, b, c, inint[9] + 0x8b44f7af, 12);
c = MD5STEP(F1, c, d, a, b, inint[10] + 0xffff5bb1, 17);
b = MD5STEP(F1, b, c, d, a, inint[11] + 0x895cd7be, 22);
a = MD5STEP(F1, a, b, c, d, inint[12] + 0x6b901122, 7);
d = MD5STEP(F1, d, a, b, c, inint[13] + 0xfd987193, 12);
c = MD5STEP(F1, c, d, a, b, inint[14] + 0xa679438e, 17);
b = MD5STEP(F1, b, c, d, a, inint[15] + 0x49b40821, 22);
a = MD5STEP(F2, a, b, c, d, inint[1] + 0xf61e2562, 5);
d = MD5STEP(F2, d, a, b, c, inint[6] + 0xc040b340, 9);
c = MD5STEP(F2, c, d, a, b, inint[11] + 0x265e5a51, 14);
b = MD5STEP(F2, b, c, d, a, inint[0] + 0xe9b6c7aa, 20);
a = MD5STEP(F2, a, b, c, d, inint[5] + 0xd62f105d, 5);
d = MD5STEP(F2, d, a, b, c, inint[10] + 0x02441453, 9);
c = MD5STEP(F2, c, d, a, b, inint[15] + 0xd8a1e681, 14);
b = MD5STEP(F2, b, c, d, a, inint[4] + 0xe7d3fbc8, 20);
a = MD5STEP(F2, a, b, c, d, inint[9] + 0x21e1cde6, 5);
d = MD5STEP(F2, d, a, b, c, inint[14] + 0xc33707d6, 9);
c = MD5STEP(F2, c, d, a, b, inint[3] + 0xf4d50d87, 14);
b = MD5STEP(F2, b, c, d, a, inint[8] + 0x455a14ed, 20);
a = MD5STEP(F2, a, b, c, d, inint[13] + 0xa9e3e905, 5);
d = MD5STEP(F2, d, a, b, c, inint[2] + 0xfcefa3f8, 9);
c = MD5STEP(F2, c, d, a, b, inint[7] + 0x676f02d9, 14);
b = MD5STEP(F2, b, c, d, a, inint[12] + 0x8d2a4c8a, 20);
a = MD5STEP(F3, a, b, c, d, inint[5] + 0xfffa3942, 4);
d = MD5STEP(F3, d, a, b, c, inint[8] + 0x8771f681, 11);
c = MD5STEP(F3, c, d, a, b, inint[11] + 0x6d9d6122, 16);
b = MD5STEP(F3, b, c, d, a, inint[14] + 0xfde5380c, 23);
a = MD5STEP(F3, a, b, c, d, inint[1] + 0xa4beea44, 4);
d = MD5STEP(F3, d, a, b, c, inint[4] + 0x4bdecfa9, 11);
c = MD5STEP(F3, c, d, a, b, inint[7] + 0xf6bb4b60, 16);
b = MD5STEP(F3, b, c, d, a, inint[10] + 0xbebfbc70, 23);
a = MD5STEP(F3, a, b, c, d, inint[13] + 0x289b7ec6, 4);
d = MD5STEP(F3, d, a, b, c, inint[0] + 0xeaa127fa, 11);
c = MD5STEP(F3, c, d, a, b, inint[3] + 0xd4ef3085, 16);
b = MD5STEP(F3, b, c, d, a, inint[6] + 0x04881d05, 23);
a = MD5STEP(F3, a, b, c, d, inint[9] + 0xd9d4d039, 4);
d = MD5STEP(F3, d, a, b, c, inint[12] + 0xe6db99e5, 11);
c = MD5STEP(F3, c, d, a, b, inint[15] + 0x1fa27cf8, 16);
b = MD5STEP(F3, b, c, d, a, inint[2] + 0xc4ac5665, 23);
a = MD5STEP(F4, a, b, c, d, inint[0] + 0xf4292244, 6);
d = MD5STEP(F4, d, a, b, c, inint[7] + 0x432aff97, 10);
c = MD5STEP(F4, c, d, a, b, inint[14] + 0xab9423a7, 15);
b = MD5STEP(F4, b, c, d, a, inint[5] + 0xfc93a039, 21);
a = MD5STEP(F4, a, b, c, d, inint[12] + 0x655b59c3, 6);
d = MD5STEP(F4, d, a, b, c, inint[3] + 0x8f0ccc92, 10);
c = MD5STEP(F4, c, d, a, b, inint[10] + 0xffeff47d, 15);
b = MD5STEP(F4, b, c, d, a, inint[1] + 0x85845dd1, 21);
a = MD5STEP(F4, a, b, c, d, inint[8] + 0x6fa87e4f, 6);
d = MD5STEP(F4, d, a, b, c, inint[15] + 0xfe2ce6e0, 10);
c = MD5STEP(F4, c, d, a, b, inint[6] + 0xa3014314, 15);
b = MD5STEP(F4, b, c, d, a, inint[13] + 0x4e0811a1, 21);
a = MD5STEP(F4, a, b, c, d, inint[4] + 0xf7537e82, 6);
d = MD5STEP(F4, d, a, b, c, inint[11] + 0xbd3af235, 10);
c = MD5STEP(F4, c, d, a, b, inint[2] + 0x2ad7d2bb, 15);
b = MD5STEP(F4, b, c, d, a, inint[9] + 0xeb86d391, 21);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
private int GET_32BIT_LSB_FIRST(byte[] b, int off) {
return
((int)(b[off+0]&0xff)) |
((int)(b[off+1]&0xff) << 8) |
((int)(b[off+2]&0xff) << 16) |
((int)(b[off+3]&0xff) << 24);
}
private void PUT_32BIT_LSB_FIRST(byte[] b, int off, int value) {
b[off+0] = (byte) (value & 0xff);
b[off+1] = (byte) ((value >> 8) & 0xff);
b[off+2] = (byte) ((value >> 16)& 0xff);
b[off+3] = (byte) ((value >> 24)& 0xff);
}
// These are debug routines I was using while trying to
// get this code to generate the same hashes as the C version.
// (IIRC, all the errors were due to the absence of unsigned
// ints in Java.)
/*
private void debugStatus(String m) {
System.out.println(m+":");
System.out.println("in: "+dumpBytes(in));
System.out.println("bits: "+bits);
System.out.println("buf: "
+Integer.toHexString(buf[0])+" "
+Integer.toHexString(buf[1])+" "
+Integer.toHexString(buf[2])+" "
+Integer.toHexString(buf[3]));
}
*/
private static String dumpBytes(byte[] bytes) {
int i;
StringBuffer sb = new StringBuffer();
for (i=0; i<bytes.length; i++) {
if (i%32 == 0 && i!=0) {
sb.append("\n");
}
String s = Integer.toHexString(bytes[i]);
if (s.length() < 2) {
s = "0"+s;
}
if (s.length() > 2) {
s = s.substring(s.length()-2);
}
sb.append(s);
}
return sb.toString();
}
/* public static byte[] HexStringToBytes(String str)
{
String strTmp;
if(str.length()%2 != 0)
{
strTmp = str.substring(0, str.length()-2);
str = strTmp+"0"+str.substring(str.length()-2);
}
byte[] b = new byte[str.length()/2];
for(int i = 0;i<str.length()/2;i++)
{
b[i] = Byte.parseByte(str.substring(2*i, 2*i+1), 16);
}
return b;
}*/
public static byte[] toBinArray( String hexStr ){
byte bArray[] = new byte[hexStr.length()/2];
for(int i=0; i<(hexStr.length()/2); i++){
byte firstNibble = Byte.parseByte(hexStr.substring(2*i,2*i+1),16); // [x,y)
byte secondNibble = Byte.parseByte(hexStr.substring(2*i+1,2*i+2),16);
int finalByte = (secondNibble) | (firstNibble << 4 ); // bit-operations only with numbers, not bytes.
bArray[i] = (byte) finalByte;
}
return bArray;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -