author | Jan Vrany <jan.vrany@labware.com> |
Wed, 23 Jun 2021 12:50:05 +0100 | |
branch | jv |
changeset 25430 | acd92449dc30 |
parent 19659 | 0f585374259a |
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" |
|
12174 | 141 |
! ! |
142 |
||
12146 | 143 |
!HashStream class methodsFor:'self tests'! |
7042 | 144 |
|
145 |
test |
|
146 |
"test against testVector" |
|
147 |
||
148 |
self testVector do:[:pair | |
|
13924 | 149 |
|data expectedHash expectedHashBytes computedHash hashStream| |
7042 | 150 |
|
151 |
data := pair first. |
|
152 |
expectedHash := pair second. |
|
12753 | 153 |
expectedHash isString ifTrue:[ |
154 |
expectedHashBytes := ByteArray fromHexString:expectedHash |
|
155 |
] ifFalse:[ |
|
156 |
expectedHashBytes := expectedHash |
|
157 |
]. |
|
7042 | 158 |
|
13366 | 159 |
"/ non-stream interface must be implemented by all |
13924 | 160 |
computedHash := self hashValueOf:data. |
161 |
self assert:computedHash = expectedHashBytes message:'Test failed'. |
|
12174 | 162 |
self canStream ifTrue:[ |
163 |
hashStream := self new. |
|
164 |
hashStream nextPut:data. |
|
13924 | 165 |
computedHash := hashStream hashValue. |
166 |
self assert:computedHash = expectedHashBytes message:'Test failed' |
|
7042 | 167 |
]. |
168 |
]. |
|
169 |
||
170 |
" |
|
171 |
MD5Stream test. |
|
172 |
SHA1Stream test. |
|
173 |
RipeMD160Stream test. |
|
174 |
" |
|
13924 | 175 |
|
176 |
"Modified: / 10-01-2012 / 22:35:17 / cg" |
|
7042 | 177 |
! |
178 |
||
179 |
testVector |
|
13918 | 180 |
"/ obsolete - moved to TestHashAlgorithms unit test. |
7042 | 181 |
|
182 |
^ self subclassResponsibility |
|
13918 | 183 |
|
184 |
"Modified (comment): / 09-01-2012 / 21:49:59 / cg" |
|
7042 | 185 |
! ! |
186 |
||
7015 | 187 |
!HashStream class methodsFor:'utilities'! |
188 |
||
19644 | 189 |
cryptBlock:aStringOrByteArray from:srcIdx to:srcEndOrNil into:resultOrNil startingAt:dstIdx encrypt:encryptMode |
15621 | 190 |
"one-way encryption of aStringOrByteArray. |
19644 | 191 |
Used when a HashStream is used as the block cipher with OFB or CTR mode. |
15621 | 192 |
|
193 |
encryptMode is ignored here." |
|
194 |
||
195 |
|hashValue| |
|
196 |
||
19644 | 197 |
(srcIdx == 1 and:[srcEndOrNil isNil]) ifTrue:[ |
15621 | 198 |
hashValue := self hashValueOf:aStringOrByteArray. |
199 |
] ifFalse:[ |
|
200 |
|bytesToEncrypt| |
|
201 |
||
202 |
bytesToEncrypt := aStringOrByteArray copyFrom:srcIdx to:srcIdx+self hashSize-1. |
|
203 |
hashValue := self hashValueOf:bytesToEncrypt. |
|
204 |
]. |
|
205 |
resultOrNil isNil ifTrue:[ |
|
206 |
^ hashValue. |
|
207 |
] ifFalse:[ |
|
208 |
resultOrNil replaceBytesFrom:dstIdx to:dstIdx+hashValue size-1 with:hashValue startingAt:1. |
|
209 |
^ resultOrNil. |
|
210 |
]. |
|
211 |
||
212 |
" |
|
213 |
|cipher iv cipherText| |
|
214 |
||
215 |
cipher := OfbCipher for:SHA512Stream. |
|
216 |
iv := cipher randomInitializationVector. |
|
217 |
cipherText := cipher encrypt:'Hello world, here is the alien from Mars and 1234567890' asByteArray. |
|
218 |
self information:cipherText printString. |
|
219 |
||
220 |
cipher initializationVector:iv. |
|
221 |
self information:(cipher decrypt:cipherText) asString. |
|
222 |
" |
|
223 |
! |
|
224 |
||
12145 | 225 |
digestMessage:aStringOrByteArrayOrStream |
226 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
227 |
! |
|
228 |
||
7042 | 229 |
hashValueOf:aStringOrByteArrayOrStream |
230 |
|hashStream| |
|
231 |
||
232 |
hashStream := self new. |
|
13929 | 233 |
aStringOrByteArrayOrStream isStream ifTrue:[ |
234 |
aStringOrByteArrayOrStream copyToEndInto:hashStream. |
|
235 |
] ifFalse:[ |
|
236 |
hashStream nextPutAll:aStringOrByteArrayOrStream. |
|
237 |
]. |
|
7042 | 238 |
|
239 |
^ hashStream hashValue |
|
7014 | 240 |
|
241 |
" |
|
242 |
MD5Stream hashValueOf:'BlaBlaBla' |
|
12146 | 243 |
MD5Stream hashValueOf:('makefile' asFilename readStream) |
7042 | 244 |
MD5Stream hashValueOf:('BlaBlaBla' readStream) |
7014 | 245 |
" |
13929 | 246 |
|
247 |
"Modified: / 12-01-2012 / 12:14:44 / cg" |
|
7014 | 248 |
! |
249 |
||
6864 | 250 |
hashValueOfFile:aFilename |
12146 | 251 |
|hash| |
7042 | 252 |
|
12146 | 253 |
aFilename asFilename readingFileDo:[:readStream| |
254 |
readStream binary. |
|
255 |
hash := self hashValueOf:readStream. |
|
7042 | 256 |
]. |
12146 | 257 |
^ hash |
7014 | 258 |
|
259 |
" |
|
12146 | 260 |
MD5Stream hashValueOfFile:'makefile' |
7014 | 261 |
" |
6864 | 262 |
! ! |
263 |
||
264 |
!HashStream methodsFor:'accessing'! |
|
265 |
||
266 |
contents |
|
267 |
"return the entire contents of the stream |
|
268 |
- this is our hashValue." |
|
269 |
||
270 |
^ self hashValue |
|
271 |
||
272 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
273 |
! ! |
|
274 |
||
19165 | 275 |
!HashStream methodsFor:'compatibility - squeak'! |
276 |
||
277 |
digestMessage:bytes |
|
278 |
"SQUEAK: answer the digest of bytes" |
|
279 |
||
280 |
^ self hashValueOf:bytes |
|
281 |
! ! |
|
282 |
||
6864 | 283 |
!HashStream methodsFor:'not implemented'! |
284 |
||
285 |
next |
|
13904 | 286 |
"I can only write" |
287 |
||
6864 | 288 |
^ self shouldNotImplement |
289 |
||
13904 | 290 |
"Created: / 17-03-1999 / 15:11:03 / stefan" |
291 |
"Modified (comment): / 09-01-2012 / 16:55:28 / cg" |
|
6864 | 292 |
! ! |
293 |
||
12142 | 294 |
!HashStream methodsFor:'operations'! |
295 |
||
19165 | 296 |
hashValueOf:bytes |
12142 | 297 |
"answer the digest of bytes" |
298 |
||
19165 | 299 |
^ self |
300 |
reset; |
|
301 |
nextPutAll:bytes; |
|
302 |
hashValue. |
|
12142 | 303 |
|
304 |
" |
|
305 |
SHA1Stream new |
|
19165 | 306 |
hashValueOf:'123456789abcdefg'; |
307 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
308 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
309 |
(SHA1Stream new hmac key:'123456') |
19165 | 310 |
hashValueOf:'123456789abcdefg'; |
311 |
hashValueOf:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
312 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
313 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
314 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
315 |
contents |
12142 | 316 |
" |
317 |
! |
|
318 |
||
319 |
reset |
|
320 |
"initialize to a clean state" |
|
321 |
||
322 |
^ self subclassResponsibility |
|
323 |
! ! |
|
324 |
||
6864 | 325 |
!HashStream methodsFor:'queries'! |
326 |
||
327 |
blockSize |
|
328 |
"the class knows about the basic block size" |
|
329 |
||
330 |
^ self class blockSize |
|
331 |
||
332 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
333 |
! |
|
334 |
||
15622
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
335 |
hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
336 |
"the class knows about the basic block size" |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
337 |
|
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
338 |
^ self class hashBlockSize |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
339 |
! |
79d968fa9f05
Rename #blockSize to #hashBlockSize
Stefan Vogel <sv@exept.de>
parents:
15621
diff
changeset
|
340 |
|
6864 | 341 |
hashSize |
13928 | 342 |
"return the size of the returned hashvalue (in bytes)" |
343 |
||
6864 | 344 |
"the class knows about the basic hash size" |
345 |
^ self class hashSize |
|
346 |
||
13928 | 347 |
"Created: / 18-03-1999 / 10:17:12 / stefan" |
348 |
"Modified: / 15-10-1999 / 11:53:20 / stefan" |
|
349 |
"Modified (comment): / 11-01-2012 / 10:12:09 / cg" |
|
6864 | 350 |
! |
351 |
||
7251 | 352 |
hashValue |
13969 | 353 |
"return the value of the computed hash" |
7251 | 354 |
|
355 |
^ self subclassResponsibility |
|
13969 | 356 |
|
357 |
"Modified (comment): / 23-01-2012 / 10:01:20 / cg" |
|
358 |
! |
|
359 |
||
6864 | 360 |
isReadable |
19439 | 361 |
"return true, if reading is supported by the receiver. |
6864 | 362 |
Always return false here" |
363 |
||
364 |
^ false |
|
365 |
||
366 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
367 |
! |
|
368 |
||
369 |
isWritable |
|
19439 | 370 |
"return true, if writing is supported by the receiver. |
6864 | 371 |
Always return true here" |
372 |
||
373 |
^ true |
|
374 |
||
375 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
376 |
! ! |
|
377 |
||
378 |
!HashStream methodsFor:'testing'! |
|
379 |
||
380 |
atEnd |
|
381 |
"return true if the end of the stream has been reached; |
|
382 |
this is never reached" |
|
383 |
||
384 |
^ false |
|
385 |
||
386 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
387 |
! ! |
|
388 |
||
389 |
!HashStream methodsFor:'writing'! |
|
390 |
||
7042 | 391 |
nextPut:anObject |
392 |
"add the hash of anObject to the computed hash so far. |
|
13903 | 393 |
anObject can be a Character, SmallInteger ByteArray or String" |
394 |
||
395 |
anObject isByteCollection ifTrue:[ |
|
396 |
self nextPutBytes:(anObject byteSize) from:anObject startingAt:1. |
|
397 |
^ self. |
|
398 |
]. |
|
7042 | 399 |
|
13903 | 400 |
anObject isCharacter ifTrue:[ |
19399 | 401 |
"/ only 8bit chars are allowed !! |
402 |
self nextPutBytes:1 from:(ByteArray with:anObject codePoint) startingAt:1. |
|
13903 | 403 |
^ self. |
404 |
]. |
|
405 |
||
406 |
anObject isInteger ifTrue:[ |
|
16869 | 407 |
self nextPutBytes:anObject digitBytes. |
408 |
^ self. |
|
13903 | 409 |
]. |
410 |
||
411 |
self error:'unsupported argument'. |
|
412 |
||
413 |
"Modified (comment): / 09-01-2012 / 16:54:05 / cg" |
|
7042 | 414 |
! |
415 |
||
6864 | 416 |
nextPutAll:aCollection |
19165 | 417 |
"Hash streams handle Strings and ByteArrays in #nextPutBytes:" |
6864 | 418 |
|
11799 | 419 |
aCollection isByteCollection ifTrue:[ |
13900 | 420 |
self nextPutBytes:(aCollection byteSize) from:aCollection startingAt:1. |
6864 | 421 |
] ifFalse:[ |
422 |
super nextPutAll:aCollection |
|
423 |
]. |
|
424 |
||
13900 | 425 |
"Created: / 14-10-1999 / 11:22:50 / stefan" |
426 |
"Modified: / 09-01-2012 / 13:02:44 / cg" |
|
7042 | 427 |
! |
428 |
||
19165 | 429 |
nextPutByte:aByte |
430 |
"add the hash of anObject to the computed hash so far. |
|
431 |
aByte can be a SmallInteger <= 255" |
|
432 |
||
433 |
self nextPutBytes:1 from:(ByteArray with:aByte) startingAt:1. |
|
434 |
! |
|
435 |
||
7042 | 436 |
nextPutBytes:count from:anObject startingAt:start |
437 |
"write count bytes from an object starting at index start. |
|
438 |
Return the number of bytes written. |
|
439 |
The object must have non-pointer indexed instvars |
|
13903 | 440 |
(i.e. be a ByteArray, String, Float- or DoubleArray)." |
7042 | 441 |
|
13903 | 442 |
self subclassResponsibility |
7042 | 443 |
|
13903 | 444 |
"Modified: / 09-01-2012 / 16:41:31 / cg" |
6864 | 445 |
! ! |
446 |
||
447 |
!HashStream class methodsFor:'documentation'! |
|
448 |
||
449 |
version |
|
19165 | 450 |
^ '$Header$' |
12142 | 451 |
! |
452 |
||
453 |
version_CVS |
|
19165 | 454 |
^ '$Header$' |
6864 | 455 |
! ! |
14842 | 456 |