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.50 date: 2024/01/30 09:35:57
user: cg
file: HashStream.st directory: libbasic
module: stx stc-classLibrary: libbasic

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

copyright

COPYRIGHT (c) 1999 by eXept Software AG All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the inclusion of the above copyright notice. This software may not be provided or otherwise made available to, or used by, any other person. No title to or ownership of the software is hereby transferred.

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 (in bytes) 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 must be redefined in concrete classes (subclassResponsibility) **

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

** This method must be redefined in concrete classes (subclassResponsibility) **

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 must be redefined in concrete classes (subclassResponsibility) **

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'
MD5Stream hashValueHexString:'Hello World'
MD5Stream hashValueOf:'Hello World'

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

o  hashValueOfFile: aFilename
generate a hashValue for the whole contents of a file

Usage example(s):

     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 must be redefined in concrete classes (subclassResponsibility) **

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

o  blockSize
the class knows about the basic block size (in bytes)

o  canStream

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 must be redefined in concrete classes (subclassResponsibility) **

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

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

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

o  nextPutAll: count from: aCollection startingAt: initialIndex
Hash streams handle Strings and ByteArrays in #nextPutBytes:
Answer the number of elements written

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 must be redefined in concrete classes (subclassResponsibility) **


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.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Tue, 03 Dec 2024 18:02:54 GMT