author | Claus Gittinger <cg@exept.de> |
Mon, 09 Jan 2012 16:54:24 +0100 | |
changeset 13903 | f2b227a2fcaa |
parent 13901 | 318c9d7e0c1a |
child 13904 | 1aacd72f4414 |
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 |
" |
|
40 |
Abstract class. Subclasses generate hash values used as checksums |
|
41 |
or for generating cryptographic signatures. |
|
42 |
||
43 |
[author:] |
|
44 |
Stefan Vogel |
|
45 |
||
46 |
[see also:] |
|
47 |
SHA1Stream MD5Stream |
|
48 |
" |
|
49 |
! ! |
|
50 |
||
51 |
!HashStream class methodsFor:'instance creation'! |
|
52 |
||
7015 | 53 |
new |
54 |
"have to re-allow new - it was disabled in Stream" |
|
55 |
^ self basicNew initialize |
|
56 |
||
57 |
||
58 |
! |
|
59 |
||
60 |
random |
|
61 |
"create a random number generator using myself" |
|
62 |
||
63 |
^ HashRandom with:self |
|
64 |
||
65 |
" |
|
66 |
SHA1Stream random next |
|
67 |
" |
|
68 |
||
69 |
"Modified: / 12.11.1999 / 17:21:17 / stefan" |
|
70 |
! ! |
|
71 |
||
12145 | 72 |
!HashStream class methodsFor:'compatibility - squeak'! |
73 |
||
74 |
hashMessage:aStringOrByteArrayOrStream |
|
12146 | 75 |
"SQUEAK compatibility |
76 |
- but this is a bad choice - squeak uses #digestMessage: at the instance side" |
|
12145 | 77 |
|
78 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
79 |
! ! |
|
80 |
||
12174 | 81 |
!HashStream class methodsFor:'queries'! |
82 |
||
83 |
canStream |
|
84 |
"simple hash functions (squeak-ported) cannot stream. |
|
85 |
Use hashFunction: there" |
|
86 |
||
87 |
^ true |
|
13889 | 88 |
! |
89 |
||
90 |
hashSize |
|
91 |
self subclassResponsibility |
|
92 |
||
93 |
"Created: / 04-01-2012 / 19:22:32 / cg" |
|
12174 | 94 |
! ! |
95 |
||
12146 | 96 |
!HashStream class methodsFor:'self tests'! |
7042 | 97 |
|
98 |
test |
|
99 |
"test against testVector" |
|
100 |
||
101 |
self testVector do:[:pair | |
|
12753 | 102 |
|data expectedHash expectedHashBytes hashStream| |
7042 | 103 |
|
104 |
data := pair first. |
|
105 |
expectedHash := pair second. |
|
12753 | 106 |
expectedHash isString ifTrue:[ |
107 |
expectedHashBytes := ByteArray fromHexString:expectedHash |
|
108 |
] ifFalse:[ |
|
109 |
expectedHashBytes := expectedHash |
|
110 |
]. |
|
7042 | 111 |
|
13366 | 112 |
"/ non-stream interface must be implemented by all |
113 |
self assert:(self hashValueOf:data) = expectedHashBytes message:'Test failed'. |
|
12174 | 114 |
self canStream ifTrue:[ |
115 |
hashStream := self new. |
|
116 |
hashStream nextPut:data. |
|
13366 | 117 |
self assert:hashStream hashValue = expectedHashBytes message:'Test failed' |
7042 | 118 |
]. |
119 |
]. |
|
120 |
||
121 |
" |
|
122 |
MD5Stream test. |
|
123 |
SHA1Stream test. |
|
124 |
RipeMD160Stream test. |
|
125 |
" |
|
126 |
! |
|
127 |
||
128 |
testVector |
|
129 |
"define a testvector to test the implementation" |
|
130 |
||
131 |
^ self subclassResponsibility |
|
132 |
! ! |
|
133 |
||
7015 | 134 |
!HashStream class methodsFor:'utilities'! |
135 |
||
12145 | 136 |
digestMessage:aStringOrByteArrayOrStream |
137 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
138 |
! |
|
139 |
||
7042 | 140 |
hashValueOf:aStringOrByteArrayOrStream |
141 |
|hashStream| |
|
142 |
||
143 |
hashStream := self new. |
|
144 |
aStringOrByteArrayOrStream readStream copyToEndInto:hashStream. |
|
145 |
||
146 |
^ hashStream hashValue |
|
7014 | 147 |
|
148 |
" |
|
149 |
MD5Stream hashValueOf:'BlaBlaBla' |
|
12146 | 150 |
MD5Stream hashValueOf:('makefile' asFilename readStream) |
7042 | 151 |
MD5Stream hashValueOf:('BlaBlaBla' readStream) |
7014 | 152 |
" |
153 |
! |
|
154 |
||
6864 | 155 |
hashValueOfFile:aFilename |
12146 | 156 |
|hash| |
7042 | 157 |
|
12146 | 158 |
aFilename asFilename readingFileDo:[:readStream| |
159 |
readStream binary. |
|
160 |
hash := self hashValueOf:readStream. |
|
7042 | 161 |
]. |
12146 | 162 |
^ hash |
7014 | 163 |
|
164 |
" |
|
12146 | 165 |
MD5Stream hashValueOfFile:'makefile' |
7014 | 166 |
" |
6864 | 167 |
! ! |
168 |
||
169 |
!HashStream methodsFor:'accessing'! |
|
170 |
||
171 |
contents |
|
172 |
"return the entire contents of the stream |
|
173 |
- this is our hashValue." |
|
174 |
||
175 |
^ self hashValue |
|
176 |
||
177 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
178 |
! ! |
|
179 |
||
180 |
!HashStream methodsFor:'not implemented'! |
|
181 |
||
182 |
next |
|
183 |
^ self shouldNotImplement |
|
184 |
||
185 |
"Created: / 17.3.1999 / 15:11:03 / stefan" |
|
186 |
! ! |
|
187 |
||
12142 | 188 |
!HashStream methodsFor:'operations'! |
189 |
||
190 |
digestMessage:bytes |
|
191 |
"answer the digest of bytes" |
|
192 |
||
193 |
self reset. |
|
194 |
self nextPutAll:bytes. |
|
195 |
||
196 |
^ self contents. |
|
197 |
||
198 |
" |
|
199 |
SHA1Stream new |
|
200 |
digestMessage:'123456789abcdefg'; |
|
201 |
digestMessage:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
202 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
203 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
204 |
digestMessage:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
205 |
digestMessage:'123456789abcdefg' |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
206 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
207 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
208 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
209 |
contents |
12142 | 210 |
" |
211 |
! |
|
212 |
||
213 |
hmac |
|
214 |
"answer a hmac stream with myself" |
|
215 |
||
216 |
^ HmacStream on:self |
|
217 |
||
218 |
" |
|
13901 | 219 |
(SHA1Stream new hmac key:'exampleKey') digestMessage:'message to generate MAC of' |
12142 | 220 |
" |
13901 | 221 |
|
222 |
"Modified (comment): / 09-01-2012 / 13:51:01 / cg" |
|
12142 | 223 |
! |
224 |
||
225 |
reset |
|
226 |
"initialize to a clean state" |
|
227 |
||
228 |
^ self subclassResponsibility |
|
229 |
! ! |
|
230 |
||
6864 | 231 |
!HashStream methodsFor:'queries'! |
232 |
||
233 |
blockSize |
|
234 |
"the class knows about the basic block size" |
|
235 |
||
236 |
^ self class blockSize |
|
237 |
||
238 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
239 |
! |
|
240 |
||
241 |
hashSize |
|
242 |
"the class knows about the basic hash size" |
|
243 |
||
244 |
^ self class hashSize |
|
245 |
||
246 |
"Created: / 18.3.1999 / 10:17:12 / stefan" |
|
247 |
"Modified: / 15.10.1999 / 11:53:20 / stefan" |
|
248 |
! |
|
249 |
||
7251 | 250 |
hashValue |
251 |
"retunr the value of the computeted hash" |
|
252 |
||
253 |
^ self subclassResponsibility |
|
254 |
! |
|
255 |
||
6864 | 256 |
isReadable |
257 |
"return true, if reading is supported by the recevier. |
|
258 |
Always return false here" |
|
259 |
||
260 |
^ false |
|
261 |
||
262 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
263 |
! |
|
264 |
||
265 |
isWritable |
|
266 |
"return true, if writing is supported by the recevier. |
|
267 |
Always return true here" |
|
268 |
||
269 |
^ true |
|
270 |
||
271 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
272 |
! ! |
|
273 |
||
274 |
!HashStream methodsFor:'testing'! |
|
275 |
||
276 |
atEnd |
|
277 |
"return true if the end of the stream has been reached; |
|
278 |
this is never reached" |
|
279 |
||
280 |
^ false |
|
281 |
||
282 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
283 |
! ! |
|
284 |
||
285 |
!HashStream methodsFor:'writing'! |
|
286 |
||
7042 | 287 |
nextPut:anObject |
288 |
"add the hash of anObject to the computed hash so far. |
|
13903 | 289 |
anObject can be a Character, SmallInteger ByteArray or String" |
290 |
||
291 |
anObject isByteCollection ifTrue:[ |
|
292 |
self nextPutBytes:(anObject byteSize) from:anObject startingAt:1. |
|
293 |
^ self. |
|
294 |
]. |
|
7042 | 295 |
|
13903 | 296 |
anObject isCharacter ifTrue:[ |
297 |
"/ only ascii allowed !! |
|
298 |
self nextPutBytes:(ByteArray with:anObject codePoint). |
|
299 |
^ self. |
|
300 |
]. |
|
301 |
||
302 |
anObject isInteger ifTrue:[ |
|
303 |
"/ only 0..255 allowed !! |
|
304 |
anObject < 256 ifTrue:[ |
|
305 |
self nextPutAll:(ByteArray with:anObject). |
|
306 |
^ self. |
|
307 |
]. |
|
308 |
]. |
|
309 |
||
310 |
self error:'unsupported argument'. |
|
311 |
||
312 |
"Modified (comment): / 09-01-2012 / 16:54:05 / cg" |
|
7042 | 313 |
! |
314 |
||
6864 | 315 |
nextPutAll:aCollection |
316 |
"Hash streams handle Strings and ByteArrays in nextPut:" |
|
317 |
||
11799 | 318 |
aCollection isByteCollection ifTrue:[ |
13900 | 319 |
self nextPutBytes:(aCollection byteSize) from:aCollection startingAt:1. |
6864 | 320 |
] ifFalse:[ |
321 |
super nextPutAll:aCollection |
|
322 |
]. |
|
323 |
||
13900 | 324 |
"Created: / 14-10-1999 / 11:22:50 / stefan" |
325 |
"Modified: / 09-01-2012 / 13:02:44 / cg" |
|
7042 | 326 |
! |
327 |
||
328 |
nextPutBytes:count from:anObject startingAt:start |
|
329 |
"write count bytes from an object starting at index start. |
|
330 |
Return the number of bytes written. |
|
331 |
The object must have non-pointer indexed instvars |
|
13903 | 332 |
(i.e. be a ByteArray, String, Float- or DoubleArray)." |
7042 | 333 |
|
13903 | 334 |
self subclassResponsibility |
7042 | 335 |
|
13903 | 336 |
"Modified: / 09-01-2012 / 16:41:31 / cg" |
6864 | 337 |
! ! |
338 |
||
339 |
!HashStream class methodsFor:'documentation'! |
|
340 |
||
341 |
version |
|
13903 | 342 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.18 2012-01-09 15:54:24 cg Exp $' |
12142 | 343 |
! |
344 |
||
345 |
version_CVS |
|
13903 | 346 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.18 2012-01-09 15:54:24 cg Exp $' |
6864 | 347 |
! ! |