author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 13 Mar 2013 00:42:41 +0000 | |
branch | jv |
changeset 18028 | e39da2aa21bc |
parent 18011 | deb0c3355881 |
parent 14842 | 8f7ca0be2aed |
child 18084 | ab5b38bd8f81 |
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 |
||
113 |
canStream |
|
114 |
"simple hash functions (squeak-ported) cannot stream. |
|
115 |
Use hashFunction: there" |
|
116 |
||
117 |
^ true |
|
13889 | 118 |
! |
119 |
||
120 |
hashSize |
|
13928 | 121 |
"return the size of the hashvalue returned by instances of this class (in bytes)" |
122 |
||
13889 | 123 |
self subclassResponsibility |
124 |
||
125 |
"Created: / 04-01-2012 / 19:22:32 / cg" |
|
12174 | 126 |
! ! |
127 |
||
12146 | 128 |
!HashStream class methodsFor:'self tests'! |
7042 | 129 |
|
130 |
test |
|
131 |
"test against testVector" |
|
132 |
||
133 |
self testVector do:[:pair | |
|
13924 | 134 |
|data expectedHash expectedHashBytes computedHash hashStream| |
7042 | 135 |
|
136 |
data := pair first. |
|
137 |
expectedHash := pair second. |
|
12753 | 138 |
expectedHash isString ifTrue:[ |
139 |
expectedHashBytes := ByteArray fromHexString:expectedHash |
|
140 |
] ifFalse:[ |
|
141 |
expectedHashBytes := expectedHash |
|
142 |
]. |
|
7042 | 143 |
|
13366 | 144 |
"/ non-stream interface must be implemented by all |
13924 | 145 |
computedHash := self hashValueOf:data. |
146 |
self assert:computedHash = expectedHashBytes message:'Test failed'. |
|
12174 | 147 |
self canStream ifTrue:[ |
148 |
hashStream := self new. |
|
149 |
hashStream nextPut:data. |
|
13924 | 150 |
computedHash := hashStream hashValue. |
151 |
self assert:computedHash = expectedHashBytes message:'Test failed' |
|
7042 | 152 |
]. |
153 |
]. |
|
154 |
||
155 |
" |
|
156 |
MD5Stream test. |
|
157 |
SHA1Stream test. |
|
158 |
RipeMD160Stream test. |
|
159 |
" |
|
13924 | 160 |
|
161 |
"Modified: / 10-01-2012 / 22:35:17 / cg" |
|
7042 | 162 |
! |
163 |
||
164 |
testVector |
|
13918 | 165 |
"/ obsolete - moved to TestHashAlgorithms unit test. |
7042 | 166 |
|
167 |
^ self subclassResponsibility |
|
13918 | 168 |
|
169 |
"Modified (comment): / 09-01-2012 / 21:49:59 / cg" |
|
7042 | 170 |
! ! |
171 |
||
7015 | 172 |
!HashStream class methodsFor:'utilities'! |
173 |
||
12145 | 174 |
digestMessage:aStringOrByteArrayOrStream |
175 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
176 |
! |
|
177 |
||
7042 | 178 |
hashValueOf:aStringOrByteArrayOrStream |
179 |
|hashStream| |
|
180 |
||
181 |
hashStream := self new. |
|
13929 | 182 |
aStringOrByteArrayOrStream isStream ifTrue:[ |
183 |
aStringOrByteArrayOrStream copyToEndInto:hashStream. |
|
184 |
] ifFalse:[ |
|
185 |
hashStream nextPutAll:aStringOrByteArrayOrStream. |
|
186 |
]. |
|
7042 | 187 |
|
188 |
^ hashStream hashValue |
|
7014 | 189 |
|
190 |
" |
|
191 |
MD5Stream hashValueOf:'BlaBlaBla' |
|
12146 | 192 |
MD5Stream hashValueOf:('makefile' asFilename readStream) |
7042 | 193 |
MD5Stream hashValueOf:('BlaBlaBla' readStream) |
7014 | 194 |
" |
13929 | 195 |
|
196 |
"Modified: / 12-01-2012 / 12:14:44 / cg" |
|
7014 | 197 |
! |
198 |
||
6864 | 199 |
hashValueOfFile:aFilename |
12146 | 200 |
|hash| |
7042 | 201 |
|
12146 | 202 |
aFilename asFilename readingFileDo:[:readStream| |
203 |
readStream binary. |
|
204 |
hash := self hashValueOf:readStream. |
|
7042 | 205 |
]. |
12146 | 206 |
^ hash |
7014 | 207 |
|
208 |
" |
|
12146 | 209 |
MD5Stream hashValueOfFile:'makefile' |
7014 | 210 |
" |
6864 | 211 |
! ! |
212 |
||
213 |
!HashStream methodsFor:'accessing'! |
|
214 |
||
215 |
contents |
|
216 |
"return the entire contents of the stream |
|
217 |
- this is our hashValue." |
|
218 |
||
219 |
^ self hashValue |
|
220 |
||
221 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
222 |
! ! |
|
223 |
||
224 |
!HashStream methodsFor:'not implemented'! |
|
225 |
||
226 |
next |
|
13904 | 227 |
"I can only write" |
228 |
||
6864 | 229 |
^ self shouldNotImplement |
230 |
||
13904 | 231 |
"Created: / 17-03-1999 / 15:11:03 / stefan" |
232 |
"Modified (comment): / 09-01-2012 / 16:55:28 / cg" |
|
6864 | 233 |
! ! |
234 |
||
12142 | 235 |
!HashStream methodsFor:'operations'! |
236 |
||
237 |
digestMessage:bytes |
|
238 |
"answer the digest of bytes" |
|
239 |
||
240 |
self reset. |
|
241 |
self nextPutAll:bytes. |
|
242 |
||
14842 | 243 |
^ self hashValue. |
12142 | 244 |
|
245 |
" |
|
246 |
SHA1Stream new |
|
247 |
digestMessage:'123456789abcdefg'; |
|
248 |
digestMessage:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
249 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
250 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
251 |
digestMessage:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
252 |
digestMessage:'123456789abcdefg' |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
253 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
254 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
255 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
256 |
contents |
12142 | 257 |
" |
258 |
! |
|
259 |
||
260 |
reset |
|
261 |
"initialize to a clean state" |
|
262 |
||
263 |
^ self subclassResponsibility |
|
264 |
! ! |
|
265 |
||
6864 | 266 |
!HashStream methodsFor:'queries'! |
267 |
||
268 |
blockSize |
|
269 |
"the class knows about the basic block size" |
|
270 |
||
271 |
^ self class blockSize |
|
272 |
||
273 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
274 |
! |
|
275 |
||
276 |
hashSize |
|
13928 | 277 |
"return the size of the returned hashvalue (in bytes)" |
278 |
||
6864 | 279 |
"the class knows about the basic hash size" |
280 |
^ self class hashSize |
|
281 |
||
13928 | 282 |
"Created: / 18-03-1999 / 10:17:12 / stefan" |
283 |
"Modified: / 15-10-1999 / 11:53:20 / stefan" |
|
284 |
"Modified (comment): / 11-01-2012 / 10:12:09 / cg" |
|
6864 | 285 |
! |
286 |
||
7251 | 287 |
hashValue |
13969 | 288 |
"return the value of the computed hash" |
7251 | 289 |
|
290 |
^ self subclassResponsibility |
|
13969 | 291 |
|
292 |
"Modified (comment): / 23-01-2012 / 10:01:20 / cg" |
|
293 |
! |
|
294 |
||
295 |
hashValueOf:aString |
|
296 |
"for convenience: |
|
297 |
return the value of the computed hash of aString" |
|
298 |
||
299 |
self reset. |
|
300 |
self nextPutAll:aString. |
|
301 |
^ self hashValue |
|
302 |
||
303 |
"Created: / 18-01-2012 / 17:26:25 / cg" |
|
7251 | 304 |
! |
305 |
||
6864 | 306 |
isReadable |
307 |
"return true, if reading is supported by the recevier. |
|
308 |
Always return false here" |
|
309 |
||
310 |
^ false |
|
311 |
||
312 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
313 |
! |
|
314 |
||
315 |
isWritable |
|
316 |
"return true, if writing is supported by the recevier. |
|
317 |
Always return true here" |
|
318 |
||
319 |
^ true |
|
320 |
||
321 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
322 |
! ! |
|
323 |
||
324 |
!HashStream methodsFor:'testing'! |
|
325 |
||
326 |
atEnd |
|
327 |
"return true if the end of the stream has been reached; |
|
328 |
this is never reached" |
|
329 |
||
330 |
^ false |
|
331 |
||
332 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
333 |
! ! |
|
334 |
||
335 |
!HashStream methodsFor:'writing'! |
|
336 |
||
7042 | 337 |
nextPut:anObject |
338 |
"add the hash of anObject to the computed hash so far. |
|
13903 | 339 |
anObject can be a Character, SmallInteger ByteArray or String" |
340 |
||
341 |
anObject isByteCollection ifTrue:[ |
|
342 |
self nextPutBytes:(anObject byteSize) from:anObject startingAt:1. |
|
343 |
^ self. |
|
344 |
]. |
|
7042 | 345 |
|
13903 | 346 |
anObject isCharacter ifTrue:[ |
347 |
"/ only ascii allowed !! |
|
348 |
self nextPutBytes:(ByteArray with:anObject codePoint). |
|
349 |
^ self. |
|
350 |
]. |
|
351 |
||
352 |
anObject isInteger ifTrue:[ |
|
353 |
"/ only 0..255 allowed !! |
|
354 |
anObject < 256 ifTrue:[ |
|
355 |
self nextPutAll:(ByteArray with:anObject). |
|
356 |
^ self. |
|
357 |
]. |
|
358 |
]. |
|
359 |
||
360 |
self error:'unsupported argument'. |
|
361 |
||
362 |
"Modified (comment): / 09-01-2012 / 16:54:05 / cg" |
|
7042 | 363 |
! |
364 |
||
6864 | 365 |
nextPutAll:aCollection |
366 |
"Hash streams handle Strings and ByteArrays in nextPut:" |
|
367 |
||
11799 | 368 |
aCollection isByteCollection ifTrue:[ |
13900 | 369 |
self nextPutBytes:(aCollection byteSize) from:aCollection startingAt:1. |
6864 | 370 |
] ifFalse:[ |
371 |
super nextPutAll:aCollection |
|
372 |
]. |
|
373 |
||
13900 | 374 |
"Created: / 14-10-1999 / 11:22:50 / stefan" |
375 |
"Modified: / 09-01-2012 / 13:02:44 / cg" |
|
7042 | 376 |
! |
377 |
||
378 |
nextPutBytes:count from:anObject startingAt:start |
|
379 |
"write count bytes from an object starting at index start. |
|
380 |
Return the number of bytes written. |
|
381 |
The object must have non-pointer indexed instvars |
|
13903 | 382 |
(i.e. be a ByteArray, String, Float- or DoubleArray)." |
7042 | 383 |
|
13903 | 384 |
self subclassResponsibility |
7042 | 385 |
|
13903 | 386 |
"Modified: / 09-01-2012 / 16:41:31 / cg" |
6864 | 387 |
! ! |
388 |
||
389 |
!HashStream class methodsFor:'documentation'! |
|
390 |
||
391 |
version |
|
14842 | 392 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.27 2013-03-08 23:37:43 stefan Exp $' |
12142 | 393 |
! |
394 |
||
395 |
version_CVS |
|
14842 | 396 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.27 2013-03-08 23:37:43 stefan Exp $' |
6864 | 397 |
! ! |
14842 | 398 |