PageRenderTime 43ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/flash-src/third-party/com/gsolo/encryption/SHA1.as

http://github.com/gimite/web-socket-js
ActionScript | 218 lines | 133 code | 30 blank | 55 comment | 17 complexity | b13fac7b871bf7eff770d28dd2fc7f59 MD5 | raw file
  1package com.gsolo.encryption {
  2	public class SHA1 {
  3		/*
  4		 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  5		 * in FIPS PUB 180-1
  6		 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  7		 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  8		 * Distributed under the BSD License
  9		 * See http://pajhome.org.uk/crypt/md5 for details.
 10		 *
 11		 * Converted to AS3 By Geoffrey Williams
 12		 */
 13		
 14		/*
 15		 * Configurable variables. You may need to tweak these to be compatible with
 16		 * the server-side, but the defaults work in most cases.
 17		 */
 18		 
 19		public static const HEX_FORMAT_LOWERCASE:uint = 0;
 20		public static const HEX_FORMAT_UPPERCASE:uint = 1;
 21		
 22		public static const BASE64_PAD_CHARACTER_DEFAULT_COMPLIANCE:String = "";
 23		public static const BASE64_PAD_CHARACTER_RFC_COMPLIANCE:String = "=";
 24		
 25		public static const BITS_PER_CHAR_ASCII:uint = 8;
 26		public static const BITS_PER_CHAR_UNICODE:uint = 8;
 27		 
 28		public static var hexcase:uint = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
 29		public static var b64pad:String  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
 30		public static var chrsz:uint   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */
 31		
 32		public static function encrypt (string:String):String {
 33			return hex_sha1 (string);
 34		}
 35		
 36		/*
 37		 * These are the functions you'll usually want to call
 38		 * They take string arguments and return either hex or base-64 encoded strings
 39		 */
 40		public static function hex_sha1 (string:String):String {
 41			return binb2hex (core_sha1( str2binb(string), string.length * chrsz));
 42		}
 43		
 44		public static function b64_sha1 (string:String):String {
 45			return binb2b64 (core_sha1 (str2binb (string), string.length * chrsz));
 46		}
 47		
 48		public static function str_sha1 (string:String):String {
 49			return binb2str (core_sha1 (str2binb (string), string.length * chrsz));
 50		}
 51		
 52		public static function hex_hmac_sha1 (key:String, data:String):String {
 53			return binb2hex (core_hmac_sha1 (key, data));
 54		}
 55		
 56		public static function b64_hmac_sha1 (key:String, data:String):String {
 57			return binb2b64 (core_hmac_sha1 (key, data));
 58		}
 59		
 60		public static function str_hmac_sha1 (key:String, data:String):String {
 61			return binb2str (core_hmac_sha1 (key, data));
 62		}
 63		
 64		/*
 65		 * Perform a simple self-test to see if the VM is working
 66		 */
 67		public static function sha1_vm_test ():Boolean {
 68		  return hex_sha1 ("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
 69		}
 70		
 71		/*
 72		 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 73		 */
 74		public static function core_sha1 (x:Array, len:Number):Array {
 75			/* append padding */
 76			x[len >> 5] |= 0x80 << (24 - len % 32);
 77			x[((len + 64 >> 9) << 4) + 15] = len;
 78		
 79			var w:Array = new Array(80);
 80			var a:Number =  1732584193;
 81			var b:Number = -271733879;
 82			var c:Number = -1732584194;
 83			var d:Number =  271733878;
 84			var e:Number = -1009589776;
 85			
 86			for(var i:Number = 0; i < x.length; i += 16) {
 87				var olda:Number = a;
 88				var oldb:Number = b;
 89				var oldc:Number = c;
 90				var oldd:Number = d;
 91				var olde:Number = e;
 92				
 93				for(var j:Number = 0; j < 80; j++) {
 94					if(j < 16) w[j] = x[i + j];
 95				     else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
 96					var t:Number = safe_add (safe_add (rol (a, 5), sha1_ft (j, b, c, d)), safe_add (safe_add (e, w[j]), sha1_kt (j)));
 97					e = d;
 98					d = c;
 99					c = rol(b, 30);
100					b = a;
101					a = t;
102				}
103				
104				a = safe_add(a, olda);
105				b = safe_add(b, oldb);
106				c = safe_add(c, oldc);
107				d = safe_add(d, oldd);
108				e = safe_add(e, olde);
109			}
110			  
111			return [a, b, c, d, e];
112		}
113		
114		/*
115		 * Perform the appropriate triplet combination function for the current
116		 * iteration
117		 */
118		public static function sha1_ft (t:Number, b:Number, c:Number, d:Number):Number {
119			if(t < 20) return (b & c) | ((~b) & d);
120			if(t < 40) return b ^ c ^ d;
121			if(t < 60) return (b & c) | (b & d) | (c & d);
122			return b ^ c ^ d;
123		}
124		
125		/*
126		 * Determine the appropriate additive constant for the current iteration
127		 */
128		public static function sha1_kt (t:Number):Number {
129			return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 : (t < 60) ? -1894007588 : -899497514;
130		}
131		
132		/*
133		 * Calculate the HMAC-SHA1 of a key and some data
134		 */
135		public static function core_hmac_sha1 (key:String, data:String):Array {
136		  var bkey:Array = str2binb (key);
137		  if (bkey.length > 16) bkey = core_sha1 (bkey, key.length * chrsz);
138		
139		  var ipad:Array = new Array(16), opad:Array = new Array(16);
140		  for(var i:Number = 0; i < 16; i++) {
141			ipad[i] = bkey[i] ^ 0x36363636;
142			opad[i] = bkey[i] ^ 0x5C5C5C5C;
143		  }
144		
145		  var hash:Array = core_sha1 (ipad.concat (str2binb(data)), 512 + data.length * chrsz);
146		  return core_sha1 (opad.concat (hash), 512 + 160);
147		}
148		
149		/*
150		 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
151		 * to work around bugs in some JS interpreters.
152		 */
153		public static function safe_add (x:Number, y:Number):Number {
154			var lsw:Number = (x & 0xFFFF) + (y & 0xFFFF);
155			var msw:Number = (x >> 16) + (y >> 16) + (lsw >> 16);
156			return (msw << 16) | (lsw & 0xFFFF);
157		}
158		
159		/*
160		 * Bitwise rotate a 32-bit number to the left.
161		 */
162		public static function rol (num:Number, cnt:Number):Number {
163			return (num << cnt) | (num >>> (32 - cnt));
164		}
165		
166		/*
167		 * Convert an 8-bit or 16-bit string to an array of big-endian words
168		 * In 8-bit function, characters >255 have their hi-byte silently ignored.
169		 */
170		public static function str2binb (str:String):Array {
171		  var bin:Array = new Array ();
172		  var mask:Number = (1 << chrsz) - 1;
173		  for (var i:Number = 0; i < str.length * chrsz; i += chrsz) bin[i>>5] |= (str.charCodeAt (i / chrsz) & mask) << (32 - chrsz - i%32);
174		  return bin;
175		}
176		
177		/*
178		 * Convert an array of big-endian words to a string
179		 */
180		public static function binb2str (bin:Array):String {
181			var str:String = "";
182			var mask:Number = (1 << chrsz) - 1;
183			for (var i:Number = 0; i < bin.length * 32; i += chrsz) str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
184			return str;
185		}
186		
187		/*
188		 * Convert an array of big-endian words to a hex string.
189		 */
190		public static function binb2hex (binarray:Array):String {
191			var hex_tab:String = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
192			var str:String = "";
193			for(var i:Number = 0; i < binarray.length * 4; i++) {
194			str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
195			          hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
196			}
197			return str;
198		}
199		
200		/*
201		 * Convert an array of big-endian words to a base-64 string
202		 */
203		public static function binb2b64 (binarray:Array):String {
204			var tab:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
205			var str:String = "";
206			for(var i:Number = 0; i < binarray.length * 4; i += 3) {
207				var triplet:Number = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
208		                | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
209		                |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
210				for(var j:Number = 0; j < 4; j++) {
211					if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
212					else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
213				}
214			}
215			return str;
216		}		
217	}
218}