1975
|
1 |
"{ Package: 'stx:libbasic2' }"
|
1192
|
2 |
|
|
3 |
HashStream subclass:#CRC32Stream
|
|
4 |
instanceVariableNames:'crc'
|
|
5 |
classVariableNames:'CrcTable'
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'System-Crypt-Streams'
|
|
8 |
!
|
|
9 |
|
|
10 |
!CRC32Stream class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42].
|
|
15 |
The CRC polynomial employed is
|
|
16 |
|
|
17 |
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
|
|
18 |
|
|
19 |
Use CRC to protect against communication errors. Do NOT use CRC for cryptography.
|
|
20 |
Use SHA1Stream or MD5Stream instead.
|
|
21 |
|
|
22 |
|
|
23 |
[author:]
|
|
24 |
Stefan Vogel (stefan@zwerg)
|
|
25 |
|
|
26 |
[instance variables:]
|
|
27 |
|
|
28 |
[class variables:]
|
|
29 |
|
|
30 |
[see also:]
|
|
31 |
SHA1Stream
|
|
32 |
MD5Stream
|
|
33 |
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
examples
|
|
38 |
"
|
|
39 |
|
1977
|
40 |
expect 60C1D0A0
|
1192
|
41 |
[exBegin]
|
1977
|
42 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString
|
1976
|
43 |
[exEnd]
|
1192
|
44 |
|
|
45 |
[exBegin]
|
|
46 |
self information:(CRC32Stream new nextPut:4711; hashValue) printString
|
1976
|
47 |
[exEnd]
|
1192
|
48 |
|
|
49 |
[exBegin]
|
|
50 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) printString
|
|
51 |
[exEnd]
|
|
52 |
|
|
53 |
[exBegin]
|
|
54 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
55 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
56 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
1975
|
57 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) digitBytes hexPrintString)
|
1192
|
58 |
[exEnd]
|
|
59 |
"
|
|
60 |
! !
|
|
61 |
|
|
62 |
!CRC32Stream class methodsFor:'initialization'!
|
|
63 |
|
|
64 |
initialize
|
|
65 |
|
|
66 |
CrcTable := IntegerArray new:256.
|
|
67 |
|
|
68 |
0 to:255 do:[:count| |i|
|
|
69 |
i := count.
|
|
70 |
8 timesRepeat:[
|
|
71 |
(i bitTest:1) ifTrue:[
|
|
72 |
i := 16rEDB88320 bitXor:(i bitShift:-1)
|
|
73 |
] ifFalse:[
|
|
74 |
i := i bitShift:-1
|
|
75 |
]
|
|
76 |
].
|
|
77 |
CrcTable at:count+1 put:i.
|
|
78 |
].
|
|
79 |
|
|
80 |
"
|
|
81 |
self initialize
|
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
|
85 |
!CRC32Stream class methodsFor:'testing'!
|
|
86 |
|
|
87 |
testVector
|
|
88 |
"define a testvector to test the implementation"
|
|
89 |
|
|
90 |
^ #(
|
|
91 |
(#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
92 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
93 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
94 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
|
|
95 |
2362461299)
|
|
96 |
|
|
97 |
(#[0 0 0 0 0 0 0 0 0 0
|
|
98 |
0 0 0 0 0 0 0 0 0 0
|
|
99 |
0 0 0 0 0 0 0 0 0 0
|
|
100 |
0 0 0 0 0 0 0 0 0 0]
|
|
101 |
3924573617)
|
|
102 |
|
|
103 |
(#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
|
104 |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
|
|
105 |
228994620 )
|
|
106 |
)
|
|
107 |
|
|
108 |
"
|
|
109 |
self test
|
|
110 |
"
|
|
111 |
! !
|
|
112 |
|
|
113 |
!CRC32Stream methodsFor:'initialization'!
|
|
114 |
|
|
115 |
initialize
|
|
116 |
"initialize the CRC"
|
|
117 |
|
|
118 |
crc := 16rFFFFFFFF.
|
|
119 |
! !
|
|
120 |
|
|
121 |
!CRC32Stream methodsFor:'queries'!
|
|
122 |
|
|
123 |
hashValue
|
|
124 |
"return the computed CRC"
|
|
125 |
|
|
126 |
^ crc bitXor:16rFFFFFFFF
|
|
127 |
! !
|
|
128 |
|
|
129 |
!CRC32Stream methodsFor:'writing'!
|
|
130 |
|
|
131 |
nextPut:arg
|
|
132 |
"add the hash of anObject to the computed hash so far.
|
|
133 |
anObject can be a Character, SmallInteger, or Character-, Byte-
|
|
134 |
Integer-Array"
|
|
135 |
|
1977
|
136 |
| argSize "{ Class:SmallInteger }"
|
|
137 |
byte "{ Class:SmallInteger }" |
|
|
138 |
|
|
139 |
arg isByteCollection ifTrue:[
|
|
140 |
argSize := arg size.
|
|
141 |
1 to:argSize do:[:n|
|
|
142 |
byte := arg byteAt:n.
|
|
143 |
crc := (CrcTable at:((crc bitXor:byte) bitAnd:16rFF)+1)
|
|
144 |
bitXor:(crc bitShift:-8).
|
|
145 |
].
|
|
146 |
^ self.
|
|
147 |
].
|
|
148 |
|
1192
|
149 |
arg isCharacter ifTrue:[
|
1977
|
150 |
crc := (CrcTable at:((crc bitXor:arg codePoint) bitAnd:16rFF)+1)
|
1192
|
151 |
bitXor:(crc bitShift:-8).
|
|
152 |
^ self.
|
|
153 |
].
|
|
154 |
|
|
155 |
arg isInteger ifTrue:[
|
|
156 |
arg < 256 ifTrue:[
|
|
157 |
crc := (CrcTable at:((crc bitXor:arg) bitAnd:16rFF)+1)
|
|
158 |
bitXor:(crc bitShift:-8).
|
|
159 |
^ self.
|
|
160 |
].
|
1977
|
161 |
arg digitBytes do:[:eachByte|
|
|
162 |
crc := (CrcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1)
|
1192
|
163 |
bitXor:(crc bitShift:-8).
|
|
164 |
].
|
|
165 |
^ self.
|
|
166 |
].
|
1977
|
167 |
|
1192
|
168 |
arg isCollection ifTrue:[
|
1977
|
169 |
arg do:[:eachElement|
|
|
170 |
self nextPut:eachElement
|
1192
|
171 |
].
|
|
172 |
^ self.
|
|
173 |
].
|
|
174 |
|
|
175 |
|
|
176 |
self error:'unsupported argument'.
|
|
177 |
! !
|
|
178 |
|
|
179 |
!CRC32Stream class methodsFor:'documentation'!
|
|
180 |
|
|
181 |
version
|
1977
|
182 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.4 2008-04-24 14:49:13 stefan Exp $'
|
1192
|
183 |
! !
|
|
184 |
|
|
185 |
CRC32Stream initialize!
|