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