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