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