annotate bindings/as3/ext/com/adobe/crypto/MD5.as @ 770:c54bc2ffbf92 tip

update tags
author convert-repo
date Fri, 16 Dec 2011 11:34:01 +0000
parents 3a0b9700b3d2
children
rev   line source
mas01mj@732 1 /*
mas01mj@732 2 Copyright (c) 2008, Adobe Systems Incorporated
mas01mj@732 3 All rights reserved.
mas01mj@732 4
mas01mj@732 5 Redistribution and use in source and binary forms, with or without
mas01mj@732 6 modification, are permitted provided that the following conditions are
mas01mj@732 7 met:
mas01mj@732 8
mas01mj@732 9 * Redistributions of source code must retain the above copyright notice,
mas01mj@732 10 this list of conditions and the following disclaimer.
mas01mj@732 11
mas01mj@732 12 * Redistributions in binary form must reproduce the above copyright
mas01mj@732 13 notice, this list of conditions and the following disclaimer in the
mas01mj@732 14 documentation and/or other materials provided with the distribution.
mas01mj@732 15
mas01mj@732 16 * Neither the name of Adobe Systems Incorporated nor the names of its
mas01mj@732 17 contributors may be used to endorse or promote products derived from
mas01mj@732 18 this software without specific prior written permission.
mas01mj@732 19
mas01mj@732 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
mas01mj@732 21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
mas01mj@732 23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
mas01mj@732 24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
mas01mj@732 25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
mas01mj@732 26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
mas01mj@732 27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
mas01mj@732 28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
mas01mj@732 29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mas01mj@732 30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mas01mj@732 31 */
mas01mj@732 32
mas01mj@732 33 package com.adobe.crypto {
mas01mj@732 34
mas01mj@732 35 import com.adobe.utils.IntUtil;
mas01mj@732 36 import flash.utils.ByteArray;
mas01mj@732 37 /**
mas01mj@732 38 * The MD5 Message-Digest Algorithm
mas01mj@732 39 *
mas01mj@732 40 * Implementation based on algorithm description at
mas01mj@732 41 * http://www.faqs.org/rfcs/rfc1321.html
mas01mj@732 42 */
mas01mj@732 43 public class MD5 {
mas01mj@732 44
mas01mj@732 45 public static var digest:ByteArray;
mas01mj@732 46 /**
mas01mj@732 47 * Performs the MD5 hash algorithm on a string.
mas01mj@732 48 *
mas01mj@732 49 * @param s The string to hash
mas01mj@732 50 * @return A string containing the hash value of s
mas01mj@732 51 * @langversion ActionScript 3.0
mas01mj@732 52 * @playerversion Flash 8.5
mas01mj@732 53 * @tiptext
mas01mj@732 54 */
mas01mj@732 55
mas01mj@732 56 public static function hash(s:String) :String{
mas01mj@732 57 //Convert to byteArray and send through hashBinary function
mas01mj@732 58 // so as to only have complex code in one location
mas01mj@732 59 var ba:ByteArray = new ByteArray();
mas01mj@732 60 ba.writeUTFBytes(s);
mas01mj@732 61 return hashBinary(ba);
mas01mj@732 62 }
mas01mj@732 63
mas01mj@732 64 public static function hashBytes(s:ByteArray) :String{
mas01mj@732 65 return hashBinary(s);
mas01mj@732 66 }
mas01mj@732 67
mas01mj@732 68 /**
mas01mj@732 69 * Performs the MD5 hash algorithm on a ByteArray.
mas01mj@732 70 *
mas01mj@732 71 * @param s The string to hash
mas01mj@732 72 * @return A string containing the hash value of s
mas01mj@732 73 * @langversion ActionScript 3.0
mas01mj@732 74 * @playerversion Flash 8.5
mas01mj@732 75 * @tiptext
mas01mj@732 76 */
mas01mj@732 77 public static function hashBinary( s:ByteArray ):String {
mas01mj@732 78 // initialize the md buffers
mas01mj@732 79 var a:int = 1732584193;
mas01mj@732 80 var b:int = -271733879;
mas01mj@732 81 var c:int = -1732584194;
mas01mj@732 82 var d:int = 271733878;
mas01mj@732 83
mas01mj@732 84 // variables to store previous values
mas01mj@732 85 var aa:int;
mas01mj@732 86 var bb:int;
mas01mj@732 87 var cc:int;
mas01mj@732 88 var dd:int;
mas01mj@732 89
mas01mj@732 90 // create the blocks from the string and
mas01mj@732 91 // save the length as a local var to reduce
mas01mj@732 92 // lookup in the loop below
mas01mj@732 93 var x:Array = createBlocks( s );
mas01mj@732 94 var len:int = x.length;
mas01mj@732 95
mas01mj@732 96 // loop over all of the blocks
mas01mj@732 97 for ( var i:int = 0; i < len; i += 16) {
mas01mj@732 98 // save previous values
mas01mj@732 99 aa = a;
mas01mj@732 100 bb = b;
mas01mj@732 101 cc = c;
mas01mj@732 102 dd = d;
mas01mj@732 103
mas01mj@732 104 // Round 1
mas01mj@732 105 a = ff( a, b, c, d, x[int(i+ 0)], 7, -680876936 ); // 1
mas01mj@732 106 d = ff( d, a, b, c, x[int(i+ 1)], 12, -389564586 ); // 2
mas01mj@732 107 c = ff( c, d, a, b, x[int(i+ 2)], 17, 606105819 ); // 3
mas01mj@732 108 b = ff( b, c, d, a, x[int(i+ 3)], 22, -1044525330 ); // 4
mas01mj@732 109 a = ff( a, b, c, d, x[int(i+ 4)], 7, -176418897 ); // 5
mas01mj@732 110 d = ff( d, a, b, c, x[int(i+ 5)], 12, 1200080426 ); // 6
mas01mj@732 111 c = ff( c, d, a, b, x[int(i+ 6)], 17, -1473231341 ); // 7
mas01mj@732 112 b = ff( b, c, d, a, x[int(i+ 7)], 22, -45705983 ); // 8
mas01mj@732 113 a = ff( a, b, c, d, x[int(i+ 8)], 7, 1770035416 ); // 9
mas01mj@732 114 d = ff( d, a, b, c, x[int(i+ 9)], 12, -1958414417 ); // 10
mas01mj@732 115 c = ff( c, d, a, b, x[int(i+10)], 17, -42063 ); // 11
mas01mj@732 116 b = ff( b, c, d, a, x[int(i+11)], 22, -1990404162 ); // 12
mas01mj@732 117 a = ff( a, b, c, d, x[int(i+12)], 7, 1804603682 ); // 13
mas01mj@732 118 d = ff( d, a, b, c, x[int(i+13)], 12, -40341101 ); // 14
mas01mj@732 119 c = ff( c, d, a, b, x[int(i+14)], 17, -1502002290 ); // 15
mas01mj@732 120 b = ff( b, c, d, a, x[int(i+15)], 22, 1236535329 ); // 16
mas01mj@732 121
mas01mj@732 122 // Round 2
mas01mj@732 123 a = gg( a, b, c, d, x[int(i+ 1)], 5, -165796510 ); // 17
mas01mj@732 124 d = gg( d, a, b, c, x[int(i+ 6)], 9, -1069501632 ); // 18
mas01mj@732 125 c = gg( c, d, a, b, x[int(i+11)], 14, 643717713 ); // 19
mas01mj@732 126 b = gg( b, c, d, a, x[int(i+ 0)], 20, -373897302 ); // 20
mas01mj@732 127 a = gg( a, b, c, d, x[int(i+ 5)], 5, -701558691 ); // 21
mas01mj@732 128 d = gg( d, a, b, c, x[int(i+10)], 9, 38016083 ); // 22
mas01mj@732 129 c = gg( c, d, a, b, x[int(i+15)], 14, -660478335 ); // 23
mas01mj@732 130 b = gg( b, c, d, a, x[int(i+ 4)], 20, -405537848 ); // 24
mas01mj@732 131 a = gg( a, b, c, d, x[int(i+ 9)], 5, 568446438 ); // 25
mas01mj@732 132 d = gg( d, a, b, c, x[int(i+14)], 9, -1019803690 ); // 26
mas01mj@732 133 c = gg( c, d, a, b, x[int(i+ 3)], 14, -187363961 ); // 27
mas01mj@732 134 b = gg( b, c, d, a, x[int(i+ 8)], 20, 1163531501 ); // 28
mas01mj@732 135 a = gg( a, b, c, d, x[int(i+13)], 5, -1444681467 ); // 29
mas01mj@732 136 d = gg( d, a, b, c, x[int(i+ 2)], 9, -51403784 ); // 30
mas01mj@732 137 c = gg( c, d, a, b, x[int(i+ 7)], 14, 1735328473 ); // 31
mas01mj@732 138 b = gg( b, c, d, a, x[int(i+12)], 20, -1926607734 ); // 32
mas01mj@732 139
mas01mj@732 140 // Round 3
mas01mj@732 141 a = hh( a, b, c, d, x[int(i+ 5)], 4, -378558 ); // 33
mas01mj@732 142 d = hh( d, a, b, c, x[int(i+ 8)], 11, -2022574463 ); // 34
mas01mj@732 143 c = hh( c, d, a, b, x[int(i+11)], 16, 1839030562 ); // 35
mas01mj@732 144 b = hh( b, c, d, a, x[int(i+14)], 23, -35309556 ); // 36
mas01mj@732 145 a = hh( a, b, c, d, x[int(i+ 1)], 4, -1530992060 ); // 37
mas01mj@732 146 d = hh( d, a, b, c, x[int(i+ 4)], 11, 1272893353 ); // 38
mas01mj@732 147 c = hh( c, d, a, b, x[int(i+ 7)], 16, -155497632 ); // 39
mas01mj@732 148 b = hh( b, c, d, a, x[int(i+10)], 23, -1094730640 ); // 40
mas01mj@732 149 a = hh( a, b, c, d, x[int(i+13)], 4, 681279174 ); // 41
mas01mj@732 150 d = hh( d, a, b, c, x[int(i+ 0)], 11, -358537222 ); // 42
mas01mj@732 151 c = hh( c, d, a, b, x[int(i+ 3)], 16, -722521979 ); // 43
mas01mj@732 152 b = hh( b, c, d, a, x[int(i+ 6)], 23, 76029189 ); // 44
mas01mj@732 153 a = hh( a, b, c, d, x[int(i+ 9)], 4, -640364487 ); // 45
mas01mj@732 154 d = hh( d, a, b, c, x[int(i+12)], 11, -421815835 ); // 46
mas01mj@732 155 c = hh( c, d, a, b, x[int(i+15)], 16, 530742520 ); // 47
mas01mj@732 156 b = hh( b, c, d, a, x[int(i+ 2)], 23, -995338651 ); // 48
mas01mj@732 157
mas01mj@732 158 // Round 4
mas01mj@732 159 a = ii( a, b, c, d, x[int(i+ 0)], 6, -198630844 ); // 49
mas01mj@732 160 d = ii( d, a, b, c, x[int(i+ 7)], 10, 1126891415 ); // 50
mas01mj@732 161 c = ii( c, d, a, b, x[int(i+14)], 15, -1416354905 ); // 51
mas01mj@732 162 b = ii( b, c, d, a, x[int(i+ 5)], 21, -57434055 ); // 52
mas01mj@732 163 a = ii( a, b, c, d, x[int(i+12)], 6, 1700485571 ); // 53
mas01mj@732 164 d = ii( d, a, b, c, x[int(i+ 3)], 10, -1894986606 ); // 54
mas01mj@732 165 c = ii( c, d, a, b, x[int(i+10)], 15, -1051523 ); // 55
mas01mj@732 166 b = ii( b, c, d, a, x[int(i+ 1)], 21, -2054922799 ); // 56
mas01mj@732 167 a = ii( a, b, c, d, x[int(i+ 8)], 6, 1873313359 ); // 57
mas01mj@732 168 d = ii( d, a, b, c, x[int(i+15)], 10, -30611744 ); // 58
mas01mj@732 169 c = ii( c, d, a, b, x[int(i+ 6)], 15, -1560198380 ); // 59
mas01mj@732 170 b = ii( b, c, d, a, x[int(i+13)], 21, 1309151649 ); // 60
mas01mj@732 171 a = ii( a, b, c, d, x[int(i+ 4)], 6, -145523070 ); // 61
mas01mj@732 172 d = ii( d, a, b, c, x[int(i+11)], 10, -1120210379 ); // 62
mas01mj@732 173 c = ii( c, d, a, b, x[int(i+ 2)], 15, 718787259 ); // 63
mas01mj@732 174 b = ii( b, c, d, a, x[int(i+ 9)], 21, -343485551 ); // 64
mas01mj@732 175
mas01mj@732 176 a += aa;
mas01mj@732 177 b += bb;
mas01mj@732 178 c += cc;
mas01mj@732 179 d += dd;
mas01mj@732 180 }
mas01mj@732 181 digest = new ByteArray()
mas01mj@732 182 digest.writeInt(a);
mas01mj@732 183 digest.writeInt(b);
mas01mj@732 184 digest.writeInt(c);
mas01mj@732 185 digest.writeInt(d);
mas01mj@732 186 digest.position = 0;
mas01mj@732 187 // Finish up by concatening the buffers with their hex output
mas01mj@732 188 return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d );
mas01mj@732 189 }
mas01mj@732 190
mas01mj@732 191 /**
mas01mj@732 192 * Auxiliary function f as defined in RFC
mas01mj@732 193 */
mas01mj@732 194 private static function f( x:int, y:int, z:int ):int {
mas01mj@732 195 return ( x & y ) | ( (~x) & z );
mas01mj@732 196 }
mas01mj@732 197
mas01mj@732 198 /**
mas01mj@732 199 * Auxiliary function g as defined in RFC
mas01mj@732 200 */
mas01mj@732 201 private static function g( x:int, y:int, z:int ):int {
mas01mj@732 202 return ( x & z ) | ( y & (~z) );
mas01mj@732 203 }
mas01mj@732 204
mas01mj@732 205 /**
mas01mj@732 206 * Auxiliary function h as defined in RFC
mas01mj@732 207 */
mas01mj@732 208 private static function h( x:int, y:int, z:int ):int {
mas01mj@732 209 return x ^ y ^ z;
mas01mj@732 210 }
mas01mj@732 211
mas01mj@732 212 /**
mas01mj@732 213 * Auxiliary function i as defined in RFC
mas01mj@732 214 */
mas01mj@732 215 private static function i( x:int, y:int, z:int ):int {
mas01mj@732 216 return y ^ ( x | (~z) );
mas01mj@732 217 }
mas01mj@732 218
mas01mj@732 219 /**
mas01mj@732 220 * A generic transformation function. The logic of ff, gg, hh, and
mas01mj@732 221 * ii are all the same, minus the function used, so pull that logic
mas01mj@732 222 * out and simplify the method bodies for the transoformation functions.
mas01mj@732 223 */
mas01mj@732 224 private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {
mas01mj@732 225 var tmp:int = a + int( func( b, c, d ) ) + x + t;
mas01mj@732 226 return IntUtil.rol( tmp, s ) + b;
mas01mj@732 227 }
mas01mj@732 228
mas01mj@732 229 /**
mas01mj@732 230 * ff transformation function
mas01mj@732 231 */
mas01mj@732 232 private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
mas01mj@732 233 return transform( f, a, b, c, d, x, s, t );
mas01mj@732 234 }
mas01mj@732 235
mas01mj@732 236 /**
mas01mj@732 237 * gg transformation function
mas01mj@732 238 */
mas01mj@732 239 private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
mas01mj@732 240 return transform( g, a, b, c, d, x, s, t );
mas01mj@732 241 }
mas01mj@732 242
mas01mj@732 243 /**
mas01mj@732 244 * hh transformation function
mas01mj@732 245 */
mas01mj@732 246 private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
mas01mj@732 247 return transform( h, a, b, c, d, x, s, t );
mas01mj@732 248 }
mas01mj@732 249
mas01mj@732 250 /**
mas01mj@732 251 * ii transformation function
mas01mj@732 252 */
mas01mj@732 253 private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
mas01mj@732 254 return transform( i, a, b, c, d, x, s, t );
mas01mj@732 255 }
mas01mj@732 256
mas01mj@732 257 /**
mas01mj@732 258 * Converts a string to a sequence of 16-word blocks
mas01mj@732 259 * that we'll do the processing on. Appends padding
mas01mj@732 260 * and length in the process.
mas01mj@732 261 *
mas01mj@732 262 * @param s The string to split into blocks
mas01mj@732 263 * @return An array containing the blocks that s was
mas01mj@732 264 * split into.
mas01mj@732 265 */
mas01mj@732 266 private static function createBlocks( s:ByteArray ):Array {
mas01mj@732 267 var blocks:Array = new Array();
mas01mj@732 268 var len:int = s.length * 8;
mas01mj@732 269 var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
mas01mj@732 270 for( var i:int = 0; i < len; i += 8 ) {
mas01mj@732 271 blocks[ int(i >> 5) ] |= ( s[ i / 8 ] & mask ) << ( i % 32 );
mas01mj@732 272 }
mas01mj@732 273
mas01mj@732 274 // append padding and length
mas01mj@732 275 blocks[ int(len >> 5) ] |= 0x80 << ( len % 32 );
mas01mj@732 276 blocks[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;
mas01mj@732 277 return blocks;
mas01mj@732 278 }
mas01mj@732 279
mas01mj@732 280 }
mas01mj@732 281 }