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