-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMD5Example.java
More file actions
104 lines (92 loc) · 3.5 KB
/
MD5Example.java
File metadata and controls
104 lines (92 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class MD5Example {
public static void main(String[] args) {
String text = "Hello, world!";
String md5Hash = calculateMD5(text);
System.out.println("MD5 hash of '" + text + "': " + md5Hash);
}
public static String calculateMD5(String text) {
// Step 1: Append padding bits
int origLength = text.length();
text += (char) 0x80; // Append single '1' bit
while ((text.length() % 64) != 56) {
text += (char) 0x00; // Append '0' bits
}
// Step 2: Append original length in bits
long lengthInBits = origLength * 8;
for (int i = 0; i < 8; i++) {
text += (char) (lengthInBits & 0xFF);
lengthInBits >>= 8;
}
// Step 3: Initialize variables
int[] s = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
int[] K = new int[64];
for (int i = 0; i < 64; i++) {
K[i] = (int) (Math.floor(Math.abs(Math.sin(i + 1)) * (1L << 32)));
}
int a0 = 0x67452301;
int b0 = 0xEFCDAB89;
int c0 = 0x98BADCFE;
int d0 = 0x10325476;
// Step 4: Process message in 512-bit chunks
int numOfChunks = text.length() / 64;
int[] chunks = new int[numOfChunks * 16];
for (int i = 0; i < numOfChunks; i++) {
for (int j = 0; j < 16; j++) {
chunks[i * 16 + j] = (text.charAt(i * 64 + j * 4) & 0xFF) |
((text.charAt(i * 64 + j * 4 + 1) & 0xFF) << 8) |
((text.charAt(i * 64 + j * 4 + 2) & 0xFF) << 16) |
((text.charAt(i * 64 + j * 4 + 3) & 0xFF) << 24);
}
}
// Remaining steps are same as before...
// Step 5: Initialize hash value for this chunk
int A = a0;
int B = b0;
int C = c0;
int D = d0;
// Step 6: Main loop
for (int i = 0; i < numOfChunks; i++) {
int[] M = new int[16];
System.arraycopy(chunks, i * 16, M, 0, 16);
int AA = A;
int BB = B;
int CC = C;
int DD = D;
for (int j = 0; j < 64; j++) {
int F, g;
if (j < 16) {
F = (BB & CC) | (~BB & DD);
g = j;
} else if (j < 32) {
F = (DD & BB) | (~DD & CC);
g = (5 * j + 1) % 16;
} else if (j < 48) {
F = BB ^ CC ^ DD;
g = (3 * j + 5) % 16;
} else {
F = CC ^ (BB | ~DD);
g = (7 * j) % 16;
}
F = F + A + K[j] + M[g];
A = D;
D = C;
C = B;
B = B + ((F << s[j]) | (F >>> (32 - s[j])));
}
A = A + AA;
B = B + BB;
C = C + CC;
D = D + DD;
}
// Step 7: Concatenate A, B, C, D
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(A));
sb.append(Integer.toHexString(B));
sb.append(Integer.toHexString(C));
sb.append(Integer.toHexString(D));
return sb.toString();
}
}