HashStream.st
changeset 15621 2a93f0420de1
parent 14842 8f7ca0be2aed
child 15622 79d968fa9f05
--- a/HashStream.st	Sat Aug 10 13:32:53 2013 +0200
+++ b/HashStream.st	Sun Aug 11 23:53:29 2013 +0200
@@ -171,6 +171,42 @@
 
 !HashStream class methodsFor:'utilities'!
 
+cryptBlock:aStringOrByteArray from:srcIdx into:resultOrNil startingAt:dstIdx encrypt:encryptMode
+    "one-way encryption of aStringOrByteArray.
+     Used when a HashStream is used as the block copher with OFB or CTR mode.
+
+     encryptMode is ignored here."
+
+    |hashValue|
+
+    srcIdx == 1 ifTrue:[
+        hashValue := self hashValueOf:aStringOrByteArray.
+    ] ifFalse:[
+        |bytesToEncrypt|
+
+        bytesToEncrypt := aStringOrByteArray copyFrom:srcIdx to:srcIdx+self hashSize-1.
+        hashValue := self hashValueOf:bytesToEncrypt.
+    ].
+    resultOrNil isNil ifTrue:[
+        ^ hashValue.
+    ] ifFalse:[
+        resultOrNil replaceBytesFrom:dstIdx to:dstIdx+hashValue size-1 with:hashValue startingAt:1.
+        ^ resultOrNil.
+    ].
+
+    "
+        |cipher iv cipherText|
+
+        cipher := OfbCipher for:SHA512Stream.
+        iv := cipher randomInitializationVector.
+        cipherText := cipher encrypt:'Hello world, here is the alien from Mars and 1234567890' asByteArray.
+        self information:cipherText printString.
+
+        cipher initializationVector:iv.
+        self information:(cipher decrypt:cipherText) asString.
+    "
+!
+
 digestMessage:aStringOrByteArrayOrStream
     ^ self hashValueOf:aStringOrByteArrayOrStream
 !
@@ -389,10 +425,10 @@
 !HashStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.27 2013-03-08 23:37:43 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.28 2013-08-11 21:53:29 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.27 2013-03-08 23:37:43 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/HashStream.st,v 1.28 2013-08-11 21:53:29 stefan Exp $'
 ! !