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