MD5Stream.as 17 KB

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