author | Claus Gittinger <cg@exept.de> |
Sat, 16 Mar 2019 23:30:01 +0100 | |
changeset 4857 | 42f77137031b |
parent 4847 | c76689ab85bf |
child 4864 | 847aab873d88 |
permissions | -rw-r--r-- |
4845 | 1 |
"{ Encoding: utf8 }" |
2 |
||
2416 | 3 |
" |
4 |
COPYRIGHT (c) 2003 by eXept Software AG |
|
2963 | 5 |
All Rights Reserved |
2416 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1975 | 14 |
"{ Package: 'stx:libbasic2' }" |
1192 | 15 |
|
4238 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
4845 | 18 |
CRCStream subclass:#CRC32Stream |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
1192 | 21 |
poolDictionaries:'' |
2324 | 22 |
category:'System-Crypt-Hashing' |
1192 | 23 |
! |
24 |
||
25 |
!CRC32Stream class methodsFor:'documentation'! |
|
26 |
||
2416 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2003 by eXept Software AG |
|
2963 | 30 |
All Rights Reserved |
2416 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
1192 | 41 |
documentation |
42 |
" |
|
2963 | 43 |
Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
44 |
The default CRC polynomial employed is |
1192 | 45 |
|
4238 | 46 |
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 |
47 |
(or 16r04C11DB7) |
|
1192 | 48 |
|
4845 | 49 |
You can also create an instace performing the Castagnioli CRC-32C |
50 |
(used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2): |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
51 |
|
4238 | 52 |
self newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
53 |
|
4845 | 54 |
poly: 16r1edc6f41 |
55 |
= x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
56 |
|
2746 | 57 |
Only use CRC to protect against communication errors; |
2310 | 58 |
do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead. |
59 |
||
2963 | 60 |
Notice that this CRC is also used with PNG images - therefore, its performance |
2310 | 61 |
directly affects png image processing. |
1192 | 62 |
|
2306 | 63 |
throughput: |
4238 | 64 |
220 Mb/s on MacBook Pro (2.6Ghz I7) |
65 |
157000 Kb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
|
66 |
150000 Kb/s on 2Ghz Duo |
|
1192 | 67 |
|
68 |
[author:] |
|
4238 | 69 |
Stefan Vogel (stefan@zwerg) |
1192 | 70 |
|
71 |
[instance variables:] |
|
72 |
||
73 |
[class variables:] |
|
74 |
||
75 |
[see also:] |
|
4238 | 76 |
SHA1Stream |
77 |
MD5Stream |
|
1192 | 78 |
|
79 |
" |
|
80 |
! |
|
81 |
||
82 |
examples |
|
83 |
" |
|
84 |
||
2306 | 85 |
expect 60C1D0A0 |
4238 | 86 |
[exBegin] |
1977 | 87 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
4238 | 88 |
[exEnd] |
1192 | 89 |
|
2306 | 90 |
expect 16r60C1D0A0 |
4238 | 91 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
92 |
self information:(CRC32Stream new |
4238 | 93 |
nextPut:$r; |
94 |
nextPut:$e; |
|
95 |
nextPut:$s; |
|
96 |
nextPut:$u; |
|
97 |
nextPut:$m; |
|
98 |
nextPut:$e; |
|
99 |
hashValue) hexPrintString |
|
100 |
[exEnd] |
|
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
101 |
|
2306 | 102 |
expect 16r70E46888: |
4238 | 103 |
[exBegin] |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
104 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString |
4238 | 105 |
[exEnd] |
1192 | 106 |
|
2306 | 107 |
expect 16r8CD04C73: |
4238 | 108 |
[exBegin] |
2963 | 109 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
4238 | 110 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
111 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
112 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString) |
|
113 |
[exEnd] |
|
2306 | 114 |
|
115 |
timing throughput: |
|
4238 | 116 |
[exBegin] |
2306 | 117 |
|hashStream n t| |
118 |
||
119 |
hashStream := CRC32Stream new. |
|
2962 | 120 |
n := 1000000. |
2306 | 121 |
t := Time millisecondsToRun:[ |
4238 | 122 |
n timesRepeat:[ |
123 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
124 |
]. |
|
125 |
]. |
|
2306 | 126 |
t := (t / 1000) asFloat. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
127 |
Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString. |
2306 | 128 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
129 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
4238 | 130 |
[exEnd] |
2306 | 131 |
|
4845 | 132 |
test vectors from https://tools.ietf.org/html/rfc3720#page-217: |
133 |
||
134 |
expect 0 |
|
135 |
[exBegin] |
|
136 |
self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString |
|
137 |
[exEnd] |
|
138 |
expect C1D04330 |
|
139 |
[exBegin] |
|
140 |
self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString |
|
141 |
[exEnd] |
|
142 |
expect E3069283 |
|
143 |
[exBegin] |
|
144 |
self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString |
|
145 |
[exEnd] |
|
146 |
expect 8A9136AA |
|
147 |
[exBegin] |
|
148 |
self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString |
|
149 |
[exEnd] |
|
150 |
expect 62a8ab43 |
|
151 |
[exBegin] |
|
152 |
self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString |
|
153 |
[exEnd] |
|
154 |
expect 46dd794e |
|
155 |
[exBegin] |
|
156 |
self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString |
|
157 |
[exEnd] |
|
1192 | 158 |
|
4845 | 159 |
" |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
160 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
161 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
162 |
!CRC32Stream class methodsFor:'instance creation'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
163 |
|
4845 | 164 |
generatorPolynomMSB:anInteger |
165 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
166 |
||
167 |
^ self generatorPolynom:(anInteger bitReversed32) |
|
1192 | 168 |
|
169 |
" |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
170 |
self assert:((self generatorPolynom:16r82F63B78) |
4845 | 171 |
nextPut:'123456789'; |
172 |
hashValue) = 16rE3069283 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
173 |
" |
4845 | 174 |
|
175 |
"Created: / 16-03-2019 / 20:55:46 / Claus Gittinger" |
|
176 |
! |
|
177 |
||
178 |
generatorPolynomMSB:anInteger initValue:initValueArg |
|
179 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
180 |
||
181 |
^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg |
|
182 |
||
183 |
"Created: / 16-03-2019 / 21:11:31 / Claus Gittinger" |
|
184 |
! |
|
185 |
||
186 |
generatorPolynomMSB:anInteger initValue:initValueArg xorOut:xorOut |
|
187 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
188 |
||
189 |
^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg xorOut:xorOut |
|
190 |
||
191 |
"Created: / 16-03-2019 / 21:21:27 / Claus Gittinger" |
|
192 |
! |
|
193 |
||
194 |
new |
|
195 |
"return an instance of the ITU-T CRC-32 |
|
196 |
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" |
|
197 |
||
198 |
"/ 16r4C11DB7 bitReversed32 -> 16rEDB88320 |
|
4857 | 199 |
^ self newCCITT |
200 |
||
201 |
"Created: / 16-03-2019 / 21:09:19 / Claus Gittinger" |
|
202 |
"Modified: / 16-03-2019 / 23:29:45 / Claus Gittinger" |
|
203 |
! |
|
204 |
||
205 |
newCCITT |
|
206 |
"return an instance of the ITU-T CRC-32 |
|
207 |
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" |
|
208 |
||
209 |
"/ 16r4C11DB7 bitReversed32 -> 16rEDB88320 |
|
4845 | 210 |
^ self generatorPolynom:16rEDB88320 initValue:16rFFFFFFFF xorOut:16rFFFFFFFF |
211 |
||
4857 | 212 |
"Created: / 16-03-2019 / 23:29:39 / Claus Gittinger" |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
213 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
214 |
|
2572 | 215 |
newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
216 |
"return an instance of the Castagnoli CRC-32 |
4845 | 217 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
218 |
(used in iSCSI & SCTP, G.hn payload, SSE4.2)" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
219 |
|
4845 | 220 |
"/ 16r1edc6f41 bitReversed32 -> 16r82F63B78 |
221 |
^ self generatorPolynom:16r82F63B78 initValue:16rFFFFFFFF xorOut:16rFFFFFFFF |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
222 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
223 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
224 |
Castagnoli crc: |
2572 | 225 |
self assert:((self newCrc32c) |
4845 | 226 |
nextPut:'123456789'; |
227 |
hashValue) = 3808858755. '16rE3069283' |
|
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
228 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
229 |
default crc: |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
230 |
self assert:((self new) |
4845 | 231 |
nextPut:'123456789'; |
232 |
hashValue) = 3421780262. '16rCBF43926' |
|
1192 | 233 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
234 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
235 |
"Modified (comment): / 17-05-2012 / 12:48:53 / cg" |
4845 | 236 |
"Modified: / 16-03-2019 / 21:21:07 / Claus Gittinger" |
1192 | 237 |
! ! |
238 |
||
4845 | 239 |
!CRC32Stream methodsFor:'initialization'! |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
240 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
241 |
generatorPolynom:anInteger |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
242 |
"set the generator polynom for this instance. |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
243 |
Note: you have to set the bit-reversed value, so the LSB must be first" |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
244 |
|
4845 | 245 |
self generatorPolynom:anInteger initValue:16rFFFFFFFF xorOut:16rFFFFFFFF. |
246 |
||
247 |
"Modified: / 16-03-2019 / 21:23:25 / Claus Gittinger" |
|
2705 | 248 |
! |
249 |
||
4845 | 250 |
generatorPolynom:anInteger initValue:initValueArg |
251 |
"set the generator polynom for this instance. |
|
252 |
Note: you have to set the bit-reversed value, so the LSB must be first" |
|
1192 | 253 |
|
4845 | 254 |
self generatorPolynom:anInteger initValue:initValueArg xorOut:16rFFFFFFFF |
1192 | 255 |
|
4845 | 256 |
"Created: / 16-03-2019 / 21:06:16 / Claus Gittinger" |
1192 | 257 |
! ! |
258 |
||
259 |
!CRC32Stream class methodsFor:'documentation'! |
|
260 |
||
261 |
version |
|
4238 | 262 |
^ '$Header$' |
2306 | 263 |
! |
264 |
||
265 |
version_CVS |
|
4238 | 266 |
^ '$Header$' |
1192 | 267 |
! ! |
268 |