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