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