author | Claus Gittinger <cg@exept.de> |
Mon, 27 May 2019 16:53:37 +0200 | |
changeset 24192 | 18512bf68422 |
parent 23381 | e02ede0d6d8d |
child 24268 | ec6569c964a0 |
permissions | -rw-r--r-- |
23381 | 1 |
"{ Encoding: utf8 }" |
2 |
||
6864 | 3 |
" |
4 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
7014 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19165 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
6864 | 18 |
Stream subclass:#HashStream |
7014 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
12418 | 22 |
category:'System-Crypt-Hashing' |
6864 | 23 |
! |
24 |
||
25 |
!HashStream class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
22512 | 44 |
Abstract class. |
13910 | 45 |
Subclasses generate hash values used as checksums |
6864 | 46 |
or for generating cryptographic signatures. |
47 |
||
13910 | 48 |
Notice: due to historic reasons and compatibility with Squeak, |
49 |
there are two modes of operation: |
|
50 |
1) hashFunction mode, in which the hash of a single block of bytes is computed |
|
51 |
2) hashStream mode, in which instances behave like a writeStream, computing and |
|
52 |
updating the hash, as data is sent to it. |
|
53 |
||
54 |
hashFunction mode is called using: #hashValueOf:aStringOrByteArray |
|
22512 | 55 |
Warning: Not all subclasses support the stream mode |
13910 | 56 |
(especially those which were ported from squeak do not). |
57 |
||
6864 | 58 |
[author:] |
59 |
Stefan Vogel |
|
60 |
||
61 |
[see also:] |
|
62 |
SHA1Stream MD5Stream |
|
63 |
" |
|
13910 | 64 |
! |
65 |
||
66 |
examples |
|
67 |
" |
|
68 |
hashFunction mode: |
|
69 |
[exBegin] |
|
70 |
MD5Stream hashValueOf:'hello world' |
|
71 |
MD4Stream hashValueOf:'hello world' |
|
72 |
[exEnd] |
|
73 |
||
74 |
hashStream mode: |
|
75 |
[exBegin] |
|
76 |
|md5| |
|
77 |
||
78 |
md5 := MD5Stream new. |
|
79 |
md5 nextPutAll:'hello world'. |
|
80 |
md5 hashValue |
|
81 |
[exEnd] |
|
82 |
" |
|
6864 | 83 |
! ! |
84 |
||
85 |
!HashStream class methodsFor:'instance creation'! |
|
86 |
||
7015 | 87 |
new |
88 |
"have to re-allow new - it was disabled in Stream" |
|
89 |
^ self basicNew initialize |
|
90 |
||
91 |
||
92 |
! |
|
93 |
||
94 |
random |
|
95 |
"create a random number generator using myself" |
|
96 |
||
97 |
^ HashRandom with:self |
|
98 |
||
99 |
" |
|
100 |
SHA1Stream random next |
|
101 |
" |
|
102 |
||
103 |
"Modified: / 12.11.1999 / 17:21:17 / stefan" |
|
104 |
! ! |
|
105 |
||
22513 | 106 |
!HashStream class methodsFor:'Compatibility-Squeak'! |
12145 | 107 |
|
108 |
hashMessage:aStringOrByteArrayOrStream |
|
12146 | 109 |
"SQUEAK compatibility |
110 |
- but this is a bad choice - squeak uses #digestMessage: at the instance side" |
|
12145 | 111 |
|
112 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
113 |
! ! |
|
114 |
||
12174 | 115 |
!HashStream class methodsFor:'queries'! |
116 |
||
15622
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
117 |
blockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
118 |
"return the block size when the hash is used by encryption/decryption. |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
119 |
(see OfbCipherMode and CtrCipherMode)" |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
120 |
|
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
121 |
^ self hashSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
122 |
! |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
123 |
|
12174 | 124 |
canStream |
125 |
"simple hash functions (squeak-ported) cannot stream. |
|
126 |
Use hashFunction: there" |
|
127 |
||
128 |
^ true |
|
13889 | 129 |
! |
130 |
||
15622
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
131 |
hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
132 |
"return the block size used internally by the compression function" |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
133 |
|
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
134 |
self subclassResponsibility |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
135 |
! |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
136 |
|
13889 | 137 |
hashSize |
13928 | 138 |
"return the size of the hashvalue returned by instances of this class (in bytes)" |
139 |
||
13889 | 140 |
self subclassResponsibility |
141 |
||
142 |
"Created: / 04-01-2012 / 19:22:32 / cg" |
|
21394 | 143 |
! |
144 |
||
145 |
isAbstract |
|
146 |
"Return if this class is an abstract class. |
|
147 |
True is returned here for myself only; false for subclasses. |
|
148 |
Abstract subclasses must redefine this again." |
|
149 |
||
150 |
^ self == HashStream. |
|
12174 | 151 |
! ! |
152 |
||
12146 | 153 |
!HashStream class methodsFor:'self tests'! |
7042 | 154 |
|
155 |
test |
|
156 |
"test against testVector" |
|
157 |
||
158 |
self testVector do:[:pair | |
|
13924 | 159 |
|data expectedHash expectedHashBytes computedHash hashStream| |
7042 | 160 |
|
161 |
data := pair first. |
|
162 |
expectedHash := pair second. |
|
12753 | 163 |
expectedHash isString ifTrue:[ |
164 |
expectedHashBytes := ByteArray fromHexString:expectedHash |
|
165 |
] ifFalse:[ |
|
166 |
expectedHashBytes := expectedHash |
|
167 |
]. |
|
7042 | 168 |
|
13366 | 169 |
"/ non-stream interface must be implemented by all |
13924 | 170 |
computedHash := self hashValueOf:data. |
171 |
self assert:computedHash = expectedHashBytes message:'Test failed'. |
|
12174 | 172 |
self canStream ifTrue:[ |
173 |
hashStream := self new. |
|
174 |
hashStream nextPut:data. |
|
13924 | 175 |
computedHash := hashStream hashValue. |
176 |
self assert:computedHash = expectedHashBytes message:'Test failed' |
|
7042 | 177 |
]. |
178 |
]. |
|
179 |
||
180 |
" |
|
181 |
MD5Stream test. |
|
182 |
SHA1Stream test. |
|
183 |
RipeMD160Stream test. |
|
184 |
" |
|
13924 | 185 |
|
186 |
"Modified: / 10-01-2012 / 22:35:17 / cg" |
|
7042 | 187 |
! |
188 |
||
189 |
testVector |
|
13918 | 190 |
"/ obsolete - moved to TestHashAlgorithms unit test. |
7042 | 191 |
|
192 |
^ self subclassResponsibility |
|
13918 | 193 |
|
194 |
"Modified (comment): / 09-01-2012 / 21:49:59 / cg" |
|
7042 | 195 |
! ! |
196 |
||
7015 | 197 |
!HashStream class methodsFor:'utilities'! |
198 |
||
19644 | 199 |
cryptBlock:aStringOrByteArray from:srcIdx to:srcEndOrNil into:resultOrNil startingAt:dstIdx encrypt:encryptMode |
15621 | 200 |
"one-way encryption of aStringOrByteArray. |
19644 | 201 |
Used when a HashStream is used as the block cipher with OFB or CTR mode. |
15621 | 202 |
|
203 |
encryptMode is ignored here." |
|
204 |
||
205 |
|hashValue| |
|
206 |
||
19644 | 207 |
(srcIdx == 1 and:[srcEndOrNil isNil]) ifTrue:[ |
15621 | 208 |
hashValue := self hashValueOf:aStringOrByteArray. |
209 |
] ifFalse:[ |
|
210 |
|bytesToEncrypt| |
|
211 |
||
212 |
bytesToEncrypt := aStringOrByteArray copyFrom:srcIdx to:srcIdx+self hashSize-1. |
|
213 |
hashValue := self hashValueOf:bytesToEncrypt. |
|
214 |
]. |
|
215 |
resultOrNil isNil ifTrue:[ |
|
216 |
^ hashValue. |
|
217 |
] ifFalse:[ |
|
218 |
resultOrNil replaceBytesFrom:dstIdx to:dstIdx+hashValue size-1 with:hashValue startingAt:1. |
|
219 |
^ resultOrNil. |
|
220 |
]. |
|
221 |
||
222 |
" |
|
223 |
|cipher iv cipherText| |
|
224 |
||
225 |
cipher := OfbCipher for:SHA512Stream. |
|
226 |
iv := cipher randomInitializationVector. |
|
227 |
cipherText := cipher encrypt:'Hello world, here is the alien from Mars and 1234567890' asByteArray. |
|
228 |
self information:cipherText printString. |
|
229 |
||
230 |
cipher initializationVector:iv. |
|
231 |
self information:(cipher decrypt:cipherText) asString. |
|
232 |
" |
|
233 |
! |
|
234 |
||
12145 | 235 |
digestMessage:aStringOrByteArrayOrStream |
236 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
237 |
! |
|
238 |
||
23381 | 239 |
hashValueHexString:aString |
240 |
| hash | |
|
241 |
||
242 |
hash := self hashValueOf:aString. |
|
243 |
^ hash hexPrintString asLowercase |
|
244 |
||
245 |
" |
|
246 |
MD5Stream hashValueHexString:'abc' |
|
247 |
" |
|
248 |
||
249 |
"Created: / 23-09-2018 / 03:44:21 / Claus Gittinger" |
|
250 |
! |
|
251 |
||
7042 | 252 |
hashValueOf:aStringOrByteArrayOrStream |
253 |
|hashStream| |
|
254 |
||
255 |
hashStream := self new. |
|
13929 | 256 |
aStringOrByteArrayOrStream isStream ifTrue:[ |
257 |
aStringOrByteArrayOrStream copyToEndInto:hashStream. |
|
258 |
] ifFalse:[ |
|
259 |
hashStream nextPutAll:aStringOrByteArrayOrStream. |
|
260 |
]. |
|
7042 | 261 |
|
262 |
^ hashStream hashValue |
|
7014 | 263 |
|
264 |
" |
|
265 |
MD5Stream hashValueOf:'BlaBlaBla' |
|
12146 | 266 |
MD5Stream hashValueOf:('makefile' asFilename readStream) |
7042 | 267 |
MD5Stream hashValueOf:('BlaBlaBla' readStream) |
7014 | 268 |
" |
13929 | 269 |
|
270 |
"Modified: / 12-01-2012 / 12:14:44 / cg" |
|
7014 | 271 |
! |
272 |
||
6864 | 273 |
hashValueOfFile:aFilename |
12146 | 274 |
|hash| |
7042 | 275 |
|
12146 | 276 |
aFilename asFilename readingFileDo:[:readStream| |
277 |
readStream binary. |
|
278 |
hash := self hashValueOf:readStream. |
|
7042 | 279 |
]. |
12146 | 280 |
^ hash |
7014 | 281 |
|
282 |
" |
|
12146 | 283 |
MD5Stream hashValueOfFile:'makefile' |
7014 | 284 |
" |
6864 | 285 |
! ! |
286 |
||
22512 | 287 |
!HashStream methodsFor:'Compatibility-Squeak'! |
288 |
||
289 |
digestMessage:bytes |
|
290 |
"SQUEAK: answer the digest of bytes" |
|
291 |
||
292 |
^ self hashValueOf:bytes |
|
22564 | 293 |
! |
294 |
||
295 |
hashMessage:bytes |
|
296 |
"SQUEAK: answer the digest of bytes" |
|
297 |
||
298 |
^ self hashValueOf:bytes |
|
22512 | 299 |
! ! |
300 |
||
6864 | 301 |
!HashStream methodsFor:'accessing'! |
302 |
||
303 |
contents |
|
304 |
"return the entire contents of the stream |
|
305 |
- this is our hashValue." |
|
306 |
||
307 |
^ self hashValue |
|
308 |
||
309 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
310 |
! ! |
|
311 |
||
312 |
!HashStream methodsFor:'not implemented'! |
|
313 |
||
314 |
next |
|
13904 | 315 |
"I can only write" |
316 |
||
6864 | 317 |
^ self shouldNotImplement |
318 |
||
13904 | 319 |
"Created: / 17-03-1999 / 15:11:03 / stefan" |
320 |
"Modified (comment): / 09-01-2012 / 16:55:28 / cg" |
|
6864 | 321 |
! ! |
322 |
||
12142 | 323 |
!HashStream methodsFor:'operations'! |
324 |
||
19165 | 325 |
hashValueOf:bytes |
12142 | 326 |
"answer the digest of bytes" |
327 |
||
19165 | 328 |
^ self |
329 |
reset; |
|
330 |
nextPutAll:bytes; |
|
331 |
hashValue. |
|
12142 | 332 |
|
333 |
" |
|
334 |
SHA1Stream new |
|
19165 | 335 |
hashValueOf:'123456789abcdefg'; |
336 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
337 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
338 |
(SHA1Stream new hmac key:'123456') |
19165 | 339 |
hashValueOf:'123456789abcdefg'; |
340 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
341 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
342 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
343 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
344 |
contents |
12142 | 345 |
" |
346 |
! |
|
347 |
||
348 |
reset |
|
349 |
"initialize to a clean state" |
|
350 |
||
351 |
^ self subclassResponsibility |
|
352 |
! ! |
|
353 |
||
6864 | 354 |
!HashStream methodsFor:'queries'! |
355 |
||
356 |
blockSize |
|
357 |
"the class knows about the basic block size" |
|
358 |
||
359 |
^ self class blockSize |
|
360 |
||
361 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
362 |
! |
|
363 |
||
15622
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
364 |
hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
365 |
"the class knows about the basic block size" |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
366 |
|
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
367 |
^ self class hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
368 |
! |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
369 |
|
6864 | 370 |
hashSize |
13928 | 371 |
"return the size of the returned hashvalue (in bytes)" |
372 |
||
6864 | 373 |
"the class knows about the basic hash size" |
374 |
^ self class hashSize |
|
375 |
||
13928 | 376 |
"Created: / 18-03-1999 / 10:17:12 / stefan" |
377 |
"Modified: / 15-10-1999 / 11:53:20 / stefan" |
|
378 |
"Modified (comment): / 11-01-2012 / 10:12:09 / cg" |
|
6864 | 379 |
! |
380 |
||
7251 | 381 |
hashValue |
13969 | 382 |
"return the value of the computed hash" |
7251 | 383 |
|
384 |
^ self subclassResponsibility |
|
13969 | 385 |
|
386 |
"Modified (comment): / 23-01-2012 / 10:01:20 / cg" |
|
387 |
! |
|
388 |
||
6864 | 389 |
isReadable |
19439 | 390 |
"return true, if reading is supported by the receiver. |
6864 | 391 |
Always return false here" |
392 |
||
393 |
^ false |
|
394 |
||
395 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
396 |
! |
|
397 |
||
398 |
isWritable |
|
19439 | 399 |
"return true, if writing is supported by the receiver. |
6864 | 400 |
Always return true here" |
401 |
||
402 |
^ true |
|
403 |
||
404 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
405 |
! ! |
|
406 |
||
407 |
!HashStream methodsFor:'testing'! |
|
408 |
||
409 |
atEnd |
|
410 |
"return true if the end of the stream has been reached; |
|
411 |
this is never reached" |
|
412 |
||
413 |
^ false |
|
414 |
||
415 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
416 |
! ! |
|
417 |
||
418 |
!HashStream methodsFor:'writing'! |
|
419 |
||
7042 | 420 |
nextPut:anObject |
421 |
"add the hash of anObject to the computed hash so far. |
|
13903 | 422 |
anObject can be a Character, SmallInteger ByteArray or String" |
423 |
||
424 |
anObject isByteCollection ifTrue:[ |
|
425 |
self nextPutBytes:(anObject byteSize) from:anObject startingAt:1. |
|
426 |
^ self. |
|
427 |
]. |
|
7042 | 428 |
|
13903 | 429 |
anObject isCharacter ifTrue:[ |
19399 | 430 |
"/ only 8bit chars are allowed !! |
431 |
self nextPutBytes:1 from:(ByteArray with:anObject codePoint) startingAt:1. |
|
13903 | 432 |
^ self. |
433 |
]. |
|
434 |
||
435 |
anObject isInteger ifTrue:[ |
|
16869 | 436 |
self nextPutBytes:anObject digitBytes. |
437 |
^ self. |
|
13903 | 438 |
]. |
439 |
||
440 |
self error:'unsupported argument'. |
|
441 |
||
442 |
"Modified (comment): / 09-01-2012 / 16:54:05 / cg" |
|
7042 | 443 |
! |
444 |
||
6864 | 445 |
nextPutAll:aCollection |
19165 | 446 |
"Hash streams handle Strings and ByteArrays in #nextPutBytes:" |
6864 | 447 |
|
11799 | 448 |
aCollection isByteCollection ifTrue:[ |
13900 | 449 |
self nextPutBytes:(aCollection byteSize) from:aCollection startingAt:1. |
6864 | 450 |
] ifFalse:[ |
451 |
super nextPutAll:aCollection |
|
452 |
]. |
|
453 |
||
13900 | 454 |
"Created: / 14-10-1999 / 11:22:50 / stefan" |
455 |
"Modified: / 09-01-2012 / 13:02:44 / cg" |
|
7042 | 456 |
! |
457 |
||
19165 | 458 |
nextPutByte:aByte |
459 |
"add the hash of anObject to the computed hash so far. |
|
460 |
aByte can be a SmallInteger <= 255" |
|
461 |
||
462 |
self nextPutBytes:1 from:(ByteArray with:aByte) startingAt:1. |
|
463 |
! |
|
464 |
||
7042 | 465 |
nextPutBytes:count from:anObject startingAt:start |
466 |
"write count bytes from an object starting at index start. |
|
467 |
Return the number of bytes written. |
|
468 |
The object must have non-pointer indexed instvars |
|
13903 | 469 |
(i.e. be a ByteArray, String, Float- or DoubleArray)." |
7042 | 470 |
|
13903 | 471 |
self subclassResponsibility |
7042 | 472 |
|
13903 | 473 |
"Modified: / 09-01-2012 / 16:41:31 / cg" |
6864 | 474 |
! ! |
475 |
||
476 |
!HashStream class methodsFor:'documentation'! |
|
477 |
||
478 |
version |
|
19165 | 479 |
^ '$Header$' |
12142 | 480 |
! |
481 |
||
482 |
version_CVS |
|
19165 | 483 |
^ '$Header$' |
6864 | 484 |
! ! |
14842 | 485 |