author | Claus Gittinger <cg@exept.de> |
Tue, 10 Jan 2012 23:48:34 +0100 | |
changeset 13925 | f7a5ed87dcf7 |
parent 13917 | d1b2343d716e |
child 13932 | 46c83dd00617 |
permissions | -rw-r--r-- |
6864 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
7041 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
6864 | 14 |
HashStream subclass:#MD5Stream |
15 |
instanceVariableNames:'hashContext' |
|
16 |
classVariableNames:'HashSize ContextSize' |
|
17 |
poolDictionaries:'' |
|
12419 | 18 |
category:'System-Crypt-Hashing' |
6864 | 19 |
! |
20 |
||
21 |
!MD5Stream primitiveDefinitions! |
|
22 |
%{ |
|
23 |
||
24 |
/* |
|
25 |
* includes, defines, structure definitions |
|
26 |
* and typedefs come here. |
|
27 |
*/ |
|
28 |
||
29 |
#include "md5.h" |
|
30 |
||
31 |
%} |
|
32 |
! ! |
|
33 |
||
34 |
!MD5Stream class methodsFor:'documentation'! |
|
35 |
||
36 |
copyright |
|
37 |
" |
|
38 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
39 |
All Rights Reserved |
|
40 |
||
41 |
This software is furnished under a license and may be used |
|
42 |
only in accordance with the terms of that license and with the |
|
43 |
inclusion of the above copyright notice. This software may not |
|
44 |
be provided or otherwise made available to, or used by, any |
|
45 |
other person. No title to or ownership of the software is |
|
46 |
hereby transferred. |
|
47 |
" |
|
48 |
||
49 |
||
50 |
! |
|
51 |
||
52 |
documentation |
|
53 |
" |
|
54 |
Generate a MD5 hash value as defined in RFC 1321. |
|
12756 | 55 |
This may be used as checksum or for generating cryptographic signatures. |
56 |
||
57 |
Note: |
|
58 |
in August 2004, some researchers have found a way to generate full collisions for MD5. |
|
59 |
Therefore, for new applications, it may be wise to choose another hash function for security stuff. |
|
13896 | 60 |
See a hash-collision example in the examples method. |
6864 | 61 |
|
12265 | 62 |
performance: roughly |
12756 | 63 |
80000 Kb/s on a 2Ghz Duo |
64 |
27200 Kb/s on a 1.2Ghz Athlon |
|
65 |
12600 Kb/s on a 400Mhz PIII |
|
66 |
9150 Kb/s on a 300Mhz Sparc. |
|
67 |
performance is almost completely limited by the speed of the md5-routine, which is the reference |
|
68 |
implementation in C from md5lib. |
|
69 |
||
6864 | 70 |
[author:] |
12756 | 71 |
Stefan Vogel |
6864 | 72 |
|
73 |
[see also:] |
|
12756 | 74 |
SHA1Stream |
6864 | 75 |
|
76 |
[class variables:] |
|
12756 | 77 |
HashSize size of returned hash value |
78 |
ContextSize (implementation) size of hash context |
|
6864 | 79 |
|
80 |
[instance variables:] |
|
12756 | 81 |
hashContext (implementation) |
82 |
internal buffer for computation of the hash value |
|
6864 | 83 |
" |
84 |
! |
|
85 |
||
86 |
examples |
|
87 |
" |
|
13001 | 88 |
[exBegin] |
10782 | 89 |
Test Vectors (from FIPS PUB 180-1); results are: |
7041 | 90 |
|
10782 | 91 |
'abc' |
92 |
-> #[90 1 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72] |
|
6864 | 93 |
|
10782 | 94 |
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' |
95 |
-> #[82 15 EF 7 96 A2 B CA AA E1 16 D3 87 6C 66 4A] |
|
7041 | 96 |
|
10782 | 97 |
A million repetitions of 'a' |
98 |
-> #[77 7 D6 AE 4E 2 7C 70 EE A2 A9 35 C2 29 6F 21] |
|
13001 | 99 |
[exEnd] |
6864 | 100 |
|
13001 | 101 |
[exBegin] |
102 |
Transcript showCR:(MD5Stream hashValueOf:'abc') hexPrintString |
|
103 |
[exEnd] |
|
104 |
||
105 |
[exBegin] |
|
12265 | 106 |
(MD5Stream hashValueOf:'abc') |
13001 | 107 |
printOn:Transcript base:16. |
10782 | 108 |
Transcript cr. |
13001 | 109 |
[exEnd] |
6864 | 110 |
|
13001 | 111 |
[exBegin] |
12265 | 112 |
(MD5Stream hashValueOfStream:('abc' readStream)) |
13001 | 113 |
printOn:Transcript base:16. |
10782 | 114 |
Transcript cr. |
13001 | 115 |
[exEnd] |
6864 | 116 |
|
13001 | 117 |
[exBegin] |
6864 | 118 |
|hashStream| |
119 |
||
120 |
hashStream := MD5Stream new. |
|
121 |
hashStream nextPut:'abc'. |
|
122 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
123 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
124 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 125 |
[exEnd] |
6864 | 126 |
|
13001 | 127 |
[exBegin] |
6864 | 128 |
|hashStream| |
129 |
||
130 |
hashStream := MD5Stream new. |
|
7041 | 131 |
hashStream nextPut:'a' asByteArray. |
132 |
hashStream nextPut:'bc' asByteArray. |
|
6864 | 133 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
134 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
135 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 136 |
[exEnd] |
6864 | 137 |
|
13001 | 138 |
[exBegin] |
6864 | 139 |
|hashStream| |
140 |
||
141 |
hashStream := MD5Stream new. |
|
142 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
|
143 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 144 |
[exEnd] |
6864 | 145 |
|
13001 | 146 |
[exBegin] |
6864 | 147 |
|hashStream| |
148 |
||
149 |
hashStream := MD5Stream new. |
|
150 |
hashStream nextPut:'a'. |
|
151 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 152 |
[exEnd] |
6864 | 153 |
|
13001 | 154 |
[exBegin] |
6864 | 155 |
|hashStream| |
156 |
||
157 |
hashStream := MD5Stream new. |
|
158 |
hashStream nextPut:$a. |
|
159 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 160 |
[exEnd] |
6864 | 161 |
|
13001 | 162 |
[exBegin] |
6864 | 163 |
|hashStream| |
164 |
||
165 |
hashStream := MD5Stream new. |
|
166 |
hashStream nextPut:'abc'. |
|
167 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
168 |
hashStream reset. |
|
169 |
hashStream nextPut:'abc'. |
|
170 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 171 |
[exEnd] |
6864 | 172 |
|
13896 | 173 |
a collision: |
174 |
[exBegin] |
|
175 |
|hashStream| |
|
176 |
||
177 |
hashStream := MD5Stream new. |
|
178 |
hashStream nextPut:(ByteArray fromHexStringWithSeparators:' |
|
179 |
d131dd02c5e6eec4693d9a0698aff95c 2fcab58712467eab4004583eb8fb7f89 |
|
180 |
55ad340609f4b30283e488832571415a 085125e8f7cdc99fd91dbdf280373c5b |
|
181 |
d8823e3156348f5bae6dacd436c919c6 dd53e2b487da03fd02396306d248cda0 |
|
182 |
e99f33420f577ee8ce54b67080a80d1e c69821bcb6a8839396f9652b6ff72a70'). |
|
183 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
184 |
hashStream reset. |
|
185 |
||
186 |
hashStream nextPut:(ByteArray fromHexStringWithSeparators:' |
|
187 |
d131dd02c5e6eec4693d9a0698aff95c 2fcab50712467eab4004583eb8fb7f89 |
|
188 |
55ad340609f4b30283e4888325f1415a 085125e8f7cdc99fd91dbd7280373c5b |
|
189 |
d8823e3156348f5bae6dacd436c919c6 dd53e23487da03fd02396306d248cda0 |
|
190 |
e99f33420f577ee8ce54b67080280d1e c69821bcb6a8839396f965ab6ff72a70'). |
|
191 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
192 |
[exEnd] |
|
193 |
||
6864 | 194 |
timing throughput: |
13001 | 195 |
[exBegin] |
6864 | 196 |
|hashStream n t| |
197 |
||
198 |
hashStream := MD5Stream new. |
|
199 |
n := 1000000. |
|
200 |
t := Time millisecondsToRun:[ |
|
13001 | 201 |
n timesRepeat:[ |
202 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
203 |
]. |
|
204 |
]. |
|
6864 | 205 |
t := (t / 1000) asFloat. |
206 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
207 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
13001 | 208 |
[exEnd] |
7041 | 209 |
" |
6864 | 210 |
! ! |
211 |
||
212 |
!MD5Stream class methodsFor:'initialization'! |
|
213 |
||
214 |
initialize |
|
215 |
|ctxSize| |
|
216 |
||
217 |
%{ |
|
8913 | 218 |
ctxSize = __mkSmallInteger(sizeof(MD5_CTX)); |
6864 | 219 |
%}. |
220 |
ContextSize := ctxSize. |
|
221 |
HashSize := 16. |
|
222 |
||
223 |
" |
|
224 |
self initialize |
|
225 |
" |
|
226 |
||
227 |
||
228 |
||
229 |
! ! |
|
230 |
||
231 |
!MD5Stream class methodsFor:'queries'! |
|
232 |
||
233 |
blockSize |
|
234 |
"return the block size used internally by the compression function" |
|
235 |
||
236 |
^ 64 |
|
237 |
||
238 |
"Created: / 18.3.1999 / 08:36:44 / stefan" |
|
239 |
! |
|
240 |
||
241 |
hashSize |
|
242 |
"return the size of the hashvalue returned by instances of this class" |
|
243 |
||
244 |
^ HashSize |
|
245 |
||
246 |
"Created: / 18.3.1999 / 08:02:16 / stefan" |
|
247 |
! ! |
|
248 |
||
249 |
!MD5Stream methodsFor:'initialization'! |
|
250 |
||
251 |
initialize |
|
252 |
||
253 |
hashContext := ByteArray new:ContextSize. |
|
254 |
self reset. |
|
255 |
||
256 |
"Modified: / 18.3.1999 / 08:03:42 / stefan" |
|
257 |
! ! |
|
258 |
||
259 |
!MD5Stream methodsFor:'positioning'! |
|
260 |
||
261 |
reset |
|
262 |
"reset the stream in order to compute a new hash value" |
|
263 |
||
264 |
%{ |
|
265 |
if (__isNonNilObject(__INST(hashContext)) && |
|
266 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
267 |
__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) |
|
268 |
) { |
|
12265 | 269 |
MD5_CTX *ctx = |
6864 | 270 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
271 |
||
272 |
MD5Init(ctx); |
|
273 |
RETURN(self); |
|
274 |
} |
|
275 |
%}. |
|
276 |
^ self primitiveFailed |
|
277 |
||
278 |
||
279 |
||
280 |
! ! |
|
281 |
||
282 |
!MD5Stream methodsFor:'queries'! |
|
283 |
||
284 |
hashValue |
|
285 |
"Get the value hashed so far. |
|
286 |
The context is kept, so that more objects may be hashed after |
|
287 |
retrieving a hash value" |
|
288 |
||
289 |
||
290 |
|digest| |
|
291 |
||
292 |
digest := ByteArray new:HashSize. |
|
293 |
||
294 |
%{ |
|
295 |
if (__isNonNilObject(__INST(hashContext)) && |
|
296 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
297 |
__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) && |
|
298 |
__isNonNilObject(digest) && |
|
299 |
__qClass(digest) == @global(ByteArray) && |
|
300 |
__byteArraySize(digest) == 16 |
|
301 |
) { |
|
12265 | 302 |
MD5_CTX *ctx = |
6864 | 303 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
304 |
MD5_CTX copyContext; |
|
305 |
||
306 |
memcpy(©Context, ctx, sizeof(copyContext)); |
|
307 |
MD5Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
|
308 |
RETURN(digest); |
|
309 |
} |
|
310 |
%}. |
|
311 |
||
12265 | 312 |
^ self primitiveFailed |
6864 | 313 |
! ! |
314 |
||
315 |
!MD5Stream methodsFor:'writing'! |
|
316 |
||
7042 | 317 |
nextPutBytes:count from:anObject startingAt:start |
318 |
"update the hash value with count bytes from an object starting at index start. |
|
12265 | 319 |
The object must have non-pointer indexed instvars |
7042 | 320 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
321 |
or an externalBytes object (with known size)" |
|
322 |
||
323 |
%{ |
|
324 |
int len, offs; |
|
325 |
int objSize, nInstVars, nInstBytes; |
|
326 |
char *extPtr; |
|
327 |
||
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
328 |
if (__isNonNilObject(__INST(hashContext)) |
7042 | 329 |
&&__qClass(__INST(hashContext)) == @global(ByteArray) |
330 |
&&__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) |
|
331 |
&& __bothSmallInteger(count, start) |
|
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
332 |
) { |
12265 | 333 |
MD5_CTX *ctx = |
334 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
7042 | 335 |
|
12265 | 336 |
len = __intVal(count); |
337 |
offs = __intVal(start) - 1; |
|
7042 | 338 |
|
12265 | 339 |
if (__isExternalBytesLike(anObject)) { |
340 |
OBJ sz; |
|
7042 | 341 |
|
12265 | 342 |
nInstBytes = 0; |
343 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
344 |
sz = __externalBytesSize(anObject); |
|
345 |
if (__isSmallInteger(sz)) { |
|
346 |
objSize = __intVal(sz); |
|
347 |
} else { |
|
348 |
objSize = 0; /* unknown */ |
|
349 |
} |
|
350 |
} else { |
|
351 |
OBJ oClass; |
|
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
352 |
|
12265 | 353 |
oClass = __Class(anObject); |
354 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
355 |
case BYTEARRAY: |
|
356 |
case WORDARRAY: |
|
357 |
case LONGARRAY: |
|
358 |
case SWORDARRAY: |
|
359 |
case SLONGARRAY: |
|
360 |
case FLOATARRAY: |
|
361 |
case DOUBLEARRAY: |
|
362 |
break; |
|
363 |
default: |
|
364 |
goto bad; |
|
365 |
} |
|
366 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
367 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
368 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
369 |
// subtract from size and add to byte-pointer |
|
370 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
|
371 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
|
372 |
} |
|
373 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
374 |
MD5Update(ctx, extPtr+offs, len); |
|
375 |
RETURN (count); |
|
376 |
} |
|
7042 | 377 |
} |
378 |
bad: ; |
|
379 |
%}. |
|
380 |
||
381 |
^ self primitiveFailed |
|
6864 | 382 |
! ! |
383 |
||
384 |
!MD5Stream class methodsFor:'documentation'! |
|
385 |
||
386 |
version |
|
13925
f7a5ed87dcf7
all tests removed from hash classes and
Claus Gittinger <cg@exept.de>
parents:
13917
diff
changeset
|
387 |
^ '$Header: /cvs/stx/stx/libbasic/MD5Stream.st,v 1.19 2012-01-10 22:48:34 cg Exp $' |
12204 | 388 |
! |
389 |
||
390 |
version_CVS |
|
13925
f7a5ed87dcf7
all tests removed from hash classes and
Claus Gittinger <cg@exept.de>
parents:
13917
diff
changeset
|
391 |
^ '$Header: /cvs/stx/stx/libbasic/MD5Stream.st,v 1.19 2012-01-10 22:48:34 cg Exp $' |
6864 | 392 |
! ! |
393 |
||
394 |
MD5Stream initialize! |