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 |
|
|
40 |
[exBegin]
|
|
41 |
self information:(CRC32Stream hashValueOf:'Dies ist ein Test') printString
|
1975
|
42 |
CRC32Stream [exEnd]
|
1192
|
43 |
|
|
44 |
[exBegin]
|
|
45 |
self information:(CRC32Stream new nextPut:4711; hashValue) printString
|
1975
|
46 |
CRC32Stream [exEnd]
|
1192
|
47 |
|
|
48 |
[exBegin]
|
|
49 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) printString
|
|
50 |
[exEnd]
|
|
51 |
|
|
52 |
[exBegin]
|
|
53 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
54 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
55 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
1975
|
56 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) digitBytes hexPrintString)
|
1192
|
57 |
[exEnd]
|
|
58 |
"
|
|
59 |
! !
|
|
60 |
|
|
61 |
!CRC32Stream class methodsFor:'initialization'!
|
|
62 |
|
|
63 |
initialize
|
|
64 |
|
|
65 |
CrcTable := IntegerArray new:256.
|
|
66 |
|
|
67 |
0 to:255 do:[:count| |i|
|
|
68 |
i := count.
|
|
69 |
8 timesRepeat:[
|
|
70 |
(i bitTest:1) ifTrue:[
|
|
71 |
i := 16rEDB88320 bitXor:(i bitShift:-1)
|
|
72 |
] ifFalse:[
|
|
73 |
i := i bitShift:-1
|
|
74 |
]
|
|
75 |
].
|
|
76 |
CrcTable at:count+1 put:i.
|
|
77 |
].
|
|
78 |
|
|
79 |
"
|
|
80 |
self initialize
|
|
81 |
"
|
|
82 |
! !
|
|
83 |
|
|
84 |
!CRC32Stream class methodsFor:'testing'!
|
|
85 |
|
|
86 |
testVector
|
|
87 |
"define a testvector to test the implementation"
|
|
88 |
|
|
89 |
^ #(
|
|
90 |
(#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
|
|
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 |
2362461299)
|
|
95 |
|
|
96 |
(#[0 0 0 0 0 0 0 0 0 0
|
|
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 |
3924573617)
|
|
101 |
|
|
102 |
(#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
|
103 |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
|
|
104 |
228994620 )
|
|
105 |
)
|
|
106 |
|
|
107 |
"
|
|
108 |
self test
|
|
109 |
"
|
|
110 |
! !
|
|
111 |
|
|
112 |
!CRC32Stream methodsFor:'initialization'!
|
|
113 |
|
|
114 |
initialize
|
|
115 |
"initialize the CRC"
|
|
116 |
|
|
117 |
crc := 16rFFFFFFFF.
|
|
118 |
! !
|
|
119 |
|
|
120 |
!CRC32Stream methodsFor:'queries'!
|
|
121 |
|
|
122 |
hashValue
|
|
123 |
"return the computed CRC"
|
|
124 |
|
|
125 |
^ crc bitXor:16rFFFFFFFF
|
|
126 |
! !
|
|
127 |
|
|
128 |
!CRC32Stream methodsFor:'writing'!
|
|
129 |
|
|
130 |
nextPut:arg
|
|
131 |
"add the hash of anObject to the computed hash so far.
|
|
132 |
anObject can be a Character, SmallInteger, or Character-, Byte-
|
|
133 |
Integer-Array"
|
|
134 |
|
|
135 |
arg isCharacter ifTrue:[
|
|
136 |
crc := (CrcTable at:((crc bitXor:arg asciiValue) bitAnd:16rFF)+1)
|
|
137 |
bitXor:(crc bitShift:-8).
|
|
138 |
^ self.
|
|
139 |
].
|
|
140 |
|
|
141 |
arg isInteger ifTrue:[
|
|
142 |
arg < 256 ifTrue:[
|
|
143 |
crc := (CrcTable at:((crc bitXor:arg) bitAnd:16rFF)+1)
|
|
144 |
bitXor:(crc bitShift:-8).
|
|
145 |
^ self.
|
|
146 |
].
|
|
147 |
arg digitBytes do:[:byte|
|
|
148 |
crc := (CrcTable at:((crc bitXor:byte) bitAnd:16rFF)+1)
|
|
149 |
bitXor:(crc bitShift:-8).
|
|
150 |
].
|
|
151 |
^ self.
|
|
152 |
].
|
|
153 |
arg isCollection ifTrue:[
|
|
154 |
arg do:[:element|
|
|
155 |
self nextPut:element
|
|
156 |
].
|
|
157 |
^ self.
|
|
158 |
].
|
|
159 |
|
|
160 |
|
|
161 |
self error:'unsupported argument'.
|
|
162 |
! !
|
|
163 |
|
|
164 |
!CRC32Stream class methodsFor:'documentation'!
|
|
165 |
|
|
166 |
version
|
1975
|
167 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.2 2008-04-24 14:18:30 stefan Exp $'
|
1192
|
168 |
! !
|
|
169 |
|
|
170 |
CRC32Stream initialize!
|