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
|
|
19 |
instanceVariableNames:''
|
|
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 |
"
|
|
43 |
newCRC_CCITT:
|
|
44 |
Standard CRC method as defined by CCITT
|
|
45 |
(ITU-T-T30, ITU-T V42, ITU-T X25, RFC1331, RFC1662, ISO/IEC FCD14443-3).
|
|
46 |
This is used in data link protocols such as HDLC, SS7, and ISDN.
|
|
47 |
|
|
48 |
The default polynomial is
|
|
49 |
x^16 + x^12 + x^5 + 1
|
|
50 |
|
|
51 |
newCRC_DNP:
|
|
52 |
Standard CRC method as defined by DNP.
|
|
53 |
This is used in data link protocols such as HDLC, SS7, and ISDN.
|
|
54 |
|
|
55 |
The default polynomial is 0x3d65
|
|
56 |
|
|
57 |
|
|
58 |
Only use CRC to protect against communication errors;
|
|
59 |
do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead.
|
|
60 |
|
|
61 |
[author:]
|
|
62 |
Claus Gittinger
|
|
63 |
|
|
64 |
[instance variables:]
|
|
65 |
|
|
66 |
[class variables:]
|
|
67 |
|
|
68 |
[see also:]
|
|
69 |
SHA1Stream
|
|
70 |
MD5Stream
|
|
71 |
|
|
72 |
"
|
|
73 |
!
|
|
74 |
|
|
75 |
examples
|
|
76 |
"
|
|
77 |
expect xxx:
|
|
78 |
[exBegin]
|
|
79 |
self information:(CRC16Stream hashValueOf:'resume') hexPrintString
|
|
80 |
[exEnd]
|
|
81 |
|
|
82 |
expect xxx:
|
|
83 |
[exBegin]
|
|
84 |
self information:(CRC16Stream new
|
|
85 |
nextPut:$r;
|
|
86 |
nextPut:$e;
|
|
87 |
nextPut:$s;
|
|
88 |
nextPut:$u;
|
|
89 |
nextPut:$m;
|
|
90 |
nextPut:$e;
|
|
91 |
hashValue) hexPrintString
|
|
92 |
[exEnd]
|
|
93 |
|
|
94 |
expect xxx:
|
|
95 |
[exBegin]
|
|
96 |
self information:(CRC16Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
|
|
97 |
[exEnd]
|
|
98 |
|
|
99 |
expect xxx:
|
|
100 |
[exBegin]
|
|
101 |
self information:((CRC16Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
102 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
103 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
104 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
|
|
105 |
[exEnd]
|
|
106 |
|
|
107 |
expect xxx:
|
|
108 |
timing throughput:
|
|
109 |
[exBegin]
|
|
110 |
|hashStream n t|
|
|
111 |
|
|
112 |
hashStream := CRC16Stream new.
|
|
113 |
n := 1000000.
|
|
114 |
t := Time millisecondsToRun:[
|
|
115 |
n timesRepeat:[
|
|
116 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
|
|
117 |
].
|
|
118 |
].
|
|
119 |
t := (t / 1000) asFloat.
|
|
120 |
Transcript show:'crc16:'; showCR: hashStream hashValue hexPrintString.
|
|
121 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
|
|
122 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s'
|
|
123 |
[exEnd]
|
|
124 |
|
|
125 |
"
|
|
126 |
! !
|
|
127 |
|
|
128 |
!CRC16Stream class methodsFor:'instance creation'!
|
|
129 |
|
|
130 |
generatorPolynomMSB:anInteger
|
|
131 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
132 |
|
|
133 |
^ self generatorPolynom:(anInteger bitReversed16)
|
|
134 |
|
|
135 |
"Created: / 16-03-2019 / 20:58:49 / Claus Gittinger"
|
|
136 |
!
|
|
137 |
|
|
138 |
generatorPolynomMSB:anInteger initValue:initValue
|
|
139 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
140 |
|
|
141 |
^ self generatorPolynom:(anInteger bitReversed16) initValue:initValue
|
|
142 |
|
|
143 |
"Created: / 16-03-2019 / 21:12:24 / Claus Gittinger"
|
|
144 |
!
|
|
145 |
|
|
146 |
generatorPolynomMSB:anInteger initValue:initValueArg xorOut:xorOut
|
|
147 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
148 |
|
|
149 |
^ self generatorPolynom:(anInteger bitReversed16) initValue:initValueArg xorOut:xorOut
|
|
150 |
|
|
151 |
"Created: / 16-03-2019 / 21:29:04 / Claus Gittinger"
|
|
152 |
!
|
|
153 |
|
|
154 |
new
|
|
155 |
"return an instance of the CCITT CRC-16"
|
|
156 |
|
|
157 |
^ self newCCITT
|
|
158 |
|
|
159 |
"Created: / 16-03-2019 / 21:33:36 / Claus Gittinger"
|
|
160 |
!
|
|
161 |
|
|
162 |
newCCITT
|
|
163 |
"return an instance of the CCITT CRC-16
|
|
164 |
2r11110101100101
|
|
165 |
x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
|
|
166 |
|
4858
|
167 |
"/ 16r1021 bitReversed16 => 16r8408
|
4862
|
168 |
^ self generatorPolynomMSB:16r1021 initValue:16rffff xorOut:16rFFFF
|
4848
|
169 |
|
|
170 |
"Created: / 16-03-2019 / 21:13:11 / Claus Gittinger"
|
4862
|
171 |
"Modified: / 17-03-2019 / 00:37:40 / Claus Gittinger"
|
4848
|
172 |
!
|
|
173 |
|
|
174 |
newDNP
|
|
175 |
"return an instance of the DNP CRC-16
|
|
176 |
2r11110101100101
|
|
177 |
x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
|
|
178 |
|
|
179 |
^ self generatorPolynomMSB:16r3d65 initValue:0 xorOut:16rFFFF
|
|
180 |
|
|
181 |
"Created: / 16-03-2019 / 20:50:03 / Claus Gittinger"
|
4861
|
182 |
"Modified (comment): / 17-03-2019 / 00:00:18 / Claus Gittinger"
|
4848
|
183 |
!
|
|
184 |
|
|
185 |
newModbus
|
|
186 |
"return an instance of the Modbus CRC-16
|
|
187 |
2r11110101100101
|
|
188 |
x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
|
|
189 |
|
|
190 |
^ self generatorPolynomMSB:16r8005 initValue:16rFFFF xorOut:0
|
|
191 |
|
|
192 |
"Created: / 16-03-2019 / 21:15:26 / Claus Gittinger"
|
4861
|
193 |
"Modified (comment): / 17-03-2019 / 00:00:23 / Claus Gittinger"
|
4848
|
194 |
! !
|
|
195 |
|
|
196 |
!CRC16Stream methodsFor:'initialization'!
|
|
197 |
|
|
198 |
generatorPolynom:anInteger
|
|
199 |
"set the generator polynom for this instance.
|
|
200 |
Note: you have to set the bit-reversed value, so the LSB must be first"
|
|
201 |
|
|
202 |
self generatorPolynom:anInteger initValue:16rFFFF xorOut:16rFFFF.
|
|
203 |
|
|
204 |
"Modified: / 16-03-2019 / 21:24:35 / Claus Gittinger"
|
|
205 |
!
|
|
206 |
|
|
207 |
generatorPolynom:anInteger initValue:initValueArg
|
|
208 |
"set the generator polynom for this instance.
|
|
209 |
Note: you have to set the bit-reversed value, so the LSB must be first"
|
|
210 |
|
|
211 |
self generatorPolynom:anInteger initValue:initValueArg xorOut:16rFFFF
|
|
212 |
|
|
213 |
"Created: / 16-03-2019 / 21:24:03 / Claus Gittinger"
|
|
214 |
! !
|
|
215 |
|
|
216 |
!CRC16Stream class methodsFor:'documentation'!
|
|
217 |
|
|
218 |
version_CVS
|
|
219 |
^ '$Header$'
|
|
220 |
! !
|
|
221 |
|