eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'HashStream':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: HashStream


Inheritance:

   Object
   |
   +--Stream
      |
      +--HashStream
         |
         +--CRCStream
         |
         +--MD5Stream
         |
         +--SHA1Stream

Package:
stx:libbasic
Category:
System-Crypt-Hashing
Version:
rev: 1.40 date: 2019/06/06 21:19:34
user: cg
file: HashStream.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Stefan Vogel

Description:


Abstract class.
Subclasses generate hash values used as checksums
or for generating cryptographic signatures.

Notice: due to historic reasons and compatibility with Squeak,
there are two modes of operation:
    1) hashFunction mode, in which the hash of a single block of bytes is computed
    2) hashStream mode, in which instances behave like a writeStream, computing and
       updating the hash, as data is sent to it.

hashFunction mode is called using: #hashValueOf:aStringOrByteArray
Warning: Not all subclasses support the stream mode
         (especially those which were ported from squeak do not).


Related information:

    SHA1Stream
    MD5Stream

Class protocol:

Compatibility-Squeak
o  hashMessage: aStringOrByteArrayOrStream
SQUEAK compatibility
- but this is a bad choice - squeak uses #digestMessage: at the instance side

instance creation
o  new
have to re-allow new - it was disabled in Stream

o  random
create a random number generator using myself

usage example(s):

     SHA1Stream random next

queries
o  blockSize
return the block size when the hash is used by encryption/decryption.
(see OfbCipherMode and CtrCipherMode)

o  canStream
simple hash functions (squeak-ported) cannot stream.
Use hashFunction: there

o  hashBlockSize
return the block size used internally by the compression function

** This method raises an error - it must be redefined in concrete classes **

o  hashSize
return the size of the hashvalue returned by instances of this class (in bytes)

** This method raises an error - it must be redefined in concrete classes **

o  isAbstract
Return if this class is an abstract class.
True is returned here for myself only; false for subclasses.
Abstract subclasses must redefine this again.

self tests
o  test
test against testVector

usage example(s):

        MD5Stream test.
        SHA1Stream test.
        RipeMD160Stream test.

o  testVector
obsolete - moved to TestHashAlgorithms unit test.

** This method raises an error - it must be redefined in concrete classes **

utilities
o  cryptBlock: aStringOrByteArray from: srcIdx to: srcEndOrNil into: resultOrNil startingAt: dstIdx encrypt: encryptMode
one-way encryption of aStringOrByteArray.
Used when a HashStream is used as the block cipher with OFB or CTR mode.

encryptMode is ignored here.

o  digestMessage: aStringOrByteArrayOrStream

o  hashValueHexString: aString
MD5Stream hashValueHexString:'abc'

o  hashValueOf: aStringOrByteArrayOrStream
MD5Stream hashValueOf:'BlaBlaBla'
MD5Stream hashValueOf:('makefile' asFilename readStream)
MD5Stream hashValueOf:('BlaBlaBla' readStream)

o  hashValueOfFile: aFilename
MD5Stream hashValueOfFile:'makefile'


Instance protocol:

Compatibility-Squeak
o  digestMessage: bytes
SQUEAK: answer the digest of bytes

o  hashMessage: bytes
SQUEAK: answer the digest of bytes

accessing
o  contents
return the entire contents of the stream
- this is our hashValue.

not implemented
o  next
I can only write

operations
o  hashValueOf: bytes
answer the digest of bytes

usage example(s):

        SHA1Stream new 
                hashValueOf:'123456789abcdefg';
                hashValueOf:'123456789abcdefg'

        (SHA1Stream new hmac key:'123456') 
                hashValueOf:'123456789abcdefg';
                hashValueOf:'123456789abcdefg'

        (SHA1Stream new hmac key:'123456') 
                nextPutAll:'123456789abcdefg';
                contents

o  reset
initialize to a clean state

** This method raises an error - it must be redefined in concrete classes **

queries
o  blockSize
the class knows about the basic block size

o  hashBlockSize
the class knows about the basic block size

o  hashSize
return the size of the returned hashvalue (in bytes)

o  hashValue
return the value of the computed hash

** This method raises an error - it must be redefined in concrete classes **

o  isReadable
return true, if reading is supported by the receiver.
Always return false here

o  isWritable
return true, if writing is supported by the receiver.
Always return true here

testing
o  atEnd
return true if the end of the stream has been reached;
this is never reached

writing
o  nextPut: anObject
add the hash of anObject to the computed hash so far.
anObject can be a Character, SmallInteger ByteArray or String

o  nextPutAll: aCollection
Hash streams handle Strings and ByteArrays in #nextPutBytes:

o  nextPutByte: aByte
add the hash of anObject to the computed hash so far.
aByte can be a SmallInteger <= 255

o  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).

** This method raises an error - it must be redefined in concrete classes **


Examples:


hashFunction mode:
  MD5Stream hashValueOf:'hello world'
  MD4Stream hashValueOf:'hello world'
hashStream mode:
  |md5|

  md5 := MD5Stream new.
  md5 nextPutAll:'hello world'.
  md5 hashValue  


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Tue, 19 Mar 2024 03:11:52 GMT