mas01mj@732
|
1 /*
|
mas01mj@732
|
2 Copyright (c) 2008, Adobe Systems Incorporated
|
mas01mj@732
|
3 All rights reserved.
|
mas01mj@732
|
4
|
mas01mj@732
|
5 Redistribution and use in source and binary forms, with or without
|
mas01mj@732
|
6 modification, are permitted provided that the following conditions are
|
mas01mj@732
|
7 met:
|
mas01mj@732
|
8
|
mas01mj@732
|
9 * Redistributions of source code must retain the above copyright notice,
|
mas01mj@732
|
10 this list of conditions and the following disclaimer.
|
mas01mj@732
|
11
|
mas01mj@732
|
12 * Redistributions in binary form must reproduce the above copyright
|
mas01mj@732
|
13 notice, this list of conditions and the following disclaimer in the
|
mas01mj@732
|
14 documentation and/or other materials provided with the distribution.
|
mas01mj@732
|
15
|
mas01mj@732
|
16 * Neither the name of Adobe Systems Incorporated nor the names of its
|
mas01mj@732
|
17 contributors may be used to endorse or promote products derived from
|
mas01mj@732
|
18 this software without specific prior written permission.
|
mas01mj@732
|
19
|
mas01mj@732
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
mas01mj@732
|
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
mas01mj@732
|
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
mas01mj@732
|
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mas01mj@732
|
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
mas01mj@732
|
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
mas01mj@732
|
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
mas01mj@732
|
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
mas01mj@732
|
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
mas01mj@732
|
31 */
|
mas01mj@732
|
32
|
mas01mj@732
|
33 package com.adobe.crypto
|
mas01mj@732
|
34 {
|
mas01mj@732
|
35 import com.adobe.utils.IntUtil;
|
mas01mj@732
|
36 import flash.utils.ByteArray;
|
mas01mj@732
|
37
|
mas01mj@732
|
38 /**
|
mas01mj@732
|
39 * Perform MD5 hash of an input stream in chunks. This class is
|
mas01mj@732
|
40 * based on com.adobe.crypto.MD5 and can process data in
|
mas01mj@732
|
41 * chunks. Both block creation and hash computation are done
|
mas01mj@732
|
42 * together for whatever input is available so that the memory
|
mas01mj@732
|
43 * overhead at a time is always fixed. Memory usage is governed by
|
mas01mj@732
|
44 * two parameters: one is the amount of data passed in to update()
|
mas01mj@732
|
45 * and the other is memoryBlockSize. The latter comes into play
|
mas01mj@732
|
46 * only when the memory window exceeds the pre allocated memory
|
mas01mj@732
|
47 * window of flash player. Usage: create an instance, call
|
mas01mj@732
|
48 * update(data) repeatedly for all chunks and finally complete()
|
mas01mj@732
|
49 * which will return the md5 hash.
|
mas01mj@732
|
50 */
|
mas01mj@732
|
51 public class MD5Stream
|
mas01mj@732
|
52 {
|
mas01mj@732
|
53 private static var mask:int = 0xFF;
|
mas01mj@732
|
54
|
mas01mj@732
|
55 private var arr:Array = [];
|
mas01mj@732
|
56
|
mas01mj@732
|
57 /* running count of length */
|
mas01mj@732
|
58 private var arrLen:int;
|
mas01mj@732
|
59
|
mas01mj@732
|
60 // initialize the md buffers
|
mas01mj@732
|
61 private var a:int = 1732584193;
|
mas01mj@732
|
62 private var b:int = -271733879;
|
mas01mj@732
|
63 private var c:int = -1732584194;
|
mas01mj@732
|
64 private var d:int = 271733878;
|
mas01mj@732
|
65
|
mas01mj@732
|
66 // variables to store previous values
|
mas01mj@732
|
67 private var aa:int;
|
mas01mj@732
|
68 private var bb:int;
|
mas01mj@732
|
69 private var cc:int;
|
mas01mj@732
|
70 private var dd:int;
|
mas01mj@732
|
71
|
mas01mj@732
|
72 /* index for data read */
|
mas01mj@732
|
73 private var arrIndexLen:int = 0;
|
mas01mj@732
|
74 /* index for hash computation */
|
mas01mj@732
|
75 private var arrProcessIndex:int = 0;
|
mas01mj@732
|
76 /* index for removing stale arr values */
|
mas01mj@732
|
77 private var cleanIndex:int = 0;
|
mas01mj@732
|
78
|
mas01mj@732
|
79 /**
|
mas01mj@732
|
80 * Change this value from the default (16384) in the range of
|
mas01mj@732
|
81 * MBs to actually affect GC as GC allocates in pools of
|
mas01mj@732
|
82 * memory */
|
mas01mj@732
|
83 public var memoryBlockSize:int = 16384;
|
mas01mj@732
|
84
|
mas01mj@732
|
85
|
mas01mj@732
|
86 public function MD5Stream()
|
mas01mj@732
|
87 {
|
mas01mj@732
|
88
|
mas01mj@732
|
89 }
|
mas01mj@732
|
90
|
mas01mj@732
|
91
|
mas01mj@732
|
92 /**
|
mas01mj@732
|
93 * Pass in chunks of the input data with update(), call
|
mas01mj@732
|
94 * complete() with an optional chunk which will return the
|
mas01mj@732
|
95 * final hash. Equivalent to the way
|
mas01mj@732
|
96 * java.security.MessageDigest works.
|
mas01mj@732
|
97 *
|
mas01mj@732
|
98 * @param input The optional bytearray chunk which is the final part of the input
|
mas01mj@732
|
99 * @return A string containing the hash value
|
mas01mj@732
|
100 * @langversion ActionScript 3.0
|
mas01mj@732
|
101 * @playerversion Flash 8.5
|
mas01mj@732
|
102 * @tiptext
|
mas01mj@732
|
103 */
|
mas01mj@732
|
104 public function complete(input:ByteArray=null):String
|
mas01mj@732
|
105 {
|
mas01mj@732
|
106 if ( arr.length == 0 )
|
mas01mj@732
|
107 {
|
mas01mj@732
|
108 if ( input == null )
|
mas01mj@732
|
109 {
|
mas01mj@732
|
110 throw new Error("null input to complete without prior call to update. At least an empty bytearray must be passed.");
|
mas01mj@732
|
111 }
|
mas01mj@732
|
112 }
|
mas01mj@732
|
113
|
mas01mj@732
|
114 if ( input != null )
|
mas01mj@732
|
115 {
|
mas01mj@732
|
116 readIntoArray(input);
|
mas01mj@732
|
117 }
|
mas01mj@732
|
118
|
mas01mj@732
|
119 //pad, append length
|
mas01mj@732
|
120 padArray(arrLen);
|
mas01mj@732
|
121
|
mas01mj@732
|
122 hashRemainingChunks(false);
|
mas01mj@732
|
123
|
mas01mj@732
|
124 var res:String = IntUtil.toHex( a ) + IntUtil.toHex( b ) +
|
mas01mj@732
|
125 IntUtil.toHex( c ) + IntUtil.toHex( d );
|
mas01mj@732
|
126 resetFields();
|
mas01mj@732
|
127
|
mas01mj@732
|
128 return res;
|
mas01mj@732
|
129 }
|
mas01mj@732
|
130
|
mas01mj@732
|
131 /**
|
mas01mj@732
|
132 * Pass in chunks of the input data with update(), call
|
mas01mj@732
|
133 * complete() with an optional chunk which will return the
|
mas01mj@732
|
134 * final hash. Equivalent to the way
|
mas01mj@732
|
135 * java.security.MessageDigest works.
|
mas01mj@732
|
136 *
|
mas01mj@732
|
137 * @param input The bytearray chunk to perform the hash on
|
mas01mj@732
|
138 * @langversion ActionScript 3.0
|
mas01mj@732
|
139 * @playerversion Flash 8.5
|
mas01mj@732
|
140 * @tiptext
|
mas01mj@732
|
141 */
|
mas01mj@732
|
142 public function update(input:ByteArray):void
|
mas01mj@732
|
143 {
|
mas01mj@732
|
144 readIntoArray(input);
|
mas01mj@732
|
145 hashRemainingChunks();
|
mas01mj@732
|
146 }
|
mas01mj@732
|
147
|
mas01mj@732
|
148 /**
|
mas01mj@732
|
149 * Re-initialize this instance for use to perform hashing on
|
mas01mj@732
|
150 * another input stream. This is called automatically by
|
mas01mj@732
|
151 * complete().
|
mas01mj@732
|
152 *
|
mas01mj@732
|
153 * @langversion ActionScript 3.0
|
mas01mj@732
|
154 * @playerversion Flash 8.5
|
mas01mj@732
|
155 * @tiptext
|
mas01mj@732
|
156 */
|
mas01mj@732
|
157 public function resetFields():void
|
mas01mj@732
|
158 {
|
mas01mj@732
|
159 //truncate array
|
mas01mj@732
|
160 arr.length = 0;
|
mas01mj@732
|
161 arrLen = 0;
|
mas01mj@732
|
162
|
mas01mj@732
|
163 // initialize the md buffers
|
mas01mj@732
|
164 a = 1732584193;
|
mas01mj@732
|
165 b = -271733879;
|
mas01mj@732
|
166 c = -1732584194;
|
mas01mj@732
|
167 d = 271733878;
|
mas01mj@732
|
168
|
mas01mj@732
|
169 // variables to store previous values
|
mas01mj@732
|
170 aa = 0;
|
mas01mj@732
|
171 bb = 0;
|
mas01mj@732
|
172 cc = 0;
|
mas01mj@732
|
173 dd = 0;
|
mas01mj@732
|
174
|
mas01mj@732
|
175 arrIndexLen = 0;
|
mas01mj@732
|
176 arrProcessIndex = 0;
|
mas01mj@732
|
177 cleanIndex = 0;
|
mas01mj@732
|
178 }
|
mas01mj@732
|
179
|
mas01mj@732
|
180 /** read into arr and free up used blocks of arr */
|
mas01mj@732
|
181 private function readIntoArray(input:ByteArray):void
|
mas01mj@732
|
182 {
|
mas01mj@732
|
183 var closestChunkLen:int = input.length * 8;
|
mas01mj@732
|
184 arrLen += closestChunkLen;
|
mas01mj@732
|
185
|
mas01mj@732
|
186 /* clean up memory. if there are entries in the array that
|
mas01mj@732
|
187 * are already processed and the amount is greater than
|
mas01mj@732
|
188 * memoryBlockSize, create a new array, copy the last
|
mas01mj@732
|
189 * block into it and let the old one get picked up by
|
mas01mj@732
|
190 * GC. */
|
mas01mj@732
|
191 if ( arrProcessIndex - cleanIndex > memoryBlockSize )
|
mas01mj@732
|
192 {
|
mas01mj@732
|
193 var newarr:Array= new Array();
|
mas01mj@732
|
194
|
mas01mj@732
|
195 /* AS Arrays in sparse arrays. arr[2002] can exist
|
mas01mj@732
|
196 * without values for arr[0] - arr[2001] */
|
mas01mj@732
|
197 for ( var j:int = arrProcessIndex; j < arr.length; j++ )
|
mas01mj@732
|
198 {
|
mas01mj@732
|
199 newarr[j] = arr[j];
|
mas01mj@732
|
200 }
|
mas01mj@732
|
201
|
mas01mj@732
|
202 cleanIndex = arrProcessIndex;
|
mas01mj@732
|
203 arr = null;
|
mas01mj@732
|
204 arr = newarr;
|
mas01mj@732
|
205 }
|
mas01mj@732
|
206
|
mas01mj@732
|
207 for ( var k:int = 0; k < closestChunkLen; k+=8 )
|
mas01mj@732
|
208 {
|
mas01mj@732
|
209 //discard high bytes (convert to uint)
|
mas01mj@732
|
210 arr[ int(arrIndexLen >> 5) ] |= ( input[ k / 8 ] & mask ) << ( arrIndexLen % 32 );
|
mas01mj@732
|
211 arrIndexLen += 8;
|
mas01mj@732
|
212 }
|
mas01mj@732
|
213
|
mas01mj@732
|
214
|
mas01mj@732
|
215 }
|
mas01mj@732
|
216
|
mas01mj@732
|
217 private function hashRemainingChunks(bUpdate:Boolean=true):void
|
mas01mj@732
|
218 {
|
mas01mj@732
|
219 var len:int = arr.length;
|
mas01mj@732
|
220
|
mas01mj@732
|
221 /* leave a 16 word block untouched if we are called from
|
mas01mj@732
|
222 * update. This is because, padArray() can modify the last
|
mas01mj@732
|
223 * block and this modification has to happen before we
|
mas01mj@732
|
224 * compute the hash. */
|
mas01mj@732
|
225 if ( bUpdate )
|
mas01mj@732
|
226 {
|
mas01mj@732
|
227 len -= 16;
|
mas01mj@732
|
228 }
|
mas01mj@732
|
229
|
mas01mj@732
|
230 /* don't do anything if don't have a 16 word block. */
|
mas01mj@732
|
231 if ( arrProcessIndex >= len || len - arrProcessIndex < 15 )
|
mas01mj@732
|
232 {
|
mas01mj@732
|
233 return;
|
mas01mj@732
|
234 }
|
mas01mj@732
|
235
|
mas01mj@732
|
236
|
mas01mj@732
|
237 for ( var i:int = arrProcessIndex; i < len ; i += 16, arrProcessIndex += 16)
|
mas01mj@732
|
238 {
|
mas01mj@732
|
239 // save previous values
|
mas01mj@732
|
240 aa = a;
|
mas01mj@732
|
241 bb = b;
|
mas01mj@732
|
242 cc = c;
|
mas01mj@732
|
243 dd = d;
|
mas01mj@732
|
244
|
mas01mj@732
|
245 // Round 1
|
mas01mj@732
|
246 a = ff( a, b, c, d, arr[int(i+ 0)], 7, -680876936 ); // 1
|
mas01mj@732
|
247 d = ff( d, a, b, c, arr[int(i+ 1)], 12, -389564586 ); // 2
|
mas01mj@732
|
248 c = ff( c, d, a, b, arr[int(i+ 2)], 17, 606105819 ); // 3
|
mas01mj@732
|
249 b = ff( b, c, d, a, arr[int(i+ 3)], 22, -1044525330 ); // 4
|
mas01mj@732
|
250 a = ff( a, b, c, d, arr[int(i+ 4)], 7, -176418897 ); // 5
|
mas01mj@732
|
251 d = ff( d, a, b, c, arr[int(i+ 5)], 12, 1200080426 ); // 6
|
mas01mj@732
|
252 c = ff( c, d, a, b, arr[int(i+ 6)], 17, -1473231341 ); // 7
|
mas01mj@732
|
253 b = ff( b, c, d, a, arr[int(i+ 7)], 22, -45705983 ); // 8
|
mas01mj@732
|
254 a = ff( a, b, c, d, arr[int(i+ 8)], 7, 1770035416 ); // 9
|
mas01mj@732
|
255 d = ff( d, a, b, c, arr[int(i+ 9)], 12, -1958414417 ); // 10
|
mas01mj@732
|
256 c = ff( c, d, a, b, arr[int(i+10)], 17, -42063 ); // 11
|
mas01mj@732
|
257 b = ff( b, c, d, a, arr[int(i+11)], 22, -1990404162 ); // 12
|
mas01mj@732
|
258 a = ff( a, b, c, d, arr[int(i+12)], 7, 1804603682 ); // 13
|
mas01mj@732
|
259 d = ff( d, a, b, c, arr[int(i+13)], 12, -40341101 ); // 14
|
mas01mj@732
|
260 c = ff( c, d, a, b, arr[int(i+14)], 17, -1502002290 ); // 15
|
mas01mj@732
|
261 b = ff( b, c, d, a, arr[int(i+15)], 22, 1236535329 ); // 16
|
mas01mj@732
|
262
|
mas01mj@732
|
263 // Round 2
|
mas01mj@732
|
264 a = gg( a, b, c, d, arr[int(i+ 1)], 5, -165796510 ); // 17
|
mas01mj@732
|
265 d = gg( d, a, b, c, arr[int(i+ 6)], 9, -1069501632 ); // 18
|
mas01mj@732
|
266 c = gg( c, d, a, b, arr[int(i+11)], 14, 643717713 ); // 19
|
mas01mj@732
|
267 b = gg( b, c, d, a, arr[int(i+ 0)], 20, -373897302 ); // 20
|
mas01mj@732
|
268 a = gg( a, b, c, d, arr[int(i+ 5)], 5, -701558691 ); // 21
|
mas01mj@732
|
269 d = gg( d, a, b, c, arr[int(i+10)], 9, 38016083 ); // 22
|
mas01mj@732
|
270 c = gg( c, d, a, b, arr[int(i+15)], 14, -660478335 ); // 23
|
mas01mj@732
|
271 b = gg( b, c, d, a, arr[int(i+ 4)], 20, -405537848 ); // 24
|
mas01mj@732
|
272 a = gg( a, b, c, d, arr[int(i+ 9)], 5, 568446438 ); // 25
|
mas01mj@732
|
273 d = gg( d, a, b, c, arr[int(i+14)], 9, -1019803690 ); // 26
|
mas01mj@732
|
274 c = gg( c, d, a, b, arr[int(i+ 3)], 14, -187363961 ); // 27
|
mas01mj@732
|
275 b = gg( b, c, d, a, arr[int(i+ 8)], 20, 1163531501 ); // 28
|
mas01mj@732
|
276 a = gg( a, b, c, d, arr[int(i+13)], 5, -1444681467 ); // 29
|
mas01mj@732
|
277 d = gg( d, a, b, c, arr[int(i+ 2)], 9, -51403784 ); // 30
|
mas01mj@732
|
278 c = gg( c, d, a, b, arr[int(i+ 7)], 14, 1735328473 ); // 31
|
mas01mj@732
|
279 b = gg( b, c, d, a, arr[int(i+12)], 20, -1926607734 ); // 32
|
mas01mj@732
|
280
|
mas01mj@732
|
281 // Round 3
|
mas01mj@732
|
282 a = hh( a, b, c, d, arr[int(i+ 5)], 4, -378558 ); // 33
|
mas01mj@732
|
283 d = hh( d, a, b, c, arr[int(i+ 8)], 11, -2022574463 ); // 34
|
mas01mj@732
|
284 c = hh( c, d, a, b, arr[int(i+11)], 16, 1839030562 ); // 35
|
mas01mj@732
|
285 b = hh( b, c, d, a, arr[int(i+14)], 23, -35309556 ); // 36
|
mas01mj@732
|
286 a = hh( a, b, c, d, arr[int(i+ 1)], 4, -1530992060 ); // 37
|
mas01mj@732
|
287 d = hh( d, a, b, c, arr[int(i+ 4)], 11, 1272893353 ); // 38
|
mas01mj@732
|
288 c = hh( c, d, a, b, arr[int(i+ 7)], 16, -155497632 ); // 39
|
mas01mj@732
|
289 b = hh( b, c, d, a, arr[int(i+10)], 23, -1094730640 ); // 40
|
mas01mj@732
|
290 a = hh( a, b, c, d, arr[int(i+13)], 4, 681279174 ); // 41
|
mas01mj@732
|
291 d = hh( d, a, b, c, arr[int(i+ 0)], 11, -358537222 ); // 42
|
mas01mj@732
|
292 c = hh( c, d, a, b, arr[int(i+ 3)], 16, -722521979 ); // 43
|
mas01mj@732
|
293 b = hh( b, c, d, a, arr[int(i+ 6)], 23, 76029189 ); // 44
|
mas01mj@732
|
294 a = hh( a, b, c, d, arr[int(i+ 9)], 4, -640364487 ); // 45
|
mas01mj@732
|
295 d = hh( d, a, b, c, arr[int(i+12)], 11, -421815835 ); // 46
|
mas01mj@732
|
296 c = hh( c, d, a, b, arr[int(i+15)], 16, 530742520 ); // 47
|
mas01mj@732
|
297 b = hh( b, c, d, a, arr[int(i+ 2)], 23, -995338651 ); // 48
|
mas01mj@732
|
298
|
mas01mj@732
|
299 // Round 4
|
mas01mj@732
|
300 a = ii( a, b, c, d, arr[int(i+ 0)], 6, -198630844 ); // 49
|
mas01mj@732
|
301 d = ii( d, a, b, c, arr[int(i+ 7)], 10, 1126891415 ); // 50
|
mas01mj@732
|
302 c = ii( c, d, a, b, arr[int(i+14)], 15, -1416354905 ); // 51
|
mas01mj@732
|
303 b = ii( b, c, d, a, arr[int(i+ 5)], 21, -57434055 ); // 52
|
mas01mj@732
|
304 a = ii( a, b, c, d, arr[int(i+12)], 6, 1700485571 ); // 53
|
mas01mj@732
|
305 d = ii( d, a, b, c, arr[int(i+ 3)], 10, -1894986606 ); // 54
|
mas01mj@732
|
306 c = ii( c, d, a, b, arr[int(i+10)], 15, -1051523 ); // 55
|
mas01mj@732
|
307 b = ii( b, c, d, a, arr[int(i+ 1)], 21, -2054922799 ); // 56
|
mas01mj@732
|
308 a = ii( a, b, c, d, arr[int(i+ 8)], 6, 1873313359 ); // 57
|
mas01mj@732
|
309 d = ii( d, a, b, c, arr[int(i+15)], 10, -30611744 ); // 58
|
mas01mj@732
|
310 c = ii( c, d, a, b, arr[int(i+ 6)], 15, -1560198380 ); // 59
|
mas01mj@732
|
311 b = ii( b, c, d, a, arr[int(i+13)], 21, 1309151649 ); // 60
|
mas01mj@732
|
312 a = ii( a, b, c, d, arr[int(i+ 4)], 6, -145523070 ); // 61
|
mas01mj@732
|
313 d = ii( d, a, b, c, arr[int(i+11)], 10, -1120210379 ); // 62
|
mas01mj@732
|
314 c = ii( c, d, a, b, arr[int(i+ 2)], 15, 718787259 ); // 63
|
mas01mj@732
|
315 b = ii( b, c, d, a, arr[int(i+ 9)], 21, -343485551 ); // 64
|
mas01mj@732
|
316
|
mas01mj@732
|
317 a += aa;
|
mas01mj@732
|
318 b += bb;
|
mas01mj@732
|
319 c += cc;
|
mas01mj@732
|
320 d += dd;
|
mas01mj@732
|
321
|
mas01mj@732
|
322 }
|
mas01mj@732
|
323
|
mas01mj@732
|
324 }
|
mas01mj@732
|
325
|
mas01mj@732
|
326 private function padArray(len:int):void
|
mas01mj@732
|
327 {
|
mas01mj@732
|
328 arr[ int(len >> 5) ] |= 0x80 << ( len % 32 );
|
mas01mj@732
|
329 arr[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;
|
mas01mj@732
|
330 arrLen = arr.length;
|
mas01mj@732
|
331 }
|
mas01mj@732
|
332
|
mas01mj@732
|
333 /* Code below same as com.adobe.crypto.MD5 */
|
mas01mj@732
|
334
|
mas01mj@732
|
335 /**
|
mas01mj@732
|
336 * Auxiliary function f as defined in RFC
|
mas01mj@732
|
337 */
|
mas01mj@732
|
338 private static function f( x:int, y:int, z:int ):int {
|
mas01mj@732
|
339 return ( x & y ) | ( (~x) & z );
|
mas01mj@732
|
340 }
|
mas01mj@732
|
341
|
mas01mj@732
|
342 /**
|
mas01mj@732
|
343 * Auxiliary function g as defined in RFC
|
mas01mj@732
|
344 */
|
mas01mj@732
|
345 private static function g( x:int, y:int, z:int ):int {
|
mas01mj@732
|
346 return ( x & z ) | ( y & (~z) );
|
mas01mj@732
|
347 }
|
mas01mj@732
|
348
|
mas01mj@732
|
349 /**
|
mas01mj@732
|
350 * Auxiliary function h as defined in RFC
|
mas01mj@732
|
351 */
|
mas01mj@732
|
352 private static function h( x:int, y:int, z:int ):int {
|
mas01mj@732
|
353 return x ^ y ^ z;
|
mas01mj@732
|
354 }
|
mas01mj@732
|
355
|
mas01mj@732
|
356 /**
|
mas01mj@732
|
357 * Auxiliary function i as defined in RFC
|
mas01mj@732
|
358 */
|
mas01mj@732
|
359 private static function i( x:int, y:int, z:int ):int {
|
mas01mj@732
|
360 return y ^ ( x | (~z) );
|
mas01mj@732
|
361 }
|
mas01mj@732
|
362
|
mas01mj@732
|
363 /**
|
mas01mj@732
|
364 * A generic transformation function. The logic of ff, gg, hh, and
|
mas01mj@732
|
365 * ii are all the same, minus the function used, so pull that logic
|
mas01mj@732
|
366 * out and simplify the method bodies for the transoformation functions.
|
mas01mj@732
|
367 */
|
mas01mj@732
|
368 private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {
|
mas01mj@732
|
369 var tmp:int = a + int( func( b, c, d ) ) + x + t;
|
mas01mj@732
|
370 return IntUtil.rol( tmp, s ) + b;
|
mas01mj@732
|
371 }
|
mas01mj@732
|
372
|
mas01mj@732
|
373 /**
|
mas01mj@732
|
374 * ff transformation function
|
mas01mj@732
|
375 */
|
mas01mj@732
|
376 private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
|
mas01mj@732
|
377 return transform( f, a, b, c, d, x, s, t );
|
mas01mj@732
|
378 }
|
mas01mj@732
|
379
|
mas01mj@732
|
380 /**
|
mas01mj@732
|
381 * gg transformation function
|
mas01mj@732
|
382 */
|
mas01mj@732
|
383 private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
|
mas01mj@732
|
384 return transform( g, a, b, c, d, x, s, t );
|
mas01mj@732
|
385 }
|
mas01mj@732
|
386
|
mas01mj@732
|
387 /**
|
mas01mj@732
|
388 * hh transformation function
|
mas01mj@732
|
389 */
|
mas01mj@732
|
390 private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
|
mas01mj@732
|
391 return transform( h, a, b, c, d, x, s, t );
|
mas01mj@732
|
392 }
|
mas01mj@732
|
393
|
mas01mj@732
|
394 /**
|
mas01mj@732
|
395 * ii transformation function
|
mas01mj@732
|
396 */
|
mas01mj@732
|
397 private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
|
mas01mj@732
|
398 return transform( i, a, b, c, d, x, s, t );
|
mas01mj@732
|
399 }
|
mas01mj@732
|
400
|
mas01mj@732
|
401 }
|
mas01mj@732
|
402 } |