mas01mj@732: /* mas01mj@732: Copyright (c) 2008, Adobe Systems Incorporated mas01mj@732: All rights reserved. mas01mj@732: mas01mj@732: Redistribution and use in source and binary forms, with or without mas01mj@732: modification, are permitted provided that the following conditions are mas01mj@732: met: mas01mj@732: mas01mj@732: * Redistributions of source code must retain the above copyright notice, mas01mj@732: this list of conditions and the following disclaimer. mas01mj@732: mas01mj@732: * Redistributions in binary form must reproduce the above copyright mas01mj@732: notice, this list of conditions and the following disclaimer in the mas01mj@732: documentation and/or other materials provided with the distribution. mas01mj@732: mas01mj@732: * Neither the name of Adobe Systems Incorporated nor the names of its mas01mj@732: contributors may be used to endorse or promote products derived from mas01mj@732: this software without specific prior written permission. mas01mj@732: mas01mj@732: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS mas01mj@732: IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, mas01mj@732: THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR mas01mj@732: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mj@732: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mj@732: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mj@732: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mj@732: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mj@732: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mj@732: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mj@732: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mj@732: */ mas01mj@732: mas01mj@732: package com.adobe.crypto { mas01mj@732: mas01mj@732: import com.adobe.utils.IntUtil; mas01mj@732: import flash.utils.ByteArray; mas01mj@732: /** mas01mj@732: * The MD5 Message-Digest Algorithm mas01mj@732: * mas01mj@732: * Implementation based on algorithm description at mas01mj@732: * http://www.faqs.org/rfcs/rfc1321.html mas01mj@732: */ mas01mj@732: public class MD5 { mas01mj@732: mas01mj@732: public static var digest:ByteArray; mas01mj@732: /** mas01mj@732: * Performs the MD5 hash algorithm on a string. mas01mj@732: * mas01mj@732: * @param s The string to hash mas01mj@732: * @return A string containing the hash value of s mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 8.5 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: mas01mj@732: public static function hash(s:String) :String{ mas01mj@732: //Convert to byteArray and send through hashBinary function mas01mj@732: // so as to only have complex code in one location mas01mj@732: var ba:ByteArray = new ByteArray(); mas01mj@732: ba.writeUTFBytes(s); mas01mj@732: return hashBinary(ba); mas01mj@732: } mas01mj@732: mas01mj@732: public static function hashBytes(s:ByteArray) :String{ mas01mj@732: return hashBinary(s); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Performs the MD5 hash algorithm on a ByteArray. mas01mj@732: * mas01mj@732: * @param s The string to hash mas01mj@732: * @return A string containing the hash value of s mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 8.5 mas01mj@732: * @tiptext mas01mj@732: */ mas01mj@732: public static function hashBinary( s:ByteArray ):String { mas01mj@732: // initialize the md buffers mas01mj@732: var a:int = 1732584193; mas01mj@732: var b:int = -271733879; mas01mj@732: var c:int = -1732584194; mas01mj@732: var d:int = 271733878; mas01mj@732: mas01mj@732: // variables to store previous values mas01mj@732: var aa:int; mas01mj@732: var bb:int; mas01mj@732: var cc:int; mas01mj@732: var dd:int; mas01mj@732: mas01mj@732: // create the blocks from the string and mas01mj@732: // save the length as a local var to reduce mas01mj@732: // lookup in the loop below mas01mj@732: var x:Array = createBlocks( s ); mas01mj@732: var len:int = x.length; mas01mj@732: mas01mj@732: // loop over all of the blocks mas01mj@732: for ( var i:int = 0; i < len; i += 16) { mas01mj@732: // save previous values mas01mj@732: aa = a; mas01mj@732: bb = b; mas01mj@732: cc = c; mas01mj@732: dd = d; mas01mj@732: mas01mj@732: // Round 1 mas01mj@732: a = ff( a, b, c, d, x[int(i+ 0)], 7, -680876936 ); // 1 mas01mj@732: d = ff( d, a, b, c, x[int(i+ 1)], 12, -389564586 ); // 2 mas01mj@732: c = ff( c, d, a, b, x[int(i+ 2)], 17, 606105819 ); // 3 mas01mj@732: b = ff( b, c, d, a, x[int(i+ 3)], 22, -1044525330 ); // 4 mas01mj@732: a = ff( a, b, c, d, x[int(i+ 4)], 7, -176418897 ); // 5 mas01mj@732: d = ff( d, a, b, c, x[int(i+ 5)], 12, 1200080426 ); // 6 mas01mj@732: c = ff( c, d, a, b, x[int(i+ 6)], 17, -1473231341 ); // 7 mas01mj@732: b = ff( b, c, d, a, x[int(i+ 7)], 22, -45705983 ); // 8 mas01mj@732: a = ff( a, b, c, d, x[int(i+ 8)], 7, 1770035416 ); // 9 mas01mj@732: d = ff( d, a, b, c, x[int(i+ 9)], 12, -1958414417 ); // 10 mas01mj@732: c = ff( c, d, a, b, x[int(i+10)], 17, -42063 ); // 11 mas01mj@732: b = ff( b, c, d, a, x[int(i+11)], 22, -1990404162 ); // 12 mas01mj@732: a = ff( a, b, c, d, x[int(i+12)], 7, 1804603682 ); // 13 mas01mj@732: d = ff( d, a, b, c, x[int(i+13)], 12, -40341101 ); // 14 mas01mj@732: c = ff( c, d, a, b, x[int(i+14)], 17, -1502002290 ); // 15 mas01mj@732: b = ff( b, c, d, a, x[int(i+15)], 22, 1236535329 ); // 16 mas01mj@732: mas01mj@732: // Round 2 mas01mj@732: a = gg( a, b, c, d, x[int(i+ 1)], 5, -165796510 ); // 17 mas01mj@732: d = gg( d, a, b, c, x[int(i+ 6)], 9, -1069501632 ); // 18 mas01mj@732: c = gg( c, d, a, b, x[int(i+11)], 14, 643717713 ); // 19 mas01mj@732: b = gg( b, c, d, a, x[int(i+ 0)], 20, -373897302 ); // 20 mas01mj@732: a = gg( a, b, c, d, x[int(i+ 5)], 5, -701558691 ); // 21 mas01mj@732: d = gg( d, a, b, c, x[int(i+10)], 9, 38016083 ); // 22 mas01mj@732: c = gg( c, d, a, b, x[int(i+15)], 14, -660478335 ); // 23 mas01mj@732: b = gg( b, c, d, a, x[int(i+ 4)], 20, -405537848 ); // 24 mas01mj@732: a = gg( a, b, c, d, x[int(i+ 9)], 5, 568446438 ); // 25 mas01mj@732: d = gg( d, a, b, c, x[int(i+14)], 9, -1019803690 ); // 26 mas01mj@732: c = gg( c, d, a, b, x[int(i+ 3)], 14, -187363961 ); // 27 mas01mj@732: b = gg( b, c, d, a, x[int(i+ 8)], 20, 1163531501 ); // 28 mas01mj@732: a = gg( a, b, c, d, x[int(i+13)], 5, -1444681467 ); // 29 mas01mj@732: d = gg( d, a, b, c, x[int(i+ 2)], 9, -51403784 ); // 30 mas01mj@732: c = gg( c, d, a, b, x[int(i+ 7)], 14, 1735328473 ); // 31 mas01mj@732: b = gg( b, c, d, a, x[int(i+12)], 20, -1926607734 ); // 32 mas01mj@732: mas01mj@732: // Round 3 mas01mj@732: a = hh( a, b, c, d, x[int(i+ 5)], 4, -378558 ); // 33 mas01mj@732: d = hh( d, a, b, c, x[int(i+ 8)], 11, -2022574463 ); // 34 mas01mj@732: c = hh( c, d, a, b, x[int(i+11)], 16, 1839030562 ); // 35 mas01mj@732: b = hh( b, c, d, a, x[int(i+14)], 23, -35309556 ); // 36 mas01mj@732: a = hh( a, b, c, d, x[int(i+ 1)], 4, -1530992060 ); // 37 mas01mj@732: d = hh( d, a, b, c, x[int(i+ 4)], 11, 1272893353 ); // 38 mas01mj@732: c = hh( c, d, a, b, x[int(i+ 7)], 16, -155497632 ); // 39 mas01mj@732: b = hh( b, c, d, a, x[int(i+10)], 23, -1094730640 ); // 40 mas01mj@732: a = hh( a, b, c, d, x[int(i+13)], 4, 681279174 ); // 41 mas01mj@732: d = hh( d, a, b, c, x[int(i+ 0)], 11, -358537222 ); // 42 mas01mj@732: c = hh( c, d, a, b, x[int(i+ 3)], 16, -722521979 ); // 43 mas01mj@732: b = hh( b, c, d, a, x[int(i+ 6)], 23, 76029189 ); // 44 mas01mj@732: a = hh( a, b, c, d, x[int(i+ 9)], 4, -640364487 ); // 45 mas01mj@732: d = hh( d, a, b, c, x[int(i+12)], 11, -421815835 ); // 46 mas01mj@732: c = hh( c, d, a, b, x[int(i+15)], 16, 530742520 ); // 47 mas01mj@732: b = hh( b, c, d, a, x[int(i+ 2)], 23, -995338651 ); // 48 mas01mj@732: mas01mj@732: // Round 4 mas01mj@732: a = ii( a, b, c, d, x[int(i+ 0)], 6, -198630844 ); // 49 mas01mj@732: d = ii( d, a, b, c, x[int(i+ 7)], 10, 1126891415 ); // 50 mas01mj@732: c = ii( c, d, a, b, x[int(i+14)], 15, -1416354905 ); // 51 mas01mj@732: b = ii( b, c, d, a, x[int(i+ 5)], 21, -57434055 ); // 52 mas01mj@732: a = ii( a, b, c, d, x[int(i+12)], 6, 1700485571 ); // 53 mas01mj@732: d = ii( d, a, b, c, x[int(i+ 3)], 10, -1894986606 ); // 54 mas01mj@732: c = ii( c, d, a, b, x[int(i+10)], 15, -1051523 ); // 55 mas01mj@732: b = ii( b, c, d, a, x[int(i+ 1)], 21, -2054922799 ); // 56 mas01mj@732: a = ii( a, b, c, d, x[int(i+ 8)], 6, 1873313359 ); // 57 mas01mj@732: d = ii( d, a, b, c, x[int(i+15)], 10, -30611744 ); // 58 mas01mj@732: c = ii( c, d, a, b, x[int(i+ 6)], 15, -1560198380 ); // 59 mas01mj@732: b = ii( b, c, d, a, x[int(i+13)], 21, 1309151649 ); // 60 mas01mj@732: a = ii( a, b, c, d, x[int(i+ 4)], 6, -145523070 ); // 61 mas01mj@732: d = ii( d, a, b, c, x[int(i+11)], 10, -1120210379 ); // 62 mas01mj@732: c = ii( c, d, a, b, x[int(i+ 2)], 15, 718787259 ); // 63 mas01mj@732: b = ii( b, c, d, a, x[int(i+ 9)], 21, -343485551 ); // 64 mas01mj@732: mas01mj@732: a += aa; mas01mj@732: b += bb; mas01mj@732: c += cc; mas01mj@732: d += dd; mas01mj@732: } mas01mj@732: digest = new ByteArray() mas01mj@732: digest.writeInt(a); mas01mj@732: digest.writeInt(b); mas01mj@732: digest.writeInt(c); mas01mj@732: digest.writeInt(d); mas01mj@732: digest.position = 0; mas01mj@732: // Finish up by concatening the buffers with their hex output mas01mj@732: return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Auxiliary function f as defined in RFC mas01mj@732: */ mas01mj@732: private static function f( x:int, y:int, z:int ):int { mas01mj@732: return ( x & y ) | ( (~x) & z ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Auxiliary function g as defined in RFC mas01mj@732: */ mas01mj@732: private static function g( x:int, y:int, z:int ):int { mas01mj@732: return ( x & z ) | ( y & (~z) ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Auxiliary function h as defined in RFC mas01mj@732: */ mas01mj@732: private static function h( x:int, y:int, z:int ):int { mas01mj@732: return x ^ y ^ z; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Auxiliary function i as defined in RFC mas01mj@732: */ mas01mj@732: private static function i( x:int, y:int, z:int ):int { mas01mj@732: return y ^ ( x | (~z) ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * A generic transformation function. The logic of ff, gg, hh, and mas01mj@732: * ii are all the same, minus the function used, so pull that logic mas01mj@732: * out and simplify the method bodies for the transoformation functions. mas01mj@732: */ mas01mj@732: private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int { mas01mj@732: var tmp:int = a + int( func( b, c, d ) ) + x + t; mas01mj@732: return IntUtil.rol( tmp, s ) + b; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * ff transformation function mas01mj@732: */ mas01mj@732: private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { mas01mj@732: return transform( f, a, b, c, d, x, s, t ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * gg transformation function mas01mj@732: */ mas01mj@732: private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { mas01mj@732: return transform( g, a, b, c, d, x, s, t ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * hh transformation function mas01mj@732: */ mas01mj@732: private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { mas01mj@732: return transform( h, a, b, c, d, x, s, t ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * ii transformation function mas01mj@732: */ mas01mj@732: private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int { mas01mj@732: return transform( i, a, b, c, d, x, s, t ); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Converts a string to a sequence of 16-word blocks mas01mj@732: * that we'll do the processing on. Appends padding mas01mj@732: * and length in the process. mas01mj@732: * mas01mj@732: * @param s The string to split into blocks mas01mj@732: * @return An array containing the blocks that s was mas01mj@732: * split into. mas01mj@732: */ mas01mj@732: private static function createBlocks( s:ByteArray ):Array { mas01mj@732: var blocks:Array = new Array(); mas01mj@732: var len:int = s.length * 8; mas01mj@732: var mask:int = 0xFF; // ignore hi byte of characters > 0xFF mas01mj@732: for( var i:int = 0; i < len; i += 8 ) { mas01mj@732: blocks[ int(i >> 5) ] |= ( s[ i / 8 ] & mask ) << ( i % 32 ); mas01mj@732: } mas01mj@732: mas01mj@732: // append padding and length mas01mj@732: blocks[ int(len >> 5) ] |= 0x80 << ( len % 32 ); mas01mj@732: blocks[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len; mas01mj@732: return blocks; mas01mj@732: } mas01mj@732: mas01mj@732: } mas01mj@732: }