PageRenderTime 588ms CodeModel.GetById 241ms app.highlight 116ms RepoModel.GetById 227ms app.codeStats 0ms

/flash-src/third-party/com/hurlant/crypto/hash/MD5.as

http://github.com/gimite/web-socket-js
ActionScript | 204 lines | 156 code | 20 blank | 28 comment | 4 complexity | c5e7c3a4e53c1145675d5697e6216def MD5 | raw file
  1/**
  2 * MD5
  3 * 
  4 * An ActionScript 3 implementation of the RSA Data Security, Inc. MD5 Message
  5 * Digest Algorithm, as defined in RFC 1321.
  6 * Copyright (c) 2007 Henri Torgemane
  7 * 
  8 * Derived from
  9 * 		A JavaScript implementation of the same.
 10 *		Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 11 * 		Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 12 * 
 13 * Note:
 14 * This algorithm should not be your first choice for new developements, but is
 15 * included to allow interoperability with existing codes and protocols.
 16 * 
 17 * See LICENSE.txt for full license information.
 18 */
 19package com.hurlant.crypto.hash
 20{
 21	import flash.utils.ByteArray;
 22	import flash.utils.Endian;
 23
 24 	public class MD5 implements IHash
 25	{
 26		public static const HASH_SIZE:int = 16;
 27		public var pad_size:int = 48;
 28
 29        public function MD5() { }
 30		
 31		public function getInputSize():uint
 32		{
 33			return 64;
 34		}
 35		
 36		public function getHashSize():uint
 37		{
 38			return HASH_SIZE;
 39		}
 40		
 41		public function getPadSize():int 
 42		{
 43			return pad_size;
 44		}
 45		
 46		public function hash(src:ByteArray):ByteArray
 47		{
 48			var len:uint = src.length *8;
 49			var savedEndian:String = src.endian;
 50			// pad to nearest int.
 51			while (src.length%4!=0) {
 52				src[src.length]=0;
 53			}
 54			// convert ByteArray to an array of uint
 55			src.position=0;
 56			var a:Array = [];
 57			src.endian=Endian.LITTLE_ENDIAN
 58			for (var i:uint=0;i<src.length;i+=4) {
 59				a.push(src.readUnsignedInt());
 60			}
 61			var h:Array = core_md5(a, len);
 62			var out:ByteArray = new ByteArray;
 63			out.endian=Endian.LITTLE_ENDIAN;
 64			for (i=0;i<4;i++) {
 65				out.writeUnsignedInt(h[i]);
 66			}
 67			// restore length!
 68			src.length = len/8;
 69			src.endian = savedEndian;
 70			
 71			return out;
 72		}
 73		
 74		private function core_md5(x:Array, len:uint):Array {
 75		  /* append padding */
 76		  x[len >> 5] |= 0x80 << ((len) % 32);
 77		  x[(((len + 64) >>> 9) << 4) + 14] = len;
 78
 79		  var a:uint = 0x67452301; // 1732584193;
 80		  var b:uint = 0xEFCDAB89; //-271733879;
 81		  var c:uint = 0x98BADCFE; //-1732584194;
 82		  var d:uint = 0x10325476; // 271733878;
 83
 84		  for(var i:uint = 0; i < x.length; i += 16)
 85		  {
 86		  	x[i]||=0;    x[i+1]||=0;  x[i+2]||=0;  x[i+3]||=0;
 87		  	x[i+4]||=0;  x[i+5]||=0;  x[i+6]||=0;  x[i+7]||=0;
 88		  	x[i+8]||=0;  x[i+9]||=0;  x[i+10]||=0; x[i+11]||=0;
 89		  	x[i+12]||=0; x[i+13]||=0; x[i+14]||=0; x[i+15]||=0;
 90
 91		    var olda:uint = a;
 92		    var oldb:uint = b;
 93		    var oldc:uint = c;
 94		    var oldd:uint = d;
 95		    
 96		    a = ff(a, b, c, d, x[i+ 0], 7 , 0xD76AA478);
 97		    d = ff(d, a, b, c, x[i+ 1], 12, 0xE8C7B756);
 98		    c = ff(c, d, a, b, x[i+ 2], 17, 0x242070DB);
 99		    b = ff(b, c, d, a, x[i+ 3], 22, 0xC1BDCEEE);
100		    a = ff(a, b, c, d, x[i+ 4], 7 , 0xF57C0FAF);
101		    d = ff(d, a, b, c, x[i+ 5], 12, 0x4787C62A);
102		    c = ff(c, d, a, b, x[i+ 6], 17, 0xA8304613);
103		    b = ff(b, c, d, a, x[i+ 7], 22, 0xFD469501);
104		    a = ff(a, b, c, d, x[i+ 8], 7 , 0x698098D8);
105		    d = ff(d, a, b, c, x[i+ 9], 12, 0x8B44F7AF);
106		    c = ff(c, d, a, b, x[i+10], 17, 0xFFFF5BB1);
107		    b = ff(b, c, d, a, x[i+11], 22, 0x895CD7BE);
108		    a = ff(a, b, c, d, x[i+12], 7 , 0x6B901122);
109		    d = ff(d, a, b, c, x[i+13], 12, 0xFD987193);
110		    c = ff(c, d, a, b, x[i+14], 17, 0xA679438E);
111		    b = ff(b, c, d, a, x[i+15], 22, 0x49B40821);
112
113		    a = gg(a, b, c, d, x[i+ 1], 5 , 0xf61e2562);
114		    d = gg(d, a, b, c, x[i+ 6], 9 , 0xc040b340);
115		    c = gg(c, d, a, b, x[i+11], 14, 0x265e5a51);
116		    b = gg(b, c, d, a, x[i+ 0], 20, 0xe9b6c7aa);
117		    a = gg(a, b, c, d, x[i+ 5], 5 , 0xd62f105d);
118		    d = gg(d, a, b, c, x[i+10], 9 ,  0x2441453);
119		    c = gg(c, d, a, b, x[i+15], 14, 0xd8a1e681);
120		    b = gg(b, c, d, a, x[i+ 4], 20, 0xe7d3fbc8);
121		    a = gg(a, b, c, d, x[i+ 9], 5 , 0x21e1cde6);
122		    d = gg(d, a, b, c, x[i+14], 9 , 0xc33707d6);
123		    c = gg(c, d, a, b, x[i+ 3], 14, 0xf4d50d87);
124		    b = gg(b, c, d, a, x[i+ 8], 20, 0x455a14ed);
125		    a = gg(a, b, c, d, x[i+13], 5 , 0xa9e3e905);
126		    d = gg(d, a, b, c, x[i+ 2], 9 , 0xfcefa3f8);
127		    c = gg(c, d, a, b, x[i+ 7], 14, 0x676f02d9);
128		    b = gg(b, c, d, a, x[i+12], 20, 0x8d2a4c8a);
129
130		    a = hh(a, b, c, d, x[i+ 5], 4 , 0xfffa3942);
131		    d = hh(d, a, b, c, x[i+ 8], 11, 0x8771f681);
132		    c = hh(c, d, a, b, x[i+11], 16, 0x6d9d6122);
133		    b = hh(b, c, d, a, x[i+14], 23, 0xfde5380c);
134		    a = hh(a, b, c, d, x[i+ 1], 4 , 0xa4beea44);
135		    d = hh(d, a, b, c, x[i+ 4], 11, 0x4bdecfa9);
136		    c = hh(c, d, a, b, x[i+ 7], 16, 0xf6bb4b60);
137		    b = hh(b, c, d, a, x[i+10], 23, 0xbebfbc70);
138		    a = hh(a, b, c, d, x[i+13], 4 , 0x289b7ec6);
139		    d = hh(d, a, b, c, x[i+ 0], 11, 0xeaa127fa);
140		    c = hh(c, d, a, b, x[i+ 3], 16, 0xd4ef3085);
141		    b = hh(b, c, d, a, x[i+ 6], 23,  0x4881d05);
142		    a = hh(a, b, c, d, x[i+ 9], 4 , 0xd9d4d039);
143		    d = hh(d, a, b, c, x[i+12], 11, 0xe6db99e5);
144		    c = hh(c, d, a, b, x[i+15], 16, 0x1fa27cf8);
145		    b = hh(b, c, d, a, x[i+ 2], 23, 0xc4ac5665);
146		
147		    a = ii(a, b, c, d, x[i+ 0], 6 , 0xf4292244);
148		    d = ii(d, a, b, c, x[i+ 7], 10, 0x432aff97);
149		    c = ii(c, d, a, b, x[i+14], 15, 0xab9423a7);
150		    b = ii(b, c, d, a, x[i+ 5], 21, 0xfc93a039);
151		    a = ii(a, b, c, d, x[i+12], 6 , 0x655b59c3);
152		    d = ii(d, a, b, c, x[i+ 3], 10, 0x8f0ccc92);
153		    c = ii(c, d, a, b, x[i+10], 15, 0xffeff47d);
154		    b = ii(b, c, d, a, x[i+ 1], 21, 0x85845dd1);
155		    a = ii(a, b, c, d, x[i+ 8], 6 , 0x6fa87e4f);
156		    d = ii(d, a, b, c, x[i+15], 10, 0xfe2ce6e0);
157		    c = ii(c, d, a, b, x[i+ 6], 15, 0xa3014314);
158		    b = ii(b, c, d, a, x[i+13], 21, 0x4e0811a1);
159		    a = ii(a, b, c, d, x[i+ 4], 6 , 0xf7537e82);
160		    d = ii(d, a, b, c, x[i+11], 10, 0xbd3af235);
161		    c = ii(c, d, a, b, x[i+ 2], 15, 0x2ad7d2bb);
162		    b = ii(b, c, d, a, x[i+ 9], 21, 0xeb86d391);
163		
164			a += olda;
165			b += oldb;
166			c += oldc;
167			d += oldd;
168			
169		  }
170		  return [ a, b, c, d ];
171		}
172
173		/*
174		 * Bitwise rotate a 32-bit number to the left.
175		 */
176		private function rol(num:uint, cnt:uint):uint
177		{
178		  return (num << cnt) | (num >>> (32 - cnt));
179		}
180
181		/*
182		 * These functions implement the four basic operations the algorithm uses.
183		 */
184		private function cmn(q:uint, a:uint, b:uint, x:uint, s:uint, t:uint):uint {
185		  return rol(a + q + x + t, s) + b;
186		}
187		private function ff(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
188		  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
189		}
190		private function gg(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
191		  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
192		}
193		private function hh(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
194		  return cmn(b ^ c ^ d, a, b, x, s, t);
195		}
196		private function ii(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
197		  return cmn(c ^ (b | (~d)), a, b, x, s, t);
198		}
199
200		public function toString():String {
201			return "md5";
202		}
203	}
204}