PageRenderTime 250ms CodeModel.GetById 80ms app.highlight 69ms RepoModel.GetById 93ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/hash/SHA1.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 106 lines | 68 code | 12 blank | 26 comment | 8 complexity | ba97b95bebda5bdd3fe2063e9c7e21cd MD5 | raw file
  1/**
  2 * SHA1
  3 * 
  4 * An ActionScript 3 implementation of Secure Hash Algorithm, SHA-1, as defined
  5 * in FIPS PUB 180-1
  6 * Copyright (c) 2007 Henri Torgemane
  7 * 
  8 * Derived from:
  9 * 		A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 10 * 		in FIPS PUB 180-1
 11 * 		Version 2.1a Copyright Paul Johnston 2000 - 2002.
 12 * 		Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 13 * 
 14 * See LICENSE.txt for full license information.
 15 */
 16package com.hurlant.crypto.hash
 17{
 18	
 19
 20	public class SHA1 extends SHABase implements IHash
 21	{
 22		public static const HASH_SIZE:int = 20;
 23		
 24		public override function getHashSize():uint {
 25			return HASH_SIZE;
 26		}
 27		
 28		protected override function core(x:Array, len:uint):Array
 29		{
 30		  /* append padding */
 31		  x[len >> 5] |= 0x80 << (24 - len % 32);
 32		  x[((len + 64 >> 9) << 4) + 15] = len;
 33		
 34		  var w:Array = [];
 35		  var a:uint =  0x67452301; //1732584193;
 36		  var b:uint = 0xEFCDAB89; //-271733879;
 37		  var c:uint = 0x98BADCFE; //-1732584194;
 38		  var d:uint = 0x10325476; //271733878;
 39		  var e:uint = 0xC3D2E1F0; //-1009589776;
 40		
 41		  for(var i:uint = 0; i < x.length; i += 16)
 42		  {
 43		  	
 44		    var olda:uint = a;
 45		    var oldb:uint = b;
 46		    var oldc:uint = c;
 47		    var oldd:uint = d;
 48		    var olde:uint = e;
 49		
 50		    for(var j:uint = 0; j < 80; j++)
 51		    {
 52		      if (j < 16) {
 53		      	w[j] = x[i + j] || 0;
 54		      } else {
 55		      	w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
 56		      }
 57		      var t:uint = rol(a,5) + ft(j,b,c,d) + e + w[j] + kt(j);
 58		      e = d;
 59		      d = c;
 60		      c = rol(b, 30);
 61		      b = a;
 62		      a = t;
 63		    }
 64			a += olda;
 65			b += oldb;
 66			c += oldc;
 67			d += oldd;
 68			e += olde;
 69		  }
 70		  return [ a, b, c, d, e ];
 71		
 72		}
 73		
 74		/*
 75		 * Bitwise rotate a 32-bit number to the left.
 76		 */
 77		private function rol(num:uint, cnt:uint):uint
 78		{
 79		  return (num << cnt) | (num >>> (32 - cnt));
 80		}
 81		
 82		/*
 83		 * Perform the appropriate triplet combination function for the current
 84		 * iteration
 85		 */
 86		private function ft(t:uint, b:uint, c:uint, d:uint):uint
 87		{
 88		  if(t < 20) return (b & c) | ((~b) & d);
 89		  if(t < 40) return b ^ c ^ d;
 90		  if(t < 60) return (b & c) | (b & d) | (c & d);
 91		  return b ^ c ^ d;
 92		}
 93		
 94		/*
 95		 * Determine the appropriate additive constant for the current iteration
 96		 */
 97		private function kt(t:uint):uint
 98		{
 99		  return (t < 20) ? 0x5A827999 : (t < 40) ?  0x6ED9EBA1 :
100		         (t < 60) ? 0x8F1BBCDC : 0xCA62C1D6;
101		}
102		public override function toString():String {
103			return "sha1";
104		}
105	}
106}