-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRipemd160.java
327 lines (258 loc) · 9.26 KB
/
Ripemd160.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package bitcoin;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("all")
class RIPEMD_160 {
/**
* Message Digest Buffer
*/
private int A;
private int B;
private int C;
private int D;
private int E;
/**
* Shifts for left line
*/
private static final int[] S = {
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
};
/**
* Shifts for right line
*/
private static final int[] SS = {
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
};
/**
* Index of 32-bits word in left line
*/
private static final int[] X = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,
};
/**
* Index of 32-bits word in right line
*/
private static final int[] XX = {
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
};
/**
* This method return hash of message
*/
public String getHash(String input){
byte[] bytes = input.getBytes();
List<Integer> intList = appendPaddingBits(bytes);
intList = appendLength(intList, bytes.length * 8);
int[] array = convertListToArray(intList);
initMdBuffer();
array = processHash(array);
return getOutput(array);
}
/**
* Step 1. Append Padding Bits
*
* The message is "padded" (extended) so that its length (in bits) is
* congruent to 448, modulo 512. That is, the message is extended so
* that it is just 64 bits shy of being a multiple of 512 bits long.
* Padding is always performed, even if the length of the message is
* already congruent to 448, modulo 512.
*
* Padding is performed as follows: a single "1" bit is appended to the
* message, and then "0" bits are appended so that the length in bits of
* the padded message becomes congruent to 448, modulo 512. In all, at
* least one bit and at most 512 bits are appended.
*
*/
private List<Integer> appendPaddingBits(byte[] bytes){
List<Integer> intList = new ArrayList<>();
for (byte aByte : bytes) {
intList.add((int) aByte);
}
int one = 128;
int zero = 0;
intList.add(one);
while (intList.size() % 64 != 56){
intList.add(zero);
}
return intList;
}
/**
* Step 2. Append Length
*
* A 64-bit representation of b (the length of the message before the
* padding bits were added) is appended to the result of the previous
* step. In the unlikely event that b is greater than 2^64, then only
* the low-order 64 bits of b are used. (These bits are appended as two
* 32-bit words and appended low-order word first in accordance with the
* previous conventions.)
*
* At this point the resulting message (after padding with bits and with
* b) has a length that is an exact multiple of 512 bits. Equivalently,
* this message has a length that is an exact multiple of 16 (32-bit)
* words. Let M[0 ... N-1] denote the words of the resulting message,
* where N is a multiple of 16.
*
*/
private List<Integer> appendLength(List<Integer> intList, int length){
List<Integer> integers = new ArrayList<>();
for (int i = 0; i < intList.size(); i += 4) {
integers.add(intList.get(i) | intList.get(i + 1) << 8 | intList.get(i + 2) << 16 | intList.get(i + 3) << 24);
}
integers.add(length);
integers.add(0);
return integers;
}
/**
* Step 3. Initialize Message Digest Buffer
*
* A four-word buffer (A,B,C,D,C) is used to compute the message digest.
* Here each of A, B, C, D, C is a 32-bit register. These registers are
* initialized to the following values in hexadecimal, low-order bytes
* first):
*
* word A: 01 23 45 67
* word B: 89 ab cd ef
* word C: fe dc ba 98
* word D: 76 54 32 10
* word E: f0 e1 d2 c3
*
*/
private void initMdBuffer(){
A = 0x67452301;
B = 0xEFCDAB89;
C = 0x98BADCFE;
D = 0x10325476;
E = 0xC3D2E1F0;
}
/**
* Step 4. Process Message in 16-Word Blocks
*/
private int[] processHash(int[] array){
// paramseters for left line
int AL, BL, CL, DL, EL;
// paramseters for right line
int AR, BR, CR, DR, ER;
int[] x = new int[16];
int length = array.length;
for(int i = 0; i < length / 16; i++){
// Copy block i into X
System.arraycopy(array, i * 16, x, 0, 16);
// init parameters
AL = AR = A;
BL = BR = B;
CL = CR = C;
DL = DR = D;
EL = ER = E;
for(int j = 0; j <= 79; j++){
int T = FF(j, AL, BL, CL, DL, x[X[j]], S[j]) + EL;
AL = EL;
EL = DL;
DL = rol(CL, 10);
CL = BL;
BL = T;
T = FFF(79 - j, AR, BR, CR, DR, x[XX[j]], SS[j]) + ER;
AR = ER;
ER = DR;
DR = rol(CR, 10);
CR = BR;
BR = T;
}
// Converting result
int T = B + CL + DR;
B = C + DL + ER;
C = D + EL + AR;
D = E + AL + BR;
E = A + BL + CR;
A = T;
}
return new int[]{A, B, C, D, E};
}
/**
* Boolean functions for rounds
*/
private int F(int x, int y, int z, int i){
if(i >= 0 && i <= 15){
return ((x) ^ (y) ^ (z));
} else if(i >= 16 && i <= 31){
return (((x) & (y)) | (~(x) & (z)));
} else if (i >= 32 && i <= 47){
return (((x) | ~(y)) ^ (z));
} else if (i >= 48 && i <= 63){
return (((x) & (z)) | ((y) & ~(z)));
} else {
return ((x) ^ ((y) | ~(z)));
}
}
/**
* Rounds for left line
*/
private int FF(int i, int a, int b, int c, int d, int x, int s){
a += F(b, c, d, i) + (x);
if(i >= 16 && i <= 31){
a += 0x5A827999;
} else if (i >= 32 && i <= 47){
a += 0x6ED9EBA1;
} else if (i >= 48 && i <= 63){
a += 0x8F1BBCDC;
} else if (i >= 64 && i <= 79){
a += 0xA953FD4E;
}
return rol(a , s);
}
/**
* Rounds for right line
*/
private int FFF(int i, int a, int b, int c, int d, int x, int s){
a += F(b, c, d, i) + (x);
if(i >= 16 && i <= 31){
a += 0x7A6D76E9;
} else if (i >= 32 && i <= 47){
a += 0x6D703EF3;
} else if (i >= 48 && i <= 63){
a += 0x5C4DD124;
} else if (i >= 64 && i <= 79){
a += 0x50A28BE6;
}
return rol(a , s);
}
private int rol(int x, int y){
return ((x)<<(y))|((x)>>>(32-y));
}
/**
* Method for converting A, B, C, D, E params to hex for getting hash
*/
private String getOutput(int[] array){
StringBuilder hexString = new StringBuilder();
for(int i = 0; i < 5; i++){
String h = Integer.toHexString(changeEndianness(array[i]));
hexString.append(h);
}
return hexString.toString();
}
private int changeEndianness(int x){
return ((x & 0xFF) << 24) | ((x & 0xFF00) << 8) | ((x & 0xFF0000) >>> 8) | ((x & 0xFF000000) >>> 24);
}
public static int[] convertListToArray(List<Integer> integerList){
int[] array = new int[integerList.size()];
for(int i = 0; i < integerList.size(); i++){
array[i] = integerList.get(i);
}
return array;
}
}