--- /dev/null
+/*\r
+ Copyright (c) 2008, Adobe Systems Incorporated\r
+ All rights reserved.\r
+\r
+ Redistribution and use in source and binary forms, with or without \r
+ modification, are permitted provided that the following conditions are\r
+ met:\r
+\r
+ * Redistributions of source code must retain the above copyright notice, \r
+ this list of conditions and the following disclaimer.\r
+ \r
+ * Redistributions in binary form must reproduce the above copyright\r
+ notice, this list of conditions and the following disclaimer in the \r
+ documentation and/or other materials provided with the distribution.\r
+ \r
+ * Neither the name of Adobe Systems Incorporated nor the names of its \r
+ contributors may be used to endorse or promote products derived from \r
+ this software without specific prior written permission.\r
+\r
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*/\r
+\r
+package com.adobe.crypto\r
+{\r
+ import com.adobe.utils.IntUtil; \r
+ import flash.utils.ByteArray;\r
+\r
+ /**\r
+ * Perform MD5 hash of an input stream in chunks. This class is\r
+ * based on com.adobe.crypto.MD5 and can process data in\r
+ * chunks. Both block creation and hash computation are done\r
+ * together for whatever input is available so that the memory\r
+ * overhead at a time is always fixed. Memory usage is governed by\r
+ * two parameters: one is the amount of data passed in to update()\r
+ * and the other is memoryBlockSize. The latter comes into play\r
+ * only when the memory window exceeds the pre allocated memory\r
+ * window of flash player. Usage: create an instance, call\r
+ * update(data) repeatedly for all chunks and finally complete()\r
+ * which will return the md5 hash.\r
+ */ \r
+ public class MD5Stream\r
+ {\r
+ private static var mask:int = 0xFF;\r
+\r
+ private var arr:Array = [];\r
+\r
+ /* running count of length */\r
+ private var arrLen:int;\r
+ \r
+ // initialize the md buffers\r
+ private var a:int = 1732584193;\r
+ private var b:int = -271733879;\r
+ private var c:int = -1732584194;\r
+ private var d:int = 271733878;\r
+ \r
+ // variables to store previous values\r
+ private var aa:int;\r
+ private var bb:int;\r
+ private var cc:int;\r
+ private var dd:int;\r
+\r
+ /* index for data read */\r
+ private var arrIndexLen:int = 0;\r
+ /* index for hash computation */\r
+ private var arrProcessIndex:int = 0;\r
+ /* index for removing stale arr values */\r
+ private var cleanIndex:int = 0;\r
+ \r
+ /** \r
+ * Change this value from the default (16384) in the range of\r
+ * MBs to actually affect GC as GC allocates in pools of\r
+ * memory */\r
+ public var memoryBlockSize:int = 16384;\r
+ \r
+ \r
+ public function MD5Stream()\r
+ {\r
+ \r
+ }\r
+ \r
+ \r
+ /**\r
+ * Pass in chunks of the input data with update(), call\r
+ * complete() with an optional chunk which will return the\r
+ * final hash. Equivalent to the way\r
+ * java.security.MessageDigest works.\r
+ *\r
+ * @param input The optional bytearray chunk which is the final part of the input\r
+ * @return A string containing the hash value\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 8.5\r
+ * @tiptext\r
+ */\r
+ public function complete(input:ByteArray=null):String\r
+ {\r
+ if ( arr.length == 0 )\r
+ {\r
+ if ( input == null )\r
+ {\r
+ throw new Error("null input to complete without prior call to update. At least an empty bytearray must be passed.");\r
+ } \r
+ }\r
+ \r
+ if ( input != null )\r
+ {\r
+ readIntoArray(input);\r
+ }\r
+\r
+ //pad, append length\r
+ padArray(arrLen);\r
+\r
+ hashRemainingChunks(false);\r
+ \r
+ var res:String = IntUtil.toHex( a ) + IntUtil.toHex( b ) + \r
+ IntUtil.toHex( c ) + IntUtil.toHex( d );\r
+ resetFields();\r
+ \r
+ return res;\r
+ }\r
+\r
+ /**\r
+ * Pass in chunks of the input data with update(), call\r
+ * complete() with an optional chunk which will return the\r
+ * final hash. Equivalent to the way\r
+ * java.security.MessageDigest works.\r
+ *\r
+ * @param input The bytearray chunk to perform the hash on\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 8.5\r
+ * @tiptext\r
+ */ \r
+ public function update(input:ByteArray):void\r
+ {\r
+ readIntoArray(input);\r
+ hashRemainingChunks();\r
+ }\r
+\r
+ /**\r
+ * Re-initialize this instance for use to perform hashing on\r
+ * another input stream. This is called automatically by\r
+ * complete().\r
+ *\r
+ * @langversion ActionScript 3.0\r
+ * @playerversion Flash 8.5\r
+ * @tiptext\r
+ */ \r
+ public function resetFields():void\r
+ {\r
+ //truncate array\r
+ arr.length = 0;\r
+ arrLen = 0;\r
+ \r
+ // initialize the md buffers\r
+ a = 1732584193;\r
+ b = -271733879;\r
+ c = -1732584194;\r
+ d = 271733878;\r
+ \r
+ // variables to store previous values\r
+ aa = 0;\r
+ bb = 0;\r
+ cc = 0;\r
+ dd = 0;\r
+ \r
+ arrIndexLen = 0; \r
+ arrProcessIndex = 0;\r
+ cleanIndex = 0;\r
+ }\r
+ \r
+ /** read into arr and free up used blocks of arr */\r
+ private function readIntoArray(input:ByteArray):void\r
+ {\r
+ var closestChunkLen:int = input.length * 8;\r
+ arrLen += closestChunkLen;\r
+ \r
+ /* clean up memory. if there are entries in the array that\r
+ * are already processed and the amount is greater than\r
+ * memoryBlockSize, create a new array, copy the last\r
+ * block into it and let the old one get picked up by\r
+ * GC. */\r
+ if ( arrProcessIndex - cleanIndex > memoryBlockSize )\r
+ {\r
+ var newarr:Array= new Array();\r
+ \r
+ /* AS Arrays in sparse arrays. arr[2002] can exist \r
+ * without values for arr[0] - arr[2001] */\r
+ for ( var j:int = arrProcessIndex; j < arr.length; j++ )\r
+ { \r
+ newarr[j] = arr[j];\r
+ }\r
+ \r
+ cleanIndex = arrProcessIndex;\r
+ arr = null;\r
+ arr = newarr;\r
+ }\r
+ \r
+ for ( var k:int = 0; k < closestChunkLen; k+=8 )\r
+ {\r
+ //discard high bytes (convert to uint)\r
+ arr[ int(arrIndexLen >> 5) ] |= ( input[ k / 8 ] & mask ) << ( arrIndexLen % 32 );\r
+ arrIndexLen += 8;\r
+ }\r
+ \r
+ \r
+ }\r
+ \r
+ private function hashRemainingChunks(bUpdate:Boolean=true):void\r
+ {\r
+ var len:int = arr.length;\r
+\r
+ /* leave a 16 word block untouched if we are called from\r
+ * update. This is because, padArray() can modify the last\r
+ * block and this modification has to happen before we\r
+ * compute the hash. */\r
+ if ( bUpdate )\r
+ {\r
+ len -= 16;\r
+ }\r
+\r
+ /* don't do anything if don't have a 16 word block. */\r
+ if ( arrProcessIndex >= len || len - arrProcessIndex < 15 )\r
+ {\r
+ return;\r
+ }\r
+\r
+ \r
+ for ( var i:int = arrProcessIndex; i < len ; i += 16, arrProcessIndex += 16) \r
+ { \r
+ // save previous values\r
+ aa = a;\r
+ bb = b;\r
+ cc = c;\r
+ dd = d; \r
+ \r
+ // Round 1\r
+ a = ff( a, b, c, d, arr[int(i+ 0)], 7, -680876936 ); // 1\r
+ d = ff( d, a, b, c, arr[int(i+ 1)], 12, -389564586 ); // 2\r
+ c = ff( c, d, a, b, arr[int(i+ 2)], 17, 606105819 ); // 3\r
+ b = ff( b, c, d, a, arr[int(i+ 3)], 22, -1044525330 ); // 4\r
+ a = ff( a, b, c, d, arr[int(i+ 4)], 7, -176418897 ); // 5\r
+ d = ff( d, a, b, c, arr[int(i+ 5)], 12, 1200080426 ); // 6\r
+ c = ff( c, d, a, b, arr[int(i+ 6)], 17, -1473231341 ); // 7\r
+ b = ff( b, c, d, a, arr[int(i+ 7)], 22, -45705983 ); // 8\r
+ a = ff( a, b, c, d, arr[int(i+ 8)], 7, 1770035416 ); // 9\r
+ d = ff( d, a, b, c, arr[int(i+ 9)], 12, -1958414417 ); // 10\r
+ c = ff( c, d, a, b, arr[int(i+10)], 17, -42063 ); // 11\r
+ b = ff( b, c, d, a, arr[int(i+11)], 22, -1990404162 ); // 12\r
+ a = ff( a, b, c, d, arr[int(i+12)], 7, 1804603682 ); // 13\r
+ d = ff( d, a, b, c, arr[int(i+13)], 12, -40341101 ); // 14\r
+ c = ff( c, d, a, b, arr[int(i+14)], 17, -1502002290 ); // 15\r
+ b = ff( b, c, d, a, arr[int(i+15)], 22, 1236535329 ); // 16\r
+ \r
+ // Round 2\r
+ a = gg( a, b, c, d, arr[int(i+ 1)], 5, -165796510 ); // 17\r
+ d = gg( d, a, b, c, arr[int(i+ 6)], 9, -1069501632 ); // 18\r
+ c = gg( c, d, a, b, arr[int(i+11)], 14, 643717713 ); // 19\r
+ b = gg( b, c, d, a, arr[int(i+ 0)], 20, -373897302 ); // 20\r
+ a = gg( a, b, c, d, arr[int(i+ 5)], 5, -701558691 ); // 21\r
+ d = gg( d, a, b, c, arr[int(i+10)], 9, 38016083 ); // 22\r
+ c = gg( c, d, a, b, arr[int(i+15)], 14, -660478335 ); // 23\r
+ b = gg( b, c, d, a, arr[int(i+ 4)], 20, -405537848 ); // 24\r
+ a = gg( a, b, c, d, arr[int(i+ 9)], 5, 568446438 ); // 25\r
+ d = gg( d, a, b, c, arr[int(i+14)], 9, -1019803690 ); // 26\r
+ c = gg( c, d, a, b, arr[int(i+ 3)], 14, -187363961 ); // 27\r
+ b = gg( b, c, d, a, arr[int(i+ 8)], 20, 1163531501 ); // 28\r
+ a = gg( a, b, c, d, arr[int(i+13)], 5, -1444681467 ); // 29\r
+ d = gg( d, a, b, c, arr[int(i+ 2)], 9, -51403784 ); // 30\r
+ c = gg( c, d, a, b, arr[int(i+ 7)], 14, 1735328473 ); // 31\r
+ b = gg( b, c, d, a, arr[int(i+12)], 20, -1926607734 ); // 32\r
+ \r
+ // Round 3\r
+ a = hh( a, b, c, d, arr[int(i+ 5)], 4, -378558 ); // 33\r
+ d = hh( d, a, b, c, arr[int(i+ 8)], 11, -2022574463 ); // 34\r
+ c = hh( c, d, a, b, arr[int(i+11)], 16, 1839030562 ); // 35\r
+ b = hh( b, c, d, a, arr[int(i+14)], 23, -35309556 ); // 36\r
+ a = hh( a, b, c, d, arr[int(i+ 1)], 4, -1530992060 ); // 37\r
+ d = hh( d, a, b, c, arr[int(i+ 4)], 11, 1272893353 ); // 38\r
+ c = hh( c, d, a, b, arr[int(i+ 7)], 16, -155497632 ); // 39\r
+ b = hh( b, c, d, a, arr[int(i+10)], 23, -1094730640 ); // 40\r
+ a = hh( a, b, c, d, arr[int(i+13)], 4, 681279174 ); // 41\r
+ d = hh( d, a, b, c, arr[int(i+ 0)], 11, -358537222 ); // 42\r
+ c = hh( c, d, a, b, arr[int(i+ 3)], 16, -722521979 ); // 43\r
+ b = hh( b, c, d, a, arr[int(i+ 6)], 23, 76029189 ); // 44\r
+ a = hh( a, b, c, d, arr[int(i+ 9)], 4, -640364487 ); // 45\r
+ d = hh( d, a, b, c, arr[int(i+12)], 11, -421815835 ); // 46\r
+ c = hh( c, d, a, b, arr[int(i+15)], 16, 530742520 ); // 47\r
+ b = hh( b, c, d, a, arr[int(i+ 2)], 23, -995338651 ); // 48\r
+ \r
+ // Round 4\r
+ a = ii( a, b, c, d, arr[int(i+ 0)], 6, -198630844 ); // 49\r
+ d = ii( d, a, b, c, arr[int(i+ 7)], 10, 1126891415 ); // 50\r
+ c = ii( c, d, a, b, arr[int(i+14)], 15, -1416354905 ); // 51\r
+ b = ii( b, c, d, a, arr[int(i+ 5)], 21, -57434055 ); // 52\r
+ a = ii( a, b, c, d, arr[int(i+12)], 6, 1700485571 ); // 53\r
+ d = ii( d, a, b, c, arr[int(i+ 3)], 10, -1894986606 ); // 54\r
+ c = ii( c, d, a, b, arr[int(i+10)], 15, -1051523 ); // 55\r
+ b = ii( b, c, d, a, arr[int(i+ 1)], 21, -2054922799 ); // 56\r
+ a = ii( a, b, c, d, arr[int(i+ 8)], 6, 1873313359 ); // 57\r
+ d = ii( d, a, b, c, arr[int(i+15)], 10, -30611744 ); // 58\r
+ c = ii( c, d, a, b, arr[int(i+ 6)], 15, -1560198380 ); // 59\r
+ b = ii( b, c, d, a, arr[int(i+13)], 21, 1309151649 ); // 60\r
+ a = ii( a, b, c, d, arr[int(i+ 4)], 6, -145523070 ); // 61\r
+ d = ii( d, a, b, c, arr[int(i+11)], 10, -1120210379 ); // 62\r
+ c = ii( c, d, a, b, arr[int(i+ 2)], 15, 718787259 ); // 63\r
+ b = ii( b, c, d, a, arr[int(i+ 9)], 21, -343485551 ); // 64\r
+ \r
+ a += aa;\r
+ b += bb;\r
+ c += cc;\r
+ d += dd;\r
+ \r
+ }\r
+ \r
+ }\r
+ \r
+ private function padArray(len:int):void\r
+ { \r
+ arr[ int(len >> 5) ] |= 0x80 << ( len % 32 );\r
+ arr[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;\r
+ arrLen = arr.length;\r
+ } \r
+ \r
+ /* Code below same as com.adobe.crypto.MD5 */ \r
+ \r
+ /**\r
+ * Auxiliary function f as defined in RFC\r
+ */\r
+ private static function f( x:int, y:int, z:int ):int {\r
+ return ( x & y ) | ( (~x) & z );\r
+ }\r
+ \r
+ /**\r
+ * Auxiliary function g as defined in RFC\r
+ */\r
+ private static function g( x:int, y:int, z:int ):int {\r
+ return ( x & z ) | ( y & (~z) );\r
+ }\r
+ \r
+ /**\r
+ * Auxiliary function h as defined in RFC\r
+ */\r
+ private static function h( x:int, y:int, z:int ):int {\r
+ return x ^ y ^ z;\r
+ }\r
+ \r
+ /**\r
+ * Auxiliary function i as defined in RFC\r
+ */\r
+ private static function i( x:int, y:int, z:int ):int {\r
+ return y ^ ( x | (~z) );\r
+ }\r
+ \r
+ /**\r
+ * A generic transformation function. The logic of ff, gg, hh, and\r
+ * ii are all the same, minus the function used, so pull that logic\r
+ * out and simplify the method bodies for the transoformation functions.\r
+ */\r
+ private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {\r
+ var tmp:int = a + int( func( b, c, d ) ) + x + t;\r
+ return IntUtil.rol( tmp, s ) + b;\r
+ }\r
+ \r
+ /**\r
+ * ff transformation function\r
+ */\r
+ private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {\r
+ return transform( f, a, b, c, d, x, s, t );\r
+ }\r
+ \r
+ /**\r
+ * gg transformation function\r
+ */\r
+ private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {\r
+ return transform( g, a, b, c, d, x, s, t );\r
+ }\r
+ \r
+ /**\r
+ * hh transformation function\r
+ */\r
+ private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {\r
+ return transform( h, a, b, c, d, x, s, t );\r
+ }\r
+ \r
+ /**\r
+ * ii transformation function\r
+ */\r
+ private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {\r
+ return transform( i, a, b, c, d, x, s, t );\r
+ }\r
+ \r
+ }\r
+}
\ No newline at end of file