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 |
"
|
|
12 |
|
7014
|
13 |
|
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
6864
|
16 |
Stream subclass:#HashStream
|
7014
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'System-Crypt-Streams'
|
6864
|
21 |
!
|
|
22 |
|
|
23 |
!HashStream class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 1999 by eXept Software AG
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
|
|
38 |
!
|
|
39 |
|
|
40 |
documentation
|
|
41 |
"
|
|
42 |
Abstract class. Subclasses generate hash values used as checksums
|
|
43 |
or for generating cryptographic signatures.
|
|
44 |
|
|
45 |
[author:]
|
|
46 |
Stefan Vogel
|
|
47 |
|
|
48 |
[see also:]
|
|
49 |
SHA1Stream MD5Stream
|
|
50 |
"
|
|
51 |
! !
|
|
52 |
|
|
53 |
!HashStream class methodsFor:'instance creation'!
|
|
54 |
|
7015
|
55 |
new
|
|
56 |
"have to re-allow new - it was disabled in Stream"
|
|
57 |
^ self basicNew initialize
|
|
58 |
|
|
59 |
|
|
60 |
!
|
|
61 |
|
|
62 |
random
|
|
63 |
"create a random number generator using myself"
|
|
64 |
|
|
65 |
^ HashRandom with:self
|
|
66 |
|
|
67 |
"
|
|
68 |
SHA1Stream random next
|
|
69 |
"
|
|
70 |
|
|
71 |
"Modified: / 12.11.1999 / 17:21:17 / stefan"
|
|
72 |
! !
|
|
73 |
|
7042
|
74 |
!HashStream class methodsFor:'testing'!
|
|
75 |
|
|
76 |
test
|
|
77 |
"test against testVector"
|
|
78 |
|
|
79 |
self testVector do:[:pair |
|
|
80 |
|data expectedHash hashStream|
|
|
81 |
|
|
82 |
data := pair first.
|
|
83 |
expectedHash := pair second.
|
|
84 |
|
|
85 |
hashStream := self new.
|
|
86 |
hashStream nextPut:data.
|
|
87 |
hashStream hashValue ~= expectedHash ifTrue:[
|
7251
|
88 |
self error:'Test failed'
|
7042
|
89 |
].
|
|
90 |
(self hashValueOf:data) ~= expectedHash ifTrue:[
|
7251
|
91 |
self error:'Test failed'
|
7042
|
92 |
].
|
|
93 |
].
|
|
94 |
|
|
95 |
"
|
|
96 |
MD5Stream test.
|
|
97 |
SHA1Stream test.
|
|
98 |
RipeMD160Stream test.
|
|
99 |
"
|
|
100 |
!
|
|
101 |
|
|
102 |
testVector
|
|
103 |
"define a testvector to test the implementation"
|
|
104 |
|
|
105 |
^ self subclassResponsibility
|
|
106 |
! !
|
|
107 |
|
7015
|
108 |
!HashStream class methodsFor:'utilities'!
|
|
109 |
|
7042
|
110 |
hashValueOf:aStringOrByteArrayOrStream
|
|
111 |
|hashStream|
|
|
112 |
|
|
113 |
hashStream := self new.
|
|
114 |
aStringOrByteArrayOrStream readStream copyToEndInto:hashStream.
|
|
115 |
|
|
116 |
^ hashStream hashValue
|
7014
|
117 |
|
|
118 |
"
|
|
119 |
MD5Stream hashValueOf:'BlaBlaBla'
|
7042
|
120 |
MD5Stream hashValueOf:('Makefile' asFilename readStream)
|
|
121 |
MD5Stream hashValueOf:('BlaBlaBla' readStream)
|
7014
|
122 |
"
|
|
123 |
!
|
|
124 |
|
6864
|
125 |
hashValueOfFile:aFilename
|
7042
|
126 |
|stream hash|
|
|
127 |
|
|
128 |
[
|
|
129 |
stream := aFilename asFilename readStream.
|
|
130 |
stream binary.
|
|
131 |
hash := self hashValueOf:stream.
|
|
132 |
] ensure:[
|
|
133 |
stream notNil ifTrue:[
|
|
134 |
stream close
|
|
135 |
].
|
|
136 |
].
|
|
137 |
^ hash.
|
7014
|
138 |
|
|
139 |
"
|
|
140 |
MD5Stream hashValueOfFile:'Makefile'
|
|
141 |
"
|
6864
|
142 |
! !
|
|
143 |
|
|
144 |
!HashStream methodsFor:'accessing'!
|
|
145 |
|
|
146 |
contents
|
|
147 |
"return the entire contents of the stream
|
|
148 |
- this is our hashValue."
|
|
149 |
|
|
150 |
^ self hashValue
|
|
151 |
|
|
152 |
"Created: / 17.3.1999 / 15:10:03 / stefan"
|
|
153 |
! !
|
|
154 |
|
|
155 |
!HashStream methodsFor:'not implemented'!
|
|
156 |
|
|
157 |
next
|
|
158 |
^ self shouldNotImplement
|
|
159 |
|
|
160 |
"Created: / 17.3.1999 / 15:11:03 / stefan"
|
|
161 |
! !
|
|
162 |
|
|
163 |
!HashStream methodsFor:'queries'!
|
|
164 |
|
|
165 |
blockSize
|
|
166 |
"the class knows about the basic block size"
|
|
167 |
|
|
168 |
^ self class blockSize
|
|
169 |
|
|
170 |
"Created: / 18.3.1999 / 10:17:02 / stefan"
|
|
171 |
!
|
|
172 |
|
|
173 |
hashSize
|
|
174 |
"the class knows about the basic hash size"
|
|
175 |
|
|
176 |
^ self class hashSize
|
|
177 |
|
|
178 |
"Created: / 18.3.1999 / 10:17:12 / stefan"
|
|
179 |
"Modified: / 15.10.1999 / 11:53:20 / stefan"
|
|
180 |
!
|
|
181 |
|
7251
|
182 |
hashValue
|
|
183 |
"retunr the value of the computeted hash"
|
|
184 |
|
|
185 |
^ self subclassResponsibility
|
|
186 |
!
|
|
187 |
|
6864
|
188 |
isReadable
|
|
189 |
"return true, if reading is supported by the recevier.
|
|
190 |
Always return false here"
|
|
191 |
|
|
192 |
^ false
|
|
193 |
|
|
194 |
"Modified: / 17.3.1999 / 15:06:09 / stefan"
|
|
195 |
!
|
|
196 |
|
|
197 |
isWritable
|
|
198 |
"return true, if writing is supported by the recevier.
|
|
199 |
Always return true here"
|
|
200 |
|
|
201 |
^ true
|
|
202 |
|
|
203 |
"Created: / 17.3.1999 / 15:05:49 / stefan"
|
|
204 |
! !
|
|
205 |
|
|
206 |
!HashStream methodsFor:'testing'!
|
|
207 |
|
|
208 |
atEnd
|
|
209 |
"return true if the end of the stream has been reached;
|
|
210 |
this is never reached"
|
|
211 |
|
|
212 |
^ false
|
|
213 |
|
|
214 |
"Created: / 17.3.1999 / 15:08:55 / stefan"
|
|
215 |
! !
|
|
216 |
|
|
217 |
!HashStream methodsFor:'writing'!
|
|
218 |
|
7042
|
219 |
nextPut:anObject
|
|
220 |
"add the hash of anObject to the computed hash so far.
|
|
221 |
anObject can be a Character, SmallInteger, or Character-, Byte-
|
|
222 |
Integer-Array"
|
|
223 |
|
|
224 |
^ self subclassResponsibility
|
|
225 |
!
|
|
226 |
|
6864
|
227 |
nextPutAll:aCollection
|
|
228 |
"Hash streams handle Strings and ByteArrays in nextPut:"
|
|
229 |
|
|
230 |
aCollection class isBytes ifTrue:[
|
|
231 |
self nextPut:aCollection.
|
|
232 |
] ifFalse:[
|
|
233 |
super nextPutAll:aCollection
|
|
234 |
].
|
|
235 |
|
|
236 |
"Created: / 14.10.1999 / 11:22:50 / stefan"
|
7042
|
237 |
!
|
|
238 |
|
|
239 |
nextPutBytes:count from:anObject startingAt:start
|
|
240 |
"write count bytes from an object starting at index start.
|
|
241 |
Return the number of bytes written.
|
|
242 |
The object must have non-pointer indexed instvars
|
|
243 |
(i.e. be a ByteArray, String, Float- or DoubleArray).
|
|
244 |
Use with care - non object oriented i/o.
|
|
245 |
This is provided for compatibility with externalStream;
|
|
246 |
to support binary storage.
|
|
247 |
|
7251
|
248 |
Redefined, because HashStream encodes integers as 4 bytes.
|
7042
|
249 |
You should implement this method in subclasses"
|
|
250 |
|
|
251 |
|idx|
|
|
252 |
|
|
253 |
"self subclassResponsibility"
|
|
254 |
|
|
255 |
idx := start.
|
|
256 |
1 to:count do:[:i |
|
|
257 |
self nextPut:(anObject byteAt:idx) asCharacter.
|
|
258 |
idx := idx + 1
|
|
259 |
].
|
|
260 |
^ count
|
6864
|
261 |
! !
|
|
262 |
|
|
263 |
!HashStream class methodsFor:'documentation'!
|
|
264 |
|
|
265 |
version
|
7251
|
266 |
^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.5 2003-05-05 12:58:46 stefan Exp $'
|
6864
|
267 |
! !
|