2 Copyright (c) 2008, Adobe Systems Incorporated
\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
9 * Redistributions of source code must retain the above copyright notice,
\r
10 this list of conditions and the following disclaimer.
\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
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
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
33 package com.adobe.crypto
\r
35 import com.adobe.utils.IntUtil;
\r
36 import flash.utils.ByteArray;
\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
51 public class MD5Stream
\r
53 private static var mask:int = 0xFF;
\r
55 private var arr:Array = [];
\r
57 /* running count of length */
\r
58 private var arrLen:int;
\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
66 // variables to store previous values
\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
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
83 public var memoryBlockSize:int = 16384;
\r
86 public function MD5Stream()
\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
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
104 public function complete(input:ByteArray=null):String
\r
106 if ( arr.length == 0 )
\r
108 if ( input == null )
\r
110 throw new Error("null input to complete without prior call to update. At least an empty bytearray must be passed.");
\r
114 if ( input != null )
\r
116 readIntoArray(input);
\r
119 //pad, append length
\r
122 hashRemainingChunks(false);
\r
124 var res:String = IntUtil.toHex( a ) + IntUtil.toHex( b ) +
\r
125 IntUtil.toHex( c ) + IntUtil.toHex( d );
\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
137 * @param input The bytearray chunk to perform the hash on
\r
138 * @langversion ActionScript 3.0
\r
139 * @playerversion Flash 8.5
\r
142 public function update(input:ByteArray):void
\r
144 readIntoArray(input);
\r
145 hashRemainingChunks();
\r
149 * Re-initialize this instance for use to perform hashing on
\r
150 * another input stream. This is called automatically by
\r
153 * @langversion ActionScript 3.0
\r
154 * @playerversion Flash 8.5
\r
157 public function resetFields():void
\r
163 // initialize the md buffers
\r
169 // variables to store previous values
\r
176 arrProcessIndex = 0;
\r
180 /** read into arr and free up used blocks of arr */
\r
181 private function readIntoArray(input:ByteArray):void
\r
183 var closestChunkLen:int = input.length * 8;
\r
184 arrLen += closestChunkLen;
\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
191 if ( arrProcessIndex - cleanIndex > memoryBlockSize )
\r
193 var newarr:Array= new Array();
\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
199 newarr[j] = arr[j];
\r
202 cleanIndex = arrProcessIndex;
\r
207 for ( var k:int = 0; k < closestChunkLen; k+=8 )
\r
209 //discard high bytes (convert to uint)
\r
210 arr[ int(arrIndexLen >> 5) ] |= ( input[ k / 8 ] & mask ) << ( arrIndexLen % 32 );
\r
217 private function hashRemainingChunks(bUpdate:Boolean=true):void
\r
219 var len:int = arr.length;
\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
230 /* don't do anything if don't have a 16 word block. */
\r
231 if ( arrProcessIndex >= len || len - arrProcessIndex < 15 )
\r
237 for ( var i:int = arrProcessIndex; i < len ; i += 16, arrProcessIndex += 16)
\r
239 // save previous values
\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
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
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
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
326 private function padArray(len:int):void
\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
333 /* Code below same as com.adobe.crypto.MD5 */
\r
336 * Auxiliary function f as defined in RFC
\r
338 private static function f( x:int, y:int, z:int ):int {
\r
339 return ( x & y ) | ( (~x) & z );
\r
343 * Auxiliary function g as defined in RFC
\r
345 private static function g( x:int, y:int, z:int ):int {
\r
346 return ( x & z ) | ( y & (~z) );
\r
350 * Auxiliary function h as defined in RFC
\r
352 private static function h( x:int, y:int, z:int ):int {
\r
357 * Auxiliary function i as defined in RFC
\r
359 private static function i( x:int, y:int, z:int ):int {
\r
360 return y ^ ( x | (~z) );
\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
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
374 * ff transformation function
\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
381 * gg transformation function
\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
388 * hh transformation function
\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
395 * ii transformation function
\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