author | Claus Gittinger <cg@exept.de> |
Fri, 17 Feb 2017 10:25:31 +0100 | |
changeset 21480 | 20b4ddb4ba7a |
parent 21394 | 52c5888621c2 |
child 22512 | eccb82f4c3ad |
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 |
" |
|
13910 | 42 |
Abstract class. |
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 |
|
53 |
Warning: Not all subclasses support the stream mode |
|
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 |
||
12145 | 104 |
!HashStream class methodsFor:'compatibility - squeak'! |
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 |
||
272 |
!HashStream methodsFor:'accessing'! |
|
273 |
||
274 |
contents |
|
275 |
"return the entire contents of the stream |
|
276 |
- this is our hashValue." |
|
277 |
||
278 |
^ self hashValue |
|
279 |
||
280 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
281 |
! ! |
|
282 |
||
19165 | 283 |
!HashStream methodsFor:'compatibility - squeak'! |
284 |
||
285 |
digestMessage:bytes |
|
286 |
"SQUEAK: answer the digest of bytes" |
|
287 |
||
288 |
^ self hashValueOf:bytes |
|
289 |
! ! |
|
290 |
||
6864 | 291 |
!HashStream methodsFor:'not implemented'! |
292 |
||
293 |
next |
|
13904 | 294 |
"I can only write" |
295 |
||
6864 | 296 |
^ self shouldNotImplement |
297 |
||
13904 | 298 |
"Created: / 17-03-1999 / 15:11:03 / stefan" |
299 |
"Modified (comment): / 09-01-2012 / 16:55:28 / cg" |
|
6864 | 300 |
! ! |
301 |
||
12142 | 302 |
!HashStream methodsFor:'operations'! |
303 |
||
19165 | 304 |
hashValueOf:bytes |
12142 | 305 |
"answer the digest of bytes" |
306 |
||
19165 | 307 |
^ self |
308 |
reset; |
|
309 |
nextPutAll:bytes; |
|
310 |
hashValue. |
|
12142 | 311 |
|
312 |
" |
|
313 |
SHA1Stream new |
|
19165 | 314 |
hashValueOf:'123456789abcdefg'; |
315 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
316 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
317 |
(SHA1Stream new hmac key:'123456') |
19165 | 318 |
hashValueOf:'123456789abcdefg'; |
319 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
320 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
321 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
322 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
323 |
contents |
12142 | 324 |
" |
325 |
! |
|
326 |
||
327 |
reset |
|
328 |
"initialize to a clean state" |
|
329 |
||
330 |
^ self subclassResponsibility |
|
331 |
! ! |
|
332 |
||
6864 | 333 |
!HashStream methodsFor:'queries'! |
334 |
||
335 |
blockSize |
|
336 |
"the class knows about the basic block size" |
|
337 |
||
338 |
^ self class blockSize |
|
339 |
||
340 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
341 |
! |
|
342 |
||
15622
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
343 |
hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
344 |
"the class knows about the basic block size" |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
345 |
|
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
346 |
^ self class hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
347 |
! |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
348 |
|
6864 | 349 |
hashSize |
13928 | 350 |
"return the size of the returned hashvalue (in bytes)" |
351 |
||
6864 | 352 |
"the class knows about the basic hash size" |
353 |
^ self class hashSize |
|
354 |
||
13928 | 355 |
"Created: / 18-03-1999 / 10:17:12 / stefan" |
356 |
"Modified: / 15-10-1999 / 11:53:20 / stefan" |
|
357 |
"Modified (comment): / 11-01-2012 / 10:12:09 / cg" |
|
6864 | 358 |
! |
359 |
||
7251 | 360 |
hashValue |
13969 | 361 |
"return the value of the computed hash" |
7251 | 362 |
|
363 |
^ self subclassResponsibility |
|
13969 | 364 |
|
365 |
"Modified (comment): / 23-01-2012 / 10:01:20 / cg" |
|
366 |
! |
|
367 |
||
6864 | 368 |
isReadable |
19439 | 369 |
"return true, if reading is supported by the receiver. |
6864 | 370 |
Always return false here" |
371 |
||
372 |
^ false |
|
373 |
||
374 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
375 |
! |
|
376 |
||
377 |
isWritable |
|
19439 | 378 |
"return true, if writing is supported by the receiver. |
6864 | 379 |
Always return true here" |
380 |
||
381 |
^ true |
|
382 |
||
383 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
384 |
! ! |
|
385 |
||
386 |
!HashStream methodsFor:'testing'! |
|
387 |
||
388 |
atEnd |
|
389 |
"return true if the end of the stream has been reached; |
|
390 |
this is never reached" |
|
391 |
||
392 |
^ false |
|
393 |
||
394 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
395 |
! ! |
|
396 |
||
397 |
!HashStream methodsFor:'writing'! |
|
398 |
||
7042 | 399 |
nextPut:anObject |
400 |
"add the hash of anObject to the computed hash so far. |
|
13903 | 401 |
anObject can be a Character, SmallInteger ByteArray or String" |
402 |
||
403 |
anObject isByteCollection ifTrue:[ |
|
404 |
self nextPutBytes:(anObject byteSize) from:anObject startingAt:1. |
|
405 |
^ self. |
|
406 |
]. |
|
7042 | 407 |
|
13903 | 408 |
anObject isCharacter ifTrue:[ |
19399 | 409 |
"/ only 8bit chars are allowed !! |
410 |
self nextPutBytes:1 from:(ByteArray with:anObject codePoint) startingAt:1. |
|
13903 | 411 |
^ self. |
412 |
]. |
|
413 |
||
414 |
anObject isInteger ifTrue:[ |
|
16869 | 415 |
self nextPutBytes:anObject digitBytes. |
416 |
^ self. |
|
13903 | 417 |
]. |
418 |
||
419 |
self error:'unsupported argument'. |
|
420 |
||
421 |
"Modified (comment): / 09-01-2012 / 16:54:05 / cg" |
|
7042 | 422 |
! |
423 |
||
6864 | 424 |
nextPutAll:aCollection |
19165 | 425 |
"Hash streams handle Strings and ByteArrays in #nextPutBytes:" |
6864 | 426 |
|
11799 | 427 |
aCollection isByteCollection ifTrue:[ |
13900 | 428 |
self nextPutBytes:(aCollection byteSize) from:aCollection startingAt:1. |
6864 | 429 |
] ifFalse:[ |
430 |
super nextPutAll:aCollection |
|
431 |
]. |
|
432 |
||
13900 | 433 |
"Created: / 14-10-1999 / 11:22:50 / stefan" |
434 |
"Modified: / 09-01-2012 / 13:02:44 / cg" |
|
7042 | 435 |
! |
436 |
||
19165 | 437 |
nextPutByte:aByte |
438 |
"add the hash of anObject to the computed hash so far. |
|
439 |
aByte can be a SmallInteger <= 255" |
|
440 |
||
441 |
self nextPutBytes:1 from:(ByteArray with:aByte) startingAt:1. |
|
442 |
! |
|
443 |
||
7042 | 444 |
nextPutBytes:count from:anObject startingAt:start |
445 |
"write count bytes from an object starting at index start. |
|
446 |
Return the number of bytes written. |
|
447 |
The object must have non-pointer indexed instvars |
|
13903 | 448 |
(i.e. be a ByteArray, String, Float- or DoubleArray)." |
7042 | 449 |
|
13903 | 450 |
self subclassResponsibility |
7042 | 451 |
|
13903 | 452 |
"Modified: / 09-01-2012 / 16:41:31 / cg" |
6864 | 453 |
! ! |
454 |
||
455 |
!HashStream class methodsFor:'documentation'! |
|
456 |
||
457 |
version |
|
19165 | 458 |
^ '$Header$' |
12142 | 459 |
! |
460 |
||
461 |
version_CVS |
|
19165 | 462 |
^ '$Header$' |
6864 | 463 |
! ! |
14842 | 464 |