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