-
Notifications
You must be signed in to change notification settings - Fork 17
/
HashRipeMD128.pas
284 lines (257 loc) · 14.1 KB
/
HashRipeMD128.pas
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
{
Hash functions
Copyright (c) 1999-2002 David Barton
Copyright (C) 2007, Ascher Stefan. All rights reserved.
}
{
Reimplementation of the hash functions from DCPcrypt
<http://www.cityinthesky.co.uk/cryptography.html> by David Barton
}
unit HashRipeMD128;
interface
uses
SysUtils, Classes, Hash;
type
THashRipeMD128 = class(THash)
private
procedure Compress;
protected
fLenHi, fLenLo: Cardinal;
fIndex: Cardinal;
fCurHash: array[0..3] of Cardinal;
fHashBuff: array[0..63] of byte;
public
procedure Init; override;
procedure Update(const Buffer; Size: Cardinal); override;
procedure Final(var Digest: array of Byte); override;
function SelfTest: boolean; override;
class function GetSize: integer; override;
procedure Burn; override;
end;
THashRipeMD128Class = class of THashRipeMD128;
implementation
{$R-}{$Q-}
function THashRipeMD128.SelfTest: boolean;
const
Test1Out: array[0..15] of byte=
($86,$be,$7a,$fa,$33,$9d,$0f,$c7,$cf,$c7,$85,$e7,$2f,$57,$8d,$33);
Test2Out: array[0..15] of byte=
($fd,$2a,$a6,$07,$f7,$1d,$c8,$f5,$10,$71,$49,$22,$b3,$71,$83,$4e);
var
TestOut: array[0..15] of byte;
begin
Init;
UpdateStr('a');
Final(TestOut);
Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
Init;
UpdateStr('abcdefghijklmnopqrstuvwxyz');
Final(TestOut);
Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
end;
procedure THashRipeMD128.Compress;
var
X: array[0..15] of Cardinal;
a, aa, b, bb, c, cc, d, dd, t: Cardinal;
begin
Move(fHashBuff,X,Sizeof(X));
a:= fCurHash[0]; aa:= a;
b:= fCurHash[1]; bb:= b;
c:= fCurHash[2]; cc:= c;
d:= fCurHash[3]; dd:= d;
t:= a + (b xor c xor d) + X[ 0]; a:= (t shl 11) or (t shr (32-11));
t:= d + (a xor b xor c) + X[ 1]; d:= (t shl 14) or (t shr (32-14));
t:= c + (d xor a xor b) + X[ 2]; c:= (t shl 15) or (t shr (32-15));
t:= b + (c xor d xor a) + X[ 3]; b:= (t shl 12) or (t shr (32-12));
t:= a + (b xor c xor d) + X[ 4]; a:= (t shl 5) or (t shr (32-5));
t:= d + (a xor b xor c) + X[ 5]; d:= (t shl 8) or (t shr (32-8));
t:= c + (d xor a xor b) + X[ 6]; c:= (t shl 7) or (t shr (32-7));
t:= b + (c xor d xor a) + X[ 7]; b:= (t shl 9) or (t shr (32-9));
t:= a + (b xor c xor d) + X[ 8]; a:= (t shl 11) or (t shr (32-11));
t:= d + (a xor b xor c) + X[ 9]; d:= (t shl 13) or (t shr (32-13));
t:= c + (d xor a xor b) + X[10]; c:= (t shl 14) or (t shr (32-14));
t:= b + (c xor d xor a) + X[11]; b:= (t shl 15) or (t shr (32-15));
t:= a + (b xor c xor d) + X[12]; a:= (t shl 6) or (t shr (32-6));
t:= d + (a xor b xor c) + X[13]; d:= (t shl 7) or (t shr (32-7));
t:= c + (d xor a xor b) + X[14]; c:= (t shl 9) or (t shr (32-9));
t:= b + (c xor d xor a) + X[15]; b:= (t shl 8) or (t shr (32-8));
t:= a + ((b and c) or (not b and d)) + X[ 7]+$5A827999; a:= (t shl 7) or (t shr (32-7));
t:= d + ((a and b) or (not a and c)) + X[ 4]+$5A827999; d:= (t shl 6) or (t shr (32-6));
t:= c + ((d and a) or (not d and b)) + X[13]+$5A827999; c:= (t shl 8) or (t shr (32-8));
t:= b + ((c and d) or (not c and a)) + X[ 1]+$5A827999; b:= (t shl 13) or (t shr (32-13));
t:= a + ((b and c) or (not b and d)) + X[10]+$5A827999; a:= (t shl 11) or (t shr (32-11));
t:= d + ((a and b) or (not a and c)) + X[ 6]+$5A827999; d:= (t shl 9) or (t shr (32-9));
t:= c + ((d and a) or (not d and b)) + X[15]+$5A827999; c:= (t shl 7) or (t shr (32-7));
t:= b + ((c and d) or (not c and a)) + X[ 3]+$5A827999; b:= (t shl 15) or (t shr (32-15));
t:= a + ((b and c) or (not b and d)) + X[12]+$5A827999; a:= (t shl 7) or (t shr (32-7));
t:= d + ((a and b) or (not a and c)) + X[ 0]+$5A827999; d:= (t shl 12) or (t shr (32-12));
t:= c + ((d and a) or (not d and b)) + X[ 9]+$5A827999; c:= (t shl 15) or (t shr (32-15));
t:= b + ((c and d) or (not c and a)) + X[ 5]+$5A827999; b:= (t shl 9) or (t shr (32-9));
t:= a + ((b and c) or (not b and d)) + X[ 2]+$5A827999; a:= (t shl 11) or (t shr (32-11));
t:= d + ((a and b) or (not a and c)) + X[14]+$5A827999; d:= (t shl 7) or (t shr (32-7));
t:= c + ((d and a) or (not d and b)) + X[11]+$5A827999; c:= (t shl 13) or (t shr (32-13));
t:= b + ((c and d) or (not c and a)) + X[ 8]+$5A827999; b:= (t shl 12) or (t shr (32-12));
t:= a + ((b or not c) xor d) + X[ 3]+$6ED9EBA1; a:= (t shl 11) or (t shr (32-11));
t:= d + ((a or not b) xor c) + X[10]+$6ED9EBA1; d:= (t shl 13) or (t shr (32-13));
t:= c + ((d or not a) xor b) + X[14]+$6ED9EBA1; c:= (t shl 6) or (t shr (32-6));
t:= b + ((c or not d) xor a) + X[ 4]+$6ED9EBA1; b:= (t shl 7) or (t shr (32-7));
t:= a + ((b or not c) xor d) + X[ 9]+$6ED9EBA1; a:= (t shl 14) or (t shr (32-14));
t:= d + ((a or not b) xor c) + X[15]+$6ED9EBA1; d:= (t shl 9) or (t shr (32-9));
t:= c + ((d or not a) xor b) + X[ 8]+$6ED9EBA1; c:= (t shl 13) or (t shr (32-13));
t:= b + ((c or not d) xor a) + X[ 1]+$6ED9EBA1; b:= (t shl 15) or (t shr (32-15));
t:= a + ((b or not c) xor d) + X[ 2]+$6ED9EBA1; a:= (t shl 14) or (t shr (32-14));
t:= d + ((a or not b) xor c) + X[ 7]+$6ED9EBA1; d:= (t shl 8) or (t shr (32-8));
t:= c + ((d or not a) xor b) + X[ 0]+$6ED9EBA1; c:= (t shl 13) or (t shr (32-13));
t:= b + ((c or not d) xor a) + X[ 6]+$6ED9EBA1; b:= (t shl 6) or (t shr (32-6));
t:= a + ((b or not c) xor d) + X[13]+$6ED9EBA1; a:= (t shl 5) or (t shr (32-5));
t:= d + ((a or not b) xor c) + X[11]+$6ED9EBA1; d:= (t shl 12) or (t shr (32-12));
t:= c + ((d or not a) xor b) + X[ 5]+$6ED9EBA1; c:= (t shl 7) or (t shr (32-7));
t:= b + ((c or not d) xor a) + X[12]+$6ED9EBA1; b:= (t shl 5) or (t shr (32-5));
t:= a + ((b and d) or (c and not d)) + X[ 1]+$8F1BBCDC; a:= (t shl 11) or (t shr (32-11));
t:= d + ((a and c) or (b and not c)) + X[ 9]+$8F1BBCDC; d:= (t shl 12) or (t shr (32-12));
t:= c + ((d and b) or (a and not b)) + X[11]+$8F1BBCDC; c:= (t shl 14) or (t shr (32-14));
t:= b + ((c and a) or (d and not a)) + X[10]+$8F1BBCDC; b:= (t shl 15) or (t shr (32-15));
t:= a + ((b and d) or (c and not d)) + X[ 0]+$8F1BBCDC; a:= (t shl 14) or (t shr (32-14));
t:= d + ((a and c) or (b and not c)) + X[ 8]+$8F1BBCDC; d:= (t shl 15) or (t shr (32-15));
t:= c + ((d and b) or (a and not b)) + X[12]+$8F1BBCDC; c:= (t shl 9) or (t shr (32-9));
t:= b + ((c and a) or (d and not a)) + X[ 4]+$8F1BBCDC; b:= (t shl 8) or (t shr (32-8));
t:= a + ((b and d) or (c and not d)) + X[13]+$8F1BBCDC; a:= (t shl 9) or (t shr (32-9));
t:= d + ((a and c) or (b and not c)) + X[ 3]+$8F1BBCDC; d:= (t shl 14) or (t shr (32-14));
t:= c + ((d and b) or (a and not b)) + X[ 7]+$8F1BBCDC; c:= (t shl 5) or (t shr (32-5));
t:= b + ((c and a) or (d and not a)) + X[15]+$8F1BBCDC; b:= (t shl 6) or (t shr (32-6));
t:= a + ((b and d) or (c and not d)) + X[14]+$8F1BBCDC; a:= (t shl 8) or (t shr (32-8));
t:= d + ((a and c) or (b and not c)) + X[ 5]+$8F1BBCDC; d:= (t shl 6) or (t shr (32-6));
t:= c + ((d and b) or (a and not b)) + X[ 6]+$8F1BBCDC; c:= (t shl 5) or (t shr (32-5));
t:= b + ((c and a) or (d and not a)) + X[ 2]+$8F1BBCDC; b:= (t shl 12) or (t shr (32-12));
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 5]+$50A28BE6; aa:= (t shl 8) or (t shr (32-8));
t:= dd + ((aa and cc) or (bb and not cc)) + X[14]+$50A28BE6; dd:= (t shl 9) or (t shr (32-9));
t:= cc + ((dd and bb) or (aa and not bb)) + X[ 7]+$50A28BE6; cc:= (t shl 9) or (t shr (32-9));
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 0]+$50A28BE6; bb:= (t shl 11) or (t shr (32-11));
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 9]+$50A28BE6; aa:= (t shl 13) or (t shr (32-13));
t:= dd + ((aa and cc) or (bb and not cc)) + X[ 2]+$50A28BE6; dd:= (t shl 15) or (t shr (32-15));
t:= cc + ((dd and bb) or (aa and not bb)) + X[11]+$50A28BE6; cc:= (t shl 15) or (t shr (32-15));
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 4]+$50A28BE6; bb:= (t shl 5) or (t shr (32-5));
t:= aa + ((bb and dd) or (cc and not dd)) + X[13]+$50A28BE6; aa:= (t shl 7) or (t shr (32-7));
t:= dd + ((aa and cc) or (bb and not cc)) + X[ 6]+$50A28BE6; dd:= (t shl 7) or (t shr (32-7));
t:= cc + ((dd and bb) or (aa and not bb)) + X[15]+$50A28BE6; cc:= (t shl 8) or (t shr (32-8));
t:= bb + ((cc and aa) or (dd and not aa)) + X[ 8]+$50A28BE6; bb:= (t shl 11) or (t shr (32-11));
t:= aa + ((bb and dd) or (cc and not dd)) + X[ 1]+$50A28BE6; aa:= (t shl 14) or (t shr (32-14));
t:= dd + ((aa and cc) or (bb and not cc)) + X[10]+$50A28BE6; dd:= (t shl 14) or (t shr (32-14));
t:= cc + ((dd and bb) or (aa and not bb)) + X[ 3]+$50A28BE6; cc:= (t shl 12) or (t shr (32-12));
t:= bb + ((cc and aa) or (dd and not aa)) + X[12]+$50A28BE6; bb:= (t shl 6) or (t shr (32-6));
t:= aa + ((bb or not cc) xor dd) + X[ 6]+$5C4DD124; aa:= (t shl 9) or (t shr (32-9));
t:= dd + ((aa or not bb) xor cc) + X[11]+$5C4DD124; dd:= (t shl 13) or (t shr (32-13));
t:= cc + ((dd or not aa) xor bb) + X[ 3]+$5C4DD124; cc:= (t shl 15) or (t shr (32-15));
t:= bb + ((cc or not dd) xor aa) + X[ 7]+$5C4DD124; bb:= (t shl 7) or (t shr (32-7));
t:= aa + ((bb or not cc) xor dd) + X[ 0]+$5C4DD124; aa:= (t shl 12) or (t shr (32-12));
t:= dd + ((aa or not bb) xor cc) + X[13]+$5C4DD124; dd:= (t shl 8) or (t shr (32-8));
t:= cc + ((dd or not aa) xor bb) + X[ 5]+$5C4DD124; cc:= (t shl 9) or (t shr (32-9));
t:= bb + ((cc or not dd) xor aa) + X[10]+$5C4DD124; bb:= (t shl 11) or (t shr (32-11));
t:= aa + ((bb or not cc) xor dd) + X[14]+$5C4DD124; aa:= (t shl 7) or (t shr (32-7));
t:= dd + ((aa or not bb) xor cc) + X[15]+$5C4DD124; dd:= (t shl 7) or (t shr (32-7));
t:= cc + ((dd or not aa) xor bb) + X[ 8]+$5C4DD124; cc:= (t shl 12) or (t shr (32-12));
t:= bb + ((cc or not dd) xor aa) + X[12]+$5C4DD124; bb:= (t shl 7) or (t shr (32-7));
t:= aa + ((bb or not cc) xor dd) + X[ 4]+$5C4DD124; aa:= (t shl 6) or (t shr (32-6));
t:= dd + ((aa or not bb) xor cc) + X[ 9]+$5C4DD124; dd:= (t shl 15) or (t shr (32-15));
t:= cc + ((dd or not aa) xor bb) + X[ 1]+$5C4DD124; cc:= (t shl 13) or (t shr (32-13));
t:= bb + ((cc or not dd) xor aa) + X[ 2]+$5C4DD124; bb:= (t shl 11) or (t shr (32-11));
t:= aa + ((bb and cc) or (not bb and dd)) + X[15]+$6D703EF3; aa:= (t shl 9) or (t shr (32-9));
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 5]+$6D703EF3; dd:= (t shl 7) or (t shr (32-7));
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 1]+$6D703EF3; cc:= (t shl 15) or (t shr (32-15));
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 3]+$6D703EF3; bb:= (t shl 11) or (t shr (32-11));
t:= aa + ((bb and cc) or (not bb and dd)) + X[ 7]+$6D703EF3; aa:= (t shl 8) or (t shr (32-8));
t:= dd + ((aa and bb) or (not aa and cc)) + X[14]+$6D703EF3; dd:= (t shl 6) or (t shr (32-6));
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 6]+$6D703EF3; cc:= (t shl 6) or (t shr (32-6));
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 9]+$6D703EF3; bb:= (t shl 14) or (t shr (32-14));
t:= aa + ((bb and cc) or (not bb and dd)) + X[11]+$6D703EF3; aa:= (t shl 12) or (t shr (32-12));
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 8]+$6D703EF3; dd:= (t shl 13) or (t shr (32-13));
t:= cc + ((dd and aa) or (not dd and bb)) + X[12]+$6D703EF3; cc:= (t shl 5) or (t shr (32-5));
t:= bb + ((cc and dd) or (not cc and aa)) + X[ 2]+$6D703EF3; bb:= (t shl 14) or (t shr (32-14));
t:= aa + ((bb and cc) or (not bb and dd)) + X[10]+$6D703EF3; aa:= (t shl 13) or (t shr (32-13));
t:= dd + ((aa and bb) or (not aa and cc)) + X[ 0]+$6D703EF3; dd:= (t shl 13) or (t shr (32-13));
t:= cc + ((dd and aa) or (not dd and bb)) + X[ 4]+$6D703EF3; cc:= (t shl 7) or (t shr (32-7));
t:= bb + ((cc and dd) or (not cc and aa)) + X[13]+$6D703EF3; bb:= (t shl 5) or (t shr (32-5));
t:= aa + (bb xor cc xor dd) + X[ 8]; aa:= (t shl 15) or (t shr (32-15));
t:= dd + (aa xor bb xor cc) + X[ 6]; dd:= (t shl 5) or (t shr (32-5));
t:= cc + (dd xor aa xor bb) + X[ 4]; cc:= (t shl 8) or (t shr (32-8));
t:= bb + (cc xor dd xor aa) + X[ 1]; bb:= (t shl 11) or (t shr (32-11));
t:= aa + (bb xor cc xor dd) + X[ 3]; aa:= (t shl 14) or (t shr (32-14));
t:= dd + (aa xor bb xor cc) + X[11]; dd:= (t shl 14) or (t shr (32-14));
t:= cc + (dd xor aa xor bb) + X[15]; cc:= (t shl 6) or (t shr (32-6));
t:= bb + (cc xor dd xor aa) + X[ 0]; bb:= (t shl 14) or (t shr (32-14));
t:= aa + (bb xor cc xor dd) + X[ 5]; aa:= (t shl 6) or (t shr (32-6));
t:= dd + (aa xor bb xor cc) + X[12]; dd:= (t shl 9) or (t shr (32-9));
t:= cc + (dd xor aa xor bb) + X[ 2]; cc:= (t shl 12) or (t shr (32-12));
t:= bb + (cc xor dd xor aa) + X[13]; bb:= (t shl 9) or (t shr (32-9));
t:= aa + (bb xor cc xor dd) + X[ 9]; aa:= (t shl 12) or (t shr (32-12));
t:= dd + (aa xor bb xor cc) + X[ 7]; dd:= (t shl 5) or (t shr (32-5));
t:= cc + (dd xor aa xor bb) + X[10]; cc:= (t shl 15) or (t shr (32-15));
t:= bb + (cc xor dd xor aa) + X[14]; bb:= (t shl 8) or (t shr (32-8));
Inc(dd,c + fCurHash[1]);
fCurHash[1]:= fCurHash[2] + d + aa;
fCurHash[2]:= fCurHash[3] + a + bb;
fCurHash[3]:= fCurHash[0] + b + cc;
fCurHash[0]:= dd;
FillChar(X,Sizeof(X),0);
fIndex:= 0;
FillChar(fHashBuff,Sizeof(fHashBuff),0);
end;
procedure THashRipeMD128.Init;
begin
Burn;
fCurHash[0]:= $67452301;
fCurHash[1]:= $efcdab89;
fCurHash[2]:= $98badcfe;
fCurHash[3]:= $10325476;
end;
procedure THashRipeMD128.Update(const Buffer; Size: Cardinal);
var
PBuf: PByte;
begin
Inc(fLenHi,Size shr 29);
Inc(fLenLo,Size*8);
if fLenLo< (Size*8) then
Inc(fLenHi);
PBuf:= @Buffer;
while Size> 0 do
begin
if (Sizeof(fHashBuff)-fIndex)<= Cardinal(Size) then
begin
Move(PBuf^,fHashBuff[fIndex],Sizeof(fHashBuff)-fIndex);
Dec(Size,Sizeof(fHashBuff)-fIndex);
Inc(PBuf,Sizeof(fHashBuff)-fIndex);
Compress;
end
else
begin
Move(PBuf^,fHashBuff[fIndex],Size);
Inc(fIndex,Size);
Size:= 0;
end;
end;
end;
procedure THashRipeMD128.Final(var Digest: array of Byte);
begin
fHashBuff[fIndex]:= $80;
if fIndex>= 56 then
Compress;
PCardinal(@fHashBuff[56])^:= fLenLo;
PCardinal(@fHashBuff[60])^:= fLenHi;
Compress;
Move(fCurHash,Digest,Sizeof(fCurHash));
Burn;
end;
procedure THashRipeMD128.Burn;
begin
fLenHi := 0; fLenLo := 0;
fIndex := 0;
FillChar(fHashBuff,Sizeof(fHashBuff),0);
FillChar(fCurHash,Sizeof(fCurHash),0);
end;
class function THashRipeMD128.GetSize: integer;
begin
Result := SizeOf(Cardinal) * 4;
end;
initialization
THash.RegisterHashType('RIPEMD-128', THashRipeMD128);
end.