HashStream.st
changeset 7042 dbb7898901e3
parent 7015 862174119468
child 7251 0ef2a2a16faa
--- a/HashStream.st	Wed Feb 19 14:01:49 2003 +0100
+++ b/HashStream.st	Wed Feb 19 18:10:44 2003 +0100
@@ -71,38 +71,74 @@
     "Modified: / 12.11.1999 / 17:21:17 / stefan"
 ! !
 
+!HashStream class methodsFor:'testing'!
+
+test
+    "test against testVector"
+
+    self testVector do:[:pair |
+        |data expectedHash hashStream|
+
+        data := pair first.
+        expectedHash := pair second.
+
+        hashStream := self new.
+        hashStream nextPut:data.
+        hashStream hashValue ~= expectedHash ifTrue:[
+            self halt
+        ].
+        (self hashValueOf:data) ~= expectedHash ifTrue:[
+            self halt
+        ].
+    ].
+
+    "
+        MD5Stream test.
+        SHA1Stream test.
+        RipeMD160Stream test.
+    "
+!
+
+testVector
+    "define a testvector to test the implementation"
+
+    ^ self subclassResponsibility
+! !
+
 !HashStream class methodsFor:'utilities'!
 
-hashValueOf:aStringOrByteArry
-    ^ self hashValueOfStream:(aStringOrByteArry readStream)
+hashValueOf:aStringOrByteArrayOrStream
+    |hashStream|
+
+    hashStream := self new.
+    aStringOrByteArrayOrStream readStream copyToEndInto:hashStream.
+
+    ^ hashStream hashValue
 
     "
      MD5Stream hashValueOf:'BlaBlaBla'
+     MD5Stream hashValueOf:('Makefile' asFilename readStream)
+     MD5Stream hashValueOf:('BlaBlaBla' readStream)
     "
 !
 
 hashValueOfFile:aFilename
-    ^ self hashValueOfStream:(aFilename asFilename readStream)
+    |stream hash|
+
+    [
+        stream := aFilename asFilename readStream.
+        stream binary.
+        hash := self hashValueOf:stream.
+    ] ensure:[
+        stream notNil ifTrue:[
+            stream close
+        ].
+    ].
+    ^ hash.
 
     "
      MD5Stream hashValueOfFile:'Makefile'
     "
-!
-
-hashValueOfStream:inStream
-    |hashStream|
-
-    hashStream := self new.
-    inStream binary.
-    inStream copyToEndInto:hashStream.
-    inStream close.
-        
-    ^ hashStream hashValue
-
-    "
-     MD5Stream hashValueOfStream:('Makefile' asFilename readStream)
-     MD5Stream hashValueOfStream:('BlaBla' readStream)
-    "
 ! !
 
 !HashStream methodsFor:'accessing'!
@@ -174,6 +210,14 @@
 
 !HashStream methodsFor:'writing'!
 
+nextPut:anObject
+    "add the hash of anObject to the computed hash so far.
+     anObject can be a Character, SmallInteger, or Character-, Byte-
+     Integer-Array"
+
+    ^ self subclassResponsibility
+!
+
 nextPutAll:aCollection
     "Hash streams handle Strings and ByteArrays in nextPut:"
 
@@ -184,10 +228,34 @@
     ].
 
     "Created: / 14.10.1999 / 11:22:50 / stefan"
+!
+
+nextPutBytes:count from:anObject startingAt:start
+    "write count bytes from an object starting at index start.
+     Return the number of bytes written.
+     The object must have non-pointer indexed instvars 
+     (i.e. be a ByteArray, String, Float- or DoubleArray).     
+     Use with care - non object oriented i/o.
+     This is provided for compatibility with externalStream;
+     to support binary storage.
+
+     Redefined, because HashStream encode integers as 4 bytes.
+     You should implement this method in subclasses"
+
+    |idx|
+
+    "self subclassResponsibility"
+
+    idx := start.
+    1 to:count do:[:i |
+        self nextPut:(anObject byteAt:idx) asCharacter.
+        idx := idx + 1
+    ].
+    ^ count
 ! !
 
 !HashStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.3 2003-02-07 11:29:26 penk Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.4 2003-02-19 17:10:44 stefan Exp $'
 ! !