author | Claus Gittinger <cg@exept.de> |
Mon, 02 Aug 2010 12:34:20 +0200 | |
changeset 12978 | 6899c32d29a9 |
parent 12753 | 3e6acf834564 |
child 13366 | 0e1bdf4f4156 |
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 |
|
88 |
! ! |
|
89 |
||
12146 | 90 |
!HashStream class methodsFor:'self tests'! |
7042 | 91 |
|
92 |
test |
|
93 |
"test against testVector" |
|
94 |
||
95 |
self testVector do:[:pair | |
|
12753 | 96 |
|data expectedHash expectedHashBytes hashStream| |
7042 | 97 |
|
98 |
data := pair first. |
|
99 |
expectedHash := pair second. |
|
12753 | 100 |
expectedHash isString ifTrue:[ |
101 |
expectedHashBytes := ByteArray fromHexString:expectedHash |
|
102 |
] ifFalse:[ |
|
103 |
expectedHashBytes := expectedHash |
|
104 |
]. |
|
7042 | 105 |
|
12174 | 106 |
self canStream ifTrue:[ |
107 |
hashStream := self new. |
|
108 |
hashStream nextPut:data. |
|
12753 | 109 |
hashStream hashValue ~= expectedHashBytes ifTrue:[ |
12174 | 110 |
self error:'Test failed' |
111 |
]. |
|
7042 | 112 |
]. |
12174 | 113 |
"/ non-stream interface must be implemented by all |
12753 | 114 |
(self hashValueOf:data) ~= expectedHashBytes ifTrue:[ |
7251 | 115 |
self error:'Test failed' |
7042 | 116 |
]. |
117 |
]. |
|
118 |
||
119 |
" |
|
120 |
MD5Stream test. |
|
121 |
SHA1Stream test. |
|
122 |
RipeMD160Stream test. |
|
123 |
" |
|
124 |
! |
|
125 |
||
126 |
testVector |
|
127 |
"define a testvector to test the implementation" |
|
128 |
||
129 |
^ self subclassResponsibility |
|
130 |
! ! |
|
131 |
||
7015 | 132 |
!HashStream class methodsFor:'utilities'! |
133 |
||
12145 | 134 |
digestMessage:aStringOrByteArrayOrStream |
135 |
^ self hashValueOf:aStringOrByteArrayOrStream |
|
136 |
! |
|
137 |
||
7042 | 138 |
hashValueOf:aStringOrByteArrayOrStream |
139 |
|hashStream| |
|
140 |
||
141 |
hashStream := self new. |
|
142 |
aStringOrByteArrayOrStream readStream copyToEndInto:hashStream. |
|
143 |
||
144 |
^ hashStream hashValue |
|
7014 | 145 |
|
146 |
" |
|
147 |
MD5Stream hashValueOf:'BlaBlaBla' |
|
12146 | 148 |
MD5Stream hashValueOf:('makefile' asFilename readStream) |
7042 | 149 |
MD5Stream hashValueOf:('BlaBlaBla' readStream) |
7014 | 150 |
" |
151 |
! |
|
152 |
||
6864 | 153 |
hashValueOfFile:aFilename |
12146 | 154 |
|hash| |
7042 | 155 |
|
12146 | 156 |
aFilename asFilename readingFileDo:[:readStream| |
157 |
readStream binary. |
|
158 |
hash := self hashValueOf:readStream. |
|
7042 | 159 |
]. |
12146 | 160 |
^ hash |
7014 | 161 |
|
162 |
" |
|
12146 | 163 |
MD5Stream hashValueOfFile:'makefile' |
7014 | 164 |
" |
6864 | 165 |
! ! |
166 |
||
167 |
!HashStream methodsFor:'accessing'! |
|
168 |
||
169 |
contents |
|
170 |
"return the entire contents of the stream |
|
171 |
- this is our hashValue." |
|
172 |
||
173 |
^ self hashValue |
|
174 |
||
175 |
"Created: / 17.3.1999 / 15:10:03 / stefan" |
|
176 |
! ! |
|
177 |
||
178 |
!HashStream methodsFor:'not implemented'! |
|
179 |
||
180 |
next |
|
181 |
^ self shouldNotImplement |
|
182 |
||
183 |
"Created: / 17.3.1999 / 15:11:03 / stefan" |
|
184 |
! ! |
|
185 |
||
12142 | 186 |
!HashStream methodsFor:'operations'! |
187 |
||
188 |
digestMessage:bytes |
|
189 |
"answer the digest of bytes" |
|
190 |
||
191 |
self reset. |
|
192 |
self nextPutAll:bytes. |
|
193 |
||
194 |
^ self contents. |
|
195 |
||
196 |
" |
|
197 |
SHA1Stream new |
|
198 |
digestMessage:'123456789abcdefg'; |
|
199 |
digestMessage:'123456789abcdefg' |
|
12273
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
200 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
201 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
202 |
digestMessage:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
203 |
digestMessage:'123456789abcdefg' |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
204 |
|
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
205 |
(SHA1Stream new hmac key:'123456') |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
206 |
nextPutAll:'123456789abcdefg'; |
96403b4aeb20
comment/format in: #digestMessage:
Stefan Vogel <sv@exept.de>
parents:
12174
diff
changeset
|
207 |
contents |
12142 | 208 |
" |
209 |
! |
|
210 |
||
211 |
hmac |
|
212 |
"answer a hmac stream with myself" |
|
213 |
||
214 |
^ HmacStream on:self |
|
215 |
||
216 |
" |
|
217 |
(SHA1Stream new hmac key:'exampleKey') digestMessage:'message to generate MAC of' |
|
218 |
" |
|
219 |
! |
|
220 |
||
221 |
reset |
|
222 |
"initialize to a clean state" |
|
223 |
||
224 |
^ self subclassResponsibility |
|
225 |
! ! |
|
226 |
||
6864 | 227 |
!HashStream methodsFor:'queries'! |
228 |
||
229 |
blockSize |
|
230 |
"the class knows about the basic block size" |
|
231 |
||
232 |
^ self class blockSize |
|
233 |
||
234 |
"Created: / 18.3.1999 / 10:17:02 / stefan" |
|
235 |
! |
|
236 |
||
237 |
hashSize |
|
238 |
"the class knows about the basic hash size" |
|
239 |
||
240 |
^ self class hashSize |
|
241 |
||
242 |
"Created: / 18.3.1999 / 10:17:12 / stefan" |
|
243 |
"Modified: / 15.10.1999 / 11:53:20 / stefan" |
|
244 |
! |
|
245 |
||
7251 | 246 |
hashValue |
247 |
"retunr the value of the computeted hash" |
|
248 |
||
249 |
^ self subclassResponsibility |
|
250 |
! |
|
251 |
||
6864 | 252 |
isReadable |
253 |
"return true, if reading is supported by the recevier. |
|
254 |
Always return false here" |
|
255 |
||
256 |
^ false |
|
257 |
||
258 |
"Modified: / 17.3.1999 / 15:06:09 / stefan" |
|
259 |
! |
|
260 |
||
261 |
isWritable |
|
262 |
"return true, if writing is supported by the recevier. |
|
263 |
Always return true here" |
|
264 |
||
265 |
^ true |
|
266 |
||
267 |
"Created: / 17.3.1999 / 15:05:49 / stefan" |
|
268 |
! ! |
|
269 |
||
270 |
!HashStream methodsFor:'testing'! |
|
271 |
||
272 |
atEnd |
|
273 |
"return true if the end of the stream has been reached; |
|
274 |
this is never reached" |
|
275 |
||
276 |
^ false |
|
277 |
||
278 |
"Created: / 17.3.1999 / 15:08:55 / stefan" |
|
279 |
! ! |
|
280 |
||
281 |
!HashStream methodsFor:'writing'! |
|
282 |
||
7042 | 283 |
nextPut:anObject |
284 |
"add the hash of anObject to the computed hash so far. |
|
285 |
anObject can be a Character, SmallInteger, or Character-, Byte- |
|
286 |
Integer-Array" |
|
287 |
||
288 |
^ self subclassResponsibility |
|
289 |
! |
|
290 |
||
6864 | 291 |
nextPutAll:aCollection |
292 |
"Hash streams handle Strings and ByteArrays in nextPut:" |
|
293 |
||
11799 | 294 |
aCollection isByteCollection ifTrue:[ |
6864 | 295 |
self nextPut:aCollection. |
296 |
] ifFalse:[ |
|
297 |
super nextPutAll:aCollection |
|
298 |
]. |
|
299 |
||
300 |
"Created: / 14.10.1999 / 11:22:50 / stefan" |
|
7042 | 301 |
! |
302 |
||
303 |
nextPutBytes:count from:anObject startingAt:start |
|
304 |
"write count bytes from an object starting at index start. |
|
305 |
Return the number of bytes written. |
|
306 |
The object must have non-pointer indexed instvars |
|
307 |
(i.e. be a ByteArray, String, Float- or DoubleArray). |
|
308 |
Use with care - non object oriented i/o. |
|
309 |
This is provided for compatibility with externalStream; |
|
310 |
to support binary storage. |
|
311 |
||
7251 | 312 |
Redefined, because HashStream encodes integers as 4 bytes. |
7042 | 313 |
You should implement this method in subclasses" |
314 |
||
315 |
|idx| |
|
316 |
||
317 |
"self subclassResponsibility" |
|
318 |
||
319 |
idx := start. |
|
320 |
1 to:count do:[:i | |
|
321 |
self nextPut:(anObject byteAt:idx) asCharacter. |
|
322 |
idx := idx + 1 |
|
323 |
]. |
|
324 |
^ count |
|
6864 | 325 |
! ! |
326 |
||
327 |
!HashStream class methodsFor:'documentation'! |
|
328 |
||
329 |
version |
|
12753 | 330 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.13 2010-03-04 14:34:21 cg Exp $' |
12142 | 331 |
! |
332 |
||
333 |
version_CVS |
|
12753 | 334 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.13 2010-03-04 14:34:21 cg Exp $' |
6864 | 335 |
! ! |