4858
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4848
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
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 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
CRCStream subclass:#CRC16Stream
|
4905
|
19 |
instanceVariableNames:'byteSwapped'
|
4848
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'System-Crypt-Hashing'
|
|
23 |
!
|
|
24 |
|
|
25 |
!CRC16Stream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
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 |
|
|
41 |
documentation
|
|
42 |
"
|
4899
|
43 |
Only use CRC to protect against communication errors;
|
|
44 |
DO NOT use CRC for cryptography, authentication, security, etc.
|
|
45 |
- use secure hashes for those instead.
|
|
46 |
|
|
47 |
ATTENTION: CRC16Stream is currently BROKEN (in development)
|
4905
|
48 |
|
|
49 |
crc16 is a full zoo of different implementations;
|
|
50 |
they differ in the polynom, the initial value and the final xor value.
|
|
51 |
Also, some return an MSB, others an LSB first value (i.e. byteswapped).
|
|
52 |
Make sure to use the correct one:
|
4874
|
53 |
|
4848
|
54 |
newCRC_CCITT:
|
4905
|
55 |
Standard CRC method as defined by CCITT with FFFF as start value
|
4848
|
56 |
(ITU-T-T30, ITU-T V42, ITU-T X25, RFC1331, RFC1662, ISO/IEC FCD14443-3).
|
|
57 |
This is used in data link protocols such as HDLC, SS7, and ISDN.
|
|
58 |
|
4905
|
59 |
The polynomial is
|
4848
|
60 |
x^16 + x^12 + x^5 + 1
|
|
61 |
|
|
62 |
newCRC_DNP:
|
|
63 |
Standard CRC method as defined by DNP.
|
|
64 |
This is used in data link protocols such as HDLC, SS7, and ISDN.
|
4905
|
65 |
DNP 3.0, or distributed network protocol is a communication protocol
|
|
66 |
designed for use between substation computers, RTUs remote terminal units,
|
|
67 |
IEDs intelligent electronic devices
|
|
68 |
and master stations for the electric utility industry.
|
|
69 |
It is now also used in familiar industries like waste water treatment,
|
|
70 |
transportation and the oil and gas industry.
|
|
71 |
The polynomial is 0x3d65
|
4848
|
72 |
|
4905
|
73 |
newKERMIT:
|
|
74 |
CRC as used in kermit protocol
|
4848
|
75 |
|
4905
|
76 |
newMODBUS:
|
|
77 |
CRC as used in the modbus protocol
|
|
78 |
|
4848
|
79 |
[author:]
|
|
80 |
Claus Gittinger
|
|
81 |
|
|
82 |
[instance variables:]
|
|
83 |
|
|
84 |
[class variables:]
|
|
85 |
|
|
86 |
[see also:]
|
|
87 |
SHA1Stream
|
|
88 |
MD5Stream
|
|
89 |
|
|
90 |
"
|
|
91 |
!
|
|
92 |
|
|
93 |
examples
|
|
94 |
"
|
|
95 |
expect xxx:
|
|
96 |
[exBegin]
|
|
97 |
self information:(CRC16Stream hashValueOf:'resume') hexPrintString
|
|
98 |
[exEnd]
|
|
99 |
|
|
100 |
expect xxx:
|
|
101 |
[exBegin]
|
|
102 |
self information:(CRC16Stream new
|
|
103 |
nextPut:$r;
|
|
104 |
nextPut:$e;
|
|
105 |
nextPut:$s;
|
|
106 |
nextPut:$u;
|
|
107 |
nextPut:$m;
|
|
108 |
nextPut:$e;
|
|
109 |
hashValue) hexPrintString
|
|
110 |
[exEnd]
|
|
111 |
|
|
112 |
expect xxx:
|
|
113 |
[exBegin]
|
|
114 |
self information:(CRC16Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
|
|
115 |
[exEnd]
|
|
116 |
|
|
117 |
expect xxx:
|
|
118 |
[exBegin]
|
|
119 |
self information:((CRC16Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
120 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
121 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
122 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
|
|
123 |
[exEnd]
|
|
124 |
|
|
125 |
expect xxx:
|
|
126 |
timing throughput:
|
|
127 |
[exBegin]
|
|
128 |
|hashStream n t|
|
|
129 |
|
|
130 |
hashStream := CRC16Stream new.
|
|
131 |
n := 1000000.
|
|
132 |
t := Time millisecondsToRun:[
|
|
133 |
n timesRepeat:[
|
|
134 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
|
|
135 |
].
|
|
136 |
].
|
|
137 |
t := (t / 1000) asFloat.
|
|
138 |
Transcript show:'crc16:'; showCR: hashStream hashValue hexPrintString.
|
|
139 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
|
|
140 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s'
|
|
141 |
[exEnd]
|
|
142 |
|
|
143 |
"
|
|
144 |
! !
|
|
145 |
|
|
146 |
!CRC16Stream class methodsFor:'instance creation'!
|
|
147 |
|
|
148 |
generatorPolynomMSB:anInteger
|
|
149 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
150 |
|
|
151 |
^ self generatorPolynom:(anInteger bitReversed16)
|
|
152 |
|
|
153 |
"Created: / 16-03-2019 / 20:58:49 / Claus Gittinger"
|
|
154 |
!
|
|
155 |
|
|
156 |
generatorPolynomMSB:anInteger initValue:initValue
|
|
157 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
158 |
|
|
159 |
^ self generatorPolynom:(anInteger bitReversed16) initValue:initValue
|
|
160 |
|
|
161 |
"Created: / 16-03-2019 / 21:12:24 / Claus Gittinger"
|
|
162 |
!
|
|
163 |
|
|
164 |
generatorPolynomMSB:anInteger initValue:initValueArg xorOut:xorOut
|
|
165 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
166 |
|
|
167 |
^ self generatorPolynom:(anInteger bitReversed16) initValue:initValueArg xorOut:xorOut
|
|
168 |
|
|
169 |
"Created: / 16-03-2019 / 21:29:04 / Claus Gittinger"
|
|
170 |
!
|
|
171 |
|
|
172 |
new
|
|
173 |
"return an instance of the CCITT CRC-16"
|
|
174 |
|
|
175 |
^ self newCCITT
|
|
176 |
|
|
177 |
"Created: / 16-03-2019 / 21:33:36 / Claus Gittinger"
|
|
178 |
!
|
|
179 |
|
|
180 |
newCCITT
|
4905
|
181 |
"return an instance of the CCITT CRC-16 with FFFF as initial value
|
4865
|
182 |
x16 + x12 + x5 + 1 "
|
4848
|
183 |
|
4858
|
184 |
"/ 16r1021 bitReversed16 => 16r8408
|
4918
|
185 |
self error:'currently broken'.
|
4905
|
186 |
^ self generatorPolynomMSB:16r1021 initValue:16rFFFF xorOut:0
|
4848
|
187 |
|
|
188 |
"Created: / 16-03-2019 / 21:13:11 / Claus Gittinger"
|
4918
|
189 |
"Modified: / 25-03-2019 / 14:32:39 / Claus Gittinger"
|
4905
|
190 |
!
|
|
191 |
|
|
192 |
newCCITT_1D0F
|
|
193 |
"return an instance of the CCITT CRC-16 with 1D0F as initial value
|
|
194 |
x16 + x12 + x5 + 1 "
|
|
195 |
|
|
196 |
"/ 16r1021 bitReversed16 => 16r8408
|
4918
|
197 |
self error:'currently broken'.
|
4905
|
198 |
^ self generatorPolynomMSB:16r1021 initValue:16r1D0F xorOut:0
|
|
199 |
|
|
200 |
"Created: / 24-03-2019 / 20:57:32 / Claus Gittinger"
|
4918
|
201 |
"Modified: / 25-03-2019 / 14:33:05 / Claus Gittinger"
|
4865
|
202 |
!
|
|
203 |
|
|
204 |
newCRC_16
|
|
205 |
"return an instance of the CRC-16
|
|
206 |
x16 + x15 + x2 + 1 "
|
|
207 |
|
|
208 |
"/ 16r8005 bitReversed16 => 16rA001
|
4918
|
209 |
self error:'currently broken'.
|
4865
|
210 |
^ self generatorPolynomMSB:16rA001 initValue:0 xorOut:16rFFFF
|
|
211 |
|
|
212 |
"Created: / 17-03-2019 / 13:59:52 / Claus Gittinger"
|
4918
|
213 |
"Modified: / 25-03-2019 / 14:32:06 / Claus Gittinger"
|
4848
|
214 |
!
|
|
215 |
|
|
216 |
newDNP
|
|
217 |
"return an instance of the DNP CRC-16
|
4905
|
218 |
x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1
|
4848
|
219 |
|
4905
|
220 |
DNP 3.0, or distributed network protocol is a communication protocol
|
|
221 |
designed for use between substation computers, RTUs remote terminal units,
|
|
222 |
IEDs intelligent electronic devices
|
|
223 |
and master stations for the electric utility industry.
|
|
224 |
It is now also used in familiar industries like waste water treatment,
|
|
225 |
transportation and the oil and gas industry."
|
|
226 |
|
|
227 |
"/ 16r3d65 bitReversed16 hexPrintString -> 'A6BC'
|
|
228 |
^ (self generatorPolynom:16rA6BC initValue:0 xorOut:16rFFFF) byteSwapped:true
|
4848
|
229 |
|
|
230 |
"Created: / 16-03-2019 / 20:50:03 / Claus Gittinger"
|
4905
|
231 |
"Modified: / 24-03-2019 / 21:17:58 / Claus Gittinger"
|
4848
|
232 |
!
|
|
233 |
|
4865
|
234 |
newKERMIT
|
|
235 |
"return an instance of the CRC-16 used by kermit
|
|
236 |
2r11110101100101
|
|
237 |
x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
|
|
238 |
|
|
239 |
"/ 16r1021 bitReversed16 => 16r8408
|
4905
|
240 |
^ (self generatorPolynomMSB:16r1021 initValue:0 xorOut:0) byteSwapped:true
|
4865
|
241 |
|
|
242 |
"Created: / 17-03-2019 / 13:51:20 / Claus Gittinger"
|
4905
|
243 |
"Modified: / 24-03-2019 / 20:10:31 / Claus Gittinger"
|
4865
|
244 |
!
|
|
245 |
|
4870
|
246 |
newMODBUS
|
|
247 |
"return an instance of the MODBUS CRC-16
|
|
248 |
x16 + x15 + x2 + 1 "
|
|
249 |
|
|
250 |
"/ 16r8005 bitReversed16 => 16rA001
|
4905
|
251 |
^ self generatorPolynom:16rA001 initValue:16rFFFF xorOut:0
|
4870
|
252 |
|
|
253 |
"Created: / 17-03-2019 / 14:22:41 / Claus Gittinger"
|
4905
|
254 |
"Modified: / 24-03-2019 / 20:08:41 / Claus Gittinger"
|
4870
|
255 |
!
|
|
256 |
|
4905
|
257 |
newXMODEM
|
|
258 |
"return an instance of the XMODEM protocol
|
|
259 |
x16 + x12 + x5 + 1
|
|
260 |
1 2r0001000000100001 "
|
4918
|
261 |
|
|
262 |
self error:'currently broken'.
|
4905
|
263 |
^ self generatorPolynom:16r1021 initValue:0 xorOut:0
|
|
264 |
"/ ^ self generatorPolynomMSB:16r1021 initValue:0 xorOut:0
|
4848
|
265 |
|
4905
|
266 |
"Created: / 24-03-2019 / 20:16:08 / Claus Gittinger"
|
4918
|
267 |
"Modified: / 25-03-2019 / 14:31:41 / Claus Gittinger"
|
4905
|
268 |
! !
|
|
269 |
|
|
270 |
!CRC16Stream class methodsFor:'queries'!
|
4848
|
271 |
|
4905
|
272 |
hashSize
|
|
273 |
"return the size of the hashvalue returned by instances of this class (in bytes)"
|
|
274 |
|
|
275 |
^ 2
|
|
276 |
|
|
277 |
"Created: / 24-03-2019 / 12:53:05 / Claus Gittinger"
|
4848
|
278 |
! !
|
|
279 |
|
|
280 |
!CRC16Stream methodsFor:'initialization'!
|
|
281 |
|
4905
|
282 |
byteSwapped
|
|
283 |
^ byteSwapped ? false
|
|
284 |
|
|
285 |
"Created: / 24-03-2019 / 20:10:12 / Claus Gittinger"
|
|
286 |
!
|
|
287 |
|
|
288 |
byteSwapped:aBoolean
|
|
289 |
byteSwapped := aBoolean
|
|
290 |
|
|
291 |
"Created: / 24-03-2019 / 20:10:01 / Claus Gittinger"
|
|
292 |
!
|
|
293 |
|
4865
|
294 |
generatorPolynom:anLSBInteger
|
|
295 |
"set the generator polynom for this instance,
|
|
296 |
set start and xorOut to 16rFFFF.
|
4848
|
297 |
Note: you have to set the bit-reversed value, so the LSB must be first"
|
|
298 |
|
4865
|
299 |
self generatorPolynom:anLSBInteger
|
|
300 |
initValue:16rFFFF xorOut:16rFFFF.
|
4848
|
301 |
|
|
302 |
"Modified: / 16-03-2019 / 21:24:35 / Claus Gittinger"
|
4865
|
303 |
"Modified (format): / 17-03-2019 / 14:02:47 / Claus Gittinger"
|
4848
|
304 |
!
|
|
305 |
|
4865
|
306 |
generatorPolynom:anLSBInteger initValue:initValueArg
|
4848
|
307 |
"set the generator polynom for this instance.
|
4865
|
308 |
set start to initValueArg and xorOut to 16rFFFF.
|
4848
|
309 |
Note: you have to set the bit-reversed value, so the LSB must be first"
|
|
310 |
|
4865
|
311 |
self generatorPolynom:anLSBInteger
|
|
312 |
initValue:initValueArg xorOut:16rFFFF
|
4848
|
313 |
|
|
314 |
"Created: / 16-03-2019 / 21:24:03 / Claus Gittinger"
|
4865
|
315 |
"Modified (format): / 17-03-2019 / 14:02:57 / Claus Gittinger"
|
|
316 |
! !
|
|
317 |
|
|
318 |
!CRC16Stream methodsFor:'queries'!
|
|
319 |
|
|
320 |
hashValue
|
|
321 |
"return the computed CRC.
|
|
322 |
For unknown reasons, the standard crc16 needs byte swapping"
|
|
323 |
|
|
324 |
|hv|
|
|
325 |
|
|
326 |
hv := super hashValue.
|
4905
|
327 |
(byteSwapped == true) ifTrue:[
|
4865
|
328 |
^ hv byteSwapped16
|
|
329 |
].
|
|
330 |
^ hv.
|
|
331 |
|
|
332 |
"Created: / 17-03-2019 / 13:45:29 / Claus Gittinger"
|
4905
|
333 |
"Modified: / 24-03-2019 / 20:11:49 / Claus Gittinger"
|
4848
|
334 |
! !
|
|
335 |
|
|
336 |
!CRC16Stream class methodsFor:'documentation'!
|
|
337 |
|
|
338 |
version_CVS
|
|
339 |
^ '$Header$'
|
|
340 |
! !
|
|
341 |
|