eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'CRC32Stream':

Home

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

Class: CRC32Stream


Inheritance:

   Object
   |
   +--Stream
      |
      +--HashStream
         |
         +--CRCStream
            |
            +--CRC32Stream
               |
               +--CRC32Stream::CRC32Stream_Castagnoli

Package:
stx:libbasic2
Category:
System-Crypt-Hashing
Version:
rev: 1.50 date: 2019/03/25 10:18:17
user: cg
file: CRC32Stream.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Stefan Vogel (stefan@zwerg)

Description:


Only use CRC to protect against communication errors;
DO NOT use CRC for cryptography, authentication, security, etc.
- use secure hashes for those instead.

Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42].
The default CRC polynomial employed is

    x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
    (or 16r04C11DB7)

You can also create an instance performing the Castagnoli CRC-32C
(used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2):

    self newCrc32c

 the polynomial is: 16r1edc6f41
    = x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1

Notice that this CRC is also used with PNG images;
therefore, its performance directly affects png image processing (png write speed).

throughput:
    235 Mb/s on MacBook Pro (2.6Ghz I7) 
            (360 Mb/s for big chunks of size 50k)
            (100 Mb/s for small chunks of size 10)
    157 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit)
    150 Mb/s on 2Ghz Duo
    
 new:
    500 Mb/s for castagnoli on MacBook Pro (2.6Ghz I7) 
            (5Gb/s for big chunks of size 50k)


[instance variables:]

[class variables:]


Related information:

    SHA1Stream
    MD5Stream

Class protocol:

instance creation
o  generatorPolynomMSB: anInteger
notice, in literature, the generator polynom is usually specified as an MSB number

o  generatorPolynomMSB: anInteger initValue: initValueArg
notice, in literature, the generator polynom is usually specified as an MSB number

o  generatorPolynomMSB: anInteger initValue: initValueArg xorOut: xorOut
notice, in literature, the generator polynom is usually specified as an MSB number

o  new
return an instance of the ITU-T CRC-32

o  newCCITT
return an instance of the ITU-T CRC-32
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1

o  newCastagnoli
return an instance of the Castagnoli CRC-32
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
(used in iSCSI & SCTP, G.hn payload, SSE4.2)

o  newCrc32c
return an instance of the Castagnoli CRC-32

usage example(s):

     Castagnoli crc:
       self assert:((self newCrc32c)
				nextPut:'123456789';
				hashValue) = 3808858755. '16rE3069283'

     default crc:
       self assert:((self new)
				nextPut:'123456789';
				hashValue) = 3421780262. '16rCBF43926'

private
o  canUseFastCRC
self canUseFastCRC

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


Instance protocol:

initialization
o  generatorPolynom: anLSBInteger
set the generator polynom for this instance.
set start and xorOut to 16rFFFFFFFF.
Note: you have to set the bit-reversed value, so the LSB must be first

o  generatorPolynom: anLSBInteger initValue: initValueArg
set the generator polynom for this instance.
set start to initValueArg and xorOut to 16rFFFFFFFF.
Note: you have to set the bit-reversed value, so the LSB must be first


Private classes:

    CRC32Stream_Castagnoli

Examples:


expect 60C1D0A0
  self information:(CRC32Stream hashValueOf:'resume') hexPrintString
expect 16r60C1D0A0
  self information:(CRC32Stream new
                          nextPut:$r;
                          nextPut:$e;
                          nextPut:$s;
                          nextPut:$u;
                          nextPut:$m;
                          nextPut:$e;
                          hashValue) hexPrintString
expect 16r70E46888:
  self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
expect 16r8CD04C73:
  self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
           16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
           16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
           16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
timing throughput: 230Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
  |hashStream n t|

  hashStream := CRC32Stream new.
  n := 2000000.
  t := Time millisecondsToRun:[
          n timesRepeat:[
              hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
          ].
       ].
  t := (t / 1000) asFloat.
  Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
  Transcript show:t; show:' seconds for '; show:(50*n/1024/1024) asFloat; showCR:' Mb'.
  Transcript show:(n*50/1024/1024 / t); showCR:' Mb/s'
500Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
  |hashStream s n t l|

  hashStream := CRC32Stream newCastagnoli.
  n := 2000000.
  s := '12345678901234567890123456789012345678901234567890'.
  l := s size.
  t := Time millisecondsToRun:[
          n timesRepeat:[
              hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
          ].
       ].
  t := (t / 1000) asFloat.
  Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
  Transcript show:t; show:' seconds for '; show:(l*n/1024/1024) asFloat; showCR:' Mb'.
  Transcript show:(n*l/1024/1024 / t); showCR:' Mb/s'
the real speed is shown with longer inputs...
  |hashStream n t l s|

  hashStream := CRC32Stream newCastagnoli.
  n := 20000.
  s := '1234567890' ,* 10000.
  l := s size.
  t := Time millisecondsToRun:[
          n timesRepeat:[
              hashStream nextPutAll:s
          ].
       ].
  t := (t / 1000) asFloat.
  Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
  Transcript show:t; show:' seconds for '; show:(l*n/1024/1024) asFloat; showCR:' Mb'.
  Transcript show:(n*l/1024/1024 / t); showCR:' Mb/s'
test vectors from https://tools.ietf.org/html/rfc3720#page-217: expect 0
  self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString
expect C1D04330
  self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString
expect E3069283
  self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString
expect 8A9136AA
  self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString
expect 62a8ab43
  self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString
expect 46dd794e
  self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Thu, 19 Sep 2019 15:16:04 GMT