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