author | Claus Gittinger <cg@exept.de> |
Sun, 08 Jan 2012 01:55:19 +0100 | |
changeset 13894 | 2f0b0aea723c |
parent 13001 | 787e592f48f6 |
child 13896 | f93b997ad55c |
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. |
|
6864 | 60 |
|
12265 | 61 |
performance: roughly |
12756 | 62 |
80000 Kb/s on a 2Ghz Duo |
63 |
27200 Kb/s on a 1.2Ghz Athlon |
|
64 |
12600 Kb/s on a 400Mhz PIII |
|
65 |
9150 Kb/s on a 300Mhz Sparc. |
|
66 |
performance is almost completely limited by the speed of the md5-routine, which is the reference |
|
67 |
implementation in C from md5lib. |
|
68 |
||
6864 | 69 |
[author:] |
12756 | 70 |
Stefan Vogel |
6864 | 71 |
|
72 |
[see also:] |
|
12756 | 73 |
SHA1Stream |
6864 | 74 |
|
75 |
[class variables:] |
|
12756 | 76 |
HashSize size of returned hash value |
77 |
ContextSize (implementation) size of hash context |
|
6864 | 78 |
|
79 |
[instance variables:] |
|
12756 | 80 |
hashContext (implementation) |
81 |
internal buffer for computation of the hash value |
|
6864 | 82 |
" |
83 |
! |
|
84 |
||
85 |
examples |
|
86 |
" |
|
13001 | 87 |
[exBegin] |
10782 | 88 |
Test Vectors (from FIPS PUB 180-1); results are: |
7041 | 89 |
|
10782 | 90 |
'abc' |
91 |
-> #[90 1 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72] |
|
6864 | 92 |
|
10782 | 93 |
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' |
94 |
-> #[82 15 EF 7 96 A2 B CA AA E1 16 D3 87 6C 66 4A] |
|
7041 | 95 |
|
10782 | 96 |
A million repetitions of 'a' |
97 |
-> #[77 7 D6 AE 4E 2 7C 70 EE A2 A9 35 C2 29 6F 21] |
|
13001 | 98 |
[exEnd] |
6864 | 99 |
|
13001 | 100 |
[exBegin] |
101 |
Transcript showCR:(MD5Stream hashValueOf:'abc') hexPrintString |
|
102 |
[exEnd] |
|
103 |
||
104 |
[exBegin] |
|
12265 | 105 |
(MD5Stream hashValueOf:'abc') |
13001 | 106 |
printOn:Transcript base:16. |
10782 | 107 |
Transcript cr. |
13001 | 108 |
[exEnd] |
6864 | 109 |
|
13001 | 110 |
[exBegin] |
12265 | 111 |
(MD5Stream hashValueOfStream:('abc' readStream)) |
13001 | 112 |
printOn:Transcript base:16. |
10782 | 113 |
Transcript cr. |
13001 | 114 |
[exEnd] |
6864 | 115 |
|
13001 | 116 |
[exBegin] |
6864 | 117 |
|hashStream| |
118 |
||
119 |
hashStream := MD5Stream new. |
|
120 |
hashStream nextPut:'abc'. |
|
121 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
122 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
|
123 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 124 |
[exEnd] |
6864 | 125 |
|
13001 | 126 |
[exBegin] |
6864 | 127 |
|hashStream| |
128 |
||
129 |
hashStream := MD5Stream new. |
|
7041 | 130 |
hashStream nextPut:'a' asByteArray. |
131 |
hashStream nextPut:'bc' asByteArray. |
|
6864 | 132 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
133 |
hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
|
134 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 135 |
[exEnd] |
6864 | 136 |
|
13001 | 137 |
[exBegin] |
6864 | 138 |
|hashStream| |
139 |
||
140 |
hashStream := MD5Stream new. |
|
141 |
1000000 timesRepeat:[ hashStream nextPut:$a ]. |
|
142 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 143 |
[exEnd] |
6864 | 144 |
|
13001 | 145 |
[exBegin] |
6864 | 146 |
|hashStream| |
147 |
||
148 |
hashStream := MD5Stream new. |
|
149 |
hashStream nextPut:'a'. |
|
150 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 151 |
[exEnd] |
6864 | 152 |
|
13001 | 153 |
[exBegin] |
6864 | 154 |
|hashStream| |
155 |
||
156 |
hashStream := MD5Stream new. |
|
157 |
hashStream nextPut:$a. |
|
158 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 159 |
[exEnd] |
6864 | 160 |
|
13001 | 161 |
[exBegin] |
6864 | 162 |
|hashStream| |
163 |
||
164 |
hashStream := MD5Stream new. |
|
165 |
hashStream nextPut:'abc'. |
|
166 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
167 |
hashStream reset. |
|
168 |
hashStream nextPut:'abc'. |
|
169 |
hashStream hashValue printOn:Transcript base:16. Transcript cr. |
|
13001 | 170 |
[exEnd] |
6864 | 171 |
|
172 |
timing throughput: |
|
13001 | 173 |
[exBegin] |
6864 | 174 |
|hashStream n t| |
175 |
||
176 |
hashStream := MD5Stream new. |
|
177 |
n := 1000000. |
|
178 |
t := Time millisecondsToRun:[ |
|
13001 | 179 |
n timesRepeat:[ |
180 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
181 |
]. |
|
182 |
]. |
|
6864 | 183 |
t := (t / 1000) asFloat. |
184 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
185 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
13001 | 186 |
[exEnd] |
7041 | 187 |
" |
6864 | 188 |
! ! |
189 |
||
190 |
!MD5Stream class methodsFor:'initialization'! |
|
191 |
||
192 |
initialize |
|
193 |
|ctxSize| |
|
194 |
||
195 |
%{ |
|
8913 | 196 |
ctxSize = __mkSmallInteger(sizeof(MD5_CTX)); |
6864 | 197 |
%}. |
198 |
ContextSize := ctxSize. |
|
199 |
HashSize := 16. |
|
200 |
||
201 |
" |
|
202 |
self initialize |
|
203 |
" |
|
204 |
||
205 |
||
206 |
||
207 |
! ! |
|
208 |
||
209 |
!MD5Stream class methodsFor:'queries'! |
|
210 |
||
211 |
blockSize |
|
212 |
"return the block size used internally by the compression function" |
|
213 |
||
214 |
^ 64 |
|
215 |
||
216 |
"Created: / 18.3.1999 / 08:36:44 / stefan" |
|
217 |
! |
|
218 |
||
219 |
hashSize |
|
220 |
"return the size of the hashvalue returned by instances of this class" |
|
221 |
||
222 |
^ HashSize |
|
223 |
||
224 |
"Created: / 18.3.1999 / 08:02:16 / stefan" |
|
225 |
! ! |
|
226 |
||
7042 | 227 |
!MD5Stream class methodsFor:'testing'! |
228 |
||
229 |
testVector |
|
230 |
||
12265 | 231 |
^ #( |
12751 | 232 |
('abc' |
233 |
#[16r90 16r01 16r50 16r98 16r3C 16rD2 16r4F 16rB0 16rD6 16r96 16r3F 16r7D 16r28 16rE1 16r7F 16r72]) |
|
7042 | 234 |
|
12751 | 235 |
('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' |
236 |
#[16r82 16r15 16rEF 16r07 16r96 16rA2 16r0B 16rCA 16rAA 16rE1 16r16 16rD3 16r87 16r6C 16r66 16r4A]) |
|
237 |
) copyWith: |
|
238 |
(Array with:(String new:1000000 withAll:$a) "/ a million a's |
|
239 |
with:#[16r77 16r07 16rD6 16rAE 16r4E 16r02 16r7C 16r70 16rEE 16rA2 16rA9 16r35 16rC2 16r29 16r6F 16r21]) |
|
7042 | 240 |
|
241 |
||
242 |
" |
|
243 |
self test |
|
244 |
" |
|
245 |
! ! |
|
246 |
||
6864 | 247 |
!MD5Stream methodsFor:'initialization'! |
248 |
||
249 |
initialize |
|
250 |
||
251 |
hashContext := ByteArray new:ContextSize. |
|
252 |
self reset. |
|
253 |
||
254 |
"Modified: / 18.3.1999 / 08:03:42 / stefan" |
|
255 |
! ! |
|
256 |
||
257 |
!MD5Stream methodsFor:'positioning'! |
|
258 |
||
259 |
reset |
|
260 |
"reset the stream in order to compute a new hash value" |
|
261 |
||
262 |
%{ |
|
263 |
if (__isNonNilObject(__INST(hashContext)) && |
|
264 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
265 |
__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) |
|
266 |
) { |
|
12265 | 267 |
MD5_CTX *ctx = |
6864 | 268 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
269 |
||
270 |
MD5Init(ctx); |
|
271 |
RETURN(self); |
|
272 |
} |
|
273 |
%}. |
|
274 |
^ self primitiveFailed |
|
275 |
||
276 |
||
277 |
||
278 |
! ! |
|
279 |
||
280 |
!MD5Stream methodsFor:'queries'! |
|
281 |
||
282 |
hashValue |
|
283 |
"Get the value hashed so far. |
|
284 |
The context is kept, so that more objects may be hashed after |
|
285 |
retrieving a hash value" |
|
286 |
||
287 |
||
288 |
|digest| |
|
289 |
||
290 |
digest := ByteArray new:HashSize. |
|
291 |
||
292 |
%{ |
|
293 |
if (__isNonNilObject(__INST(hashContext)) && |
|
294 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
295 |
__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) && |
|
296 |
__isNonNilObject(digest) && |
|
297 |
__qClass(digest) == @global(ByteArray) && |
|
298 |
__byteArraySize(digest) == 16 |
|
299 |
) { |
|
12265 | 300 |
MD5_CTX *ctx = |
6864 | 301 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
302 |
MD5_CTX copyContext; |
|
303 |
||
304 |
memcpy(©Context, ctx, sizeof(copyContext)); |
|
305 |
MD5Final(__ByteArrayInstPtr(digest)->ba_element, ©Context); |
|
306 |
RETURN(digest); |
|
307 |
} |
|
308 |
%}. |
|
309 |
||
12265 | 310 |
^ self primitiveFailed |
6864 | 311 |
! ! |
312 |
||
313 |
!MD5Stream methodsFor:'writing'! |
|
314 |
||
315 |
nextPut:anObject |
|
316 |
"update our hash value for anObject. |
|
317 |
anObject may be a String, a Character, a Smallinteger or an Array of primitive |
|
7042 | 318 |
types like ByteArray" |
6864 | 319 |
|
320 |
|ret| |
|
321 |
||
322 |
%{ |
|
323 |
if (__isNonNilObject(__INST(hashContext)) && |
|
324 |
__qClass(__INST(hashContext)) == @global(ByteArray) && |
|
325 |
__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) |
|
326 |
) { |
|
13894 | 327 |
MD5_CTX *ctx = |
328 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
6864 | 329 |
|
13894 | 330 |
if (__isNonNilObject(anObject)) { |
331 |
OBJ cls =__qClass(anObject); |
|
332 |
INT mask = (INT)(__ClassInstPtr(cls)->c_flags) & __MASKSMALLINT(ARRAYMASK); |
|
6864 | 333 |
|
13894 | 334 |
if (cls == @global(String) || cls == @global(Symbol)) { |
335 |
/* String: omit leading '\0' */ |
|
6864 | 336 |
|
13894 | 337 |
MD5Update(ctx, __StringInstPtr(anObject)->s_element, __stringSize(anObject)); |
338 |
} else if (mask != __MASKSMALLINT(POINTERARRAY) && |
|
339 |
mask != __MASKSMALLINT(WKPOINTERARRAY) && |
|
340 |
mask != __MASKSMALLINT(0) |
|
341 |
) { |
|
342 |
/* Byte|Integer|.... Array */ |
|
6864 | 343 |
|
13894 | 344 |
register int n; |
345 |
char *pFirst; |
|
346 |
||
347 |
n /* nInstVars */ = __intVal(__ClassInstPtr(cls)->c_ninstvars); |
|
348 |
n /* nInstBytes */ = OHDR_SIZE + __OBJS2BYTES__(n /* nInstVars */); |
|
349 |
pFirst = (char *)(__InstPtr(anObject)) + n /* nInstBytes */; |
|
350 |
n /* nbytes */ = __qSize(anObject) - n /* nInstBytes */; |
|
351 |
MD5Update(ctx, pFirst, n); |
|
352 |
} else if (cls == @global(Character)) { |
|
353 |
/* Character */ |
|
6864 | 354 |
|
13894 | 355 |
INT val = __intVal(_characterVal(anObject)); |
356 |
if (val > 255) { |
|
357 |
/* Two byte character */ |
|
358 |
short s = val; |
|
359 |
MD5Update(ctx, &s, 2); |
|
360 |
} else { |
|
361 |
char c = val; |
|
362 |
MD5Update(ctx, &c, 1); |
|
363 |
} |
|
364 |
} else { |
|
365 |
ret = false; |
|
366 |
} |
|
367 |
} else { |
|
368 |
if (anObject == nil) { |
|
369 |
ret = false; |
|
370 |
} else { |
|
371 |
#if 0 |
|
372 |
// cg: I hope, noone used that interface; it depended on the machines byteorder ! |
|
373 |
/* SmallInteger */ |
|
12265 | 374 |
|
13894 | 375 |
INT i = __intVal(anObject); |
376 |
MD5Update(ctx, &i, sizeof(INT)); |
|
377 |
#else |
|
378 |
ret = false; |
|
379 |
#endif |
|
380 |
} |
|
381 |
} |
|
6864 | 382 |
} |
383 |
%}. |
|
384 |
||
385 |
ret notNil ifTrue:[ |
|
13894 | 386 |
^ self primitiveFailed |
6864 | 387 |
]. |
12265 | 388 |
|
13894 | 389 |
"Created: / 22-10-1996 / 21:53:24 / stefan" |
390 |
"Modified: / 08-01-2012 / 01:49:47 / cg" |
|
7042 | 391 |
! |
392 |
||
393 |
nextPutBytes:count from:anObject startingAt:start |
|
394 |
"update the hash value with count bytes from an object starting at index start. |
|
12265 | 395 |
The object must have non-pointer indexed instvars |
7042 | 396 |
(i.e. be a ByteArray, String, Float- or DoubleArray), |
397 |
or an externalBytes object (with known size)" |
|
398 |
||
399 |
%{ |
|
400 |
int len, offs; |
|
401 |
int objSize, nInstVars, nInstBytes; |
|
402 |
char *extPtr; |
|
403 |
||
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
404 |
if (__isNonNilObject(__INST(hashContext)) |
7042 | 405 |
&&__qClass(__INST(hashContext)) == @global(ByteArray) |
406 |
&&__byteArraySize(__INST(hashContext)) == sizeof(MD5_CTX) |
|
407 |
&& __bothSmallInteger(count, start) |
|
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
408 |
) { |
12265 | 409 |
MD5_CTX *ctx = |
410 |
(MD5_CTX *)__ByteArrayInstPtr(__INST(hashContext))->ba_element; |
|
7042 | 411 |
|
12265 | 412 |
len = __intVal(count); |
413 |
offs = __intVal(start) - 1; |
|
7042 | 414 |
|
12265 | 415 |
if (__isExternalBytesLike(anObject)) { |
416 |
OBJ sz; |
|
7042 | 417 |
|
12265 | 418 |
nInstBytes = 0; |
419 |
extPtr = (char *)__externalBytesAddress(anObject); |
|
420 |
sz = __externalBytesSize(anObject); |
|
421 |
if (__isSmallInteger(sz)) { |
|
422 |
objSize = __intVal(sz); |
|
423 |
} else { |
|
424 |
objSize = 0; /* unknown */ |
|
425 |
} |
|
426 |
} else { |
|
427 |
OBJ oClass; |
|
7197
a68adb223592
also handle subclasses of ExternalBytes (Mapped..)
Claus Gittinger <cg@exept.de>
parents:
7042
diff
changeset
|
428 |
|
12265 | 429 |
oClass = __Class(anObject); |
430 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
431 |
case BYTEARRAY: |
|
432 |
case WORDARRAY: |
|
433 |
case LONGARRAY: |
|
434 |
case SWORDARRAY: |
|
435 |
case SLONGARRAY: |
|
436 |
case FLOATARRAY: |
|
437 |
case DOUBLEARRAY: |
|
438 |
break; |
|
439 |
default: |
|
440 |
goto bad; |
|
441 |
} |
|
442 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
443 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
444 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
445 |
// subtract from size and add to byte-pointer |
|
446 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
|
447 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
|
448 |
} |
|
449 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
450 |
MD5Update(ctx, extPtr+offs, len); |
|
451 |
RETURN (count); |
|
452 |
} |
|
7042 | 453 |
} |
454 |
bad: ; |
|
455 |
%}. |
|
456 |
||
457 |
^ self primitiveFailed |
|
6864 | 458 |
! ! |
459 |
||
460 |
!MD5Stream class methodsFor:'documentation'! |
|
461 |
||
462 |
version |
|
13894 | 463 |
^ '$Header: /cvs/stx/stx/libbasic/MD5Stream.st,v 1.15 2012-01-08 00:55:19 cg Exp $' |
12204 | 464 |
! |
465 |
||
466 |
version_CVS |
|
13894 | 467 |
^ '$Header: /cvs/stx/stx/libbasic/MD5Stream.st,v 1.15 2012-01-08 00:55:19 cg Exp $' |
6864 | 468 |
! ! |
469 |
||
470 |
MD5Stream initialize! |