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