PageRenderTime 56ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 32ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 119 lines | 95 code | 6 blank | 18 comment | 9 complexity | 406491253d545c336129cbedc93b6b27 MD5 | raw file
  1/**
  2 * Random
  3 * 
  4 * An ActionScript 3 implementation of a Random Number Generator
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * Derived from:
  8 * 		The jsbn library, Copyright (c) 2003-2005 Tom Wu
  9 * 
 10 * See LICENSE.txt for full license information.
 11 */
 12package com.hurlant.crypto.prng
 13{
 14	import flash.utils.ByteArray;
 15	import com.hurlant.util.Memory;
 16	import flash.system.System;
 17	import flash.system.Capabilities;
 18	import flash.accessibility.AccessibilityProperties;
 19	import flash.display.SWFVersion;
 20	import flash.display.Stage;
 21	import flash.utils.getTimer;
 22	import flash.text.Font;
 23	
 24	public class Random
 25	{
 26		private var state:IPRNG;
 27		private var ready:Boolean = false;
 28		private var pool:ByteArray;
 29		private var psize:int;
 30		private var pptr:int;
 31		private var seeded:Boolean = false;
 32		
 33		public function Random(prng:Class = null) {
 34			if (prng==null) prng = ARC4;
 35			state = new prng as IPRNG;
 36			psize= state.getPoolSize();
 37			pool = new ByteArray;
 38			pptr = 0;
 39			while (pptr <psize) {
 40				var t:uint = 65536*Math.random();
 41				pool[pptr++] = t >>> 8;
 42				pool[pptr++] = t&255;
 43			}
 44			pptr=0;
 45			seed();
 46		}
 47		
 48		public function seed(x:int = 0):void {
 49			if (x==0) {
 50				x = new Date().getTime();
 51			}
 52			pool[pptr++] ^= x & 255;
 53			pool[pptr++] ^= (x>>8)&255;
 54			pool[pptr++] ^= (x>>16)&255;
 55			pool[pptr++] ^= (x>>24)&255;
 56			pptr %= psize;
 57			seeded = true;
 58		}
 59		
 60		/**
 61		 * Gather anything we have that isn't entirely predictable:
 62		 *  - memory used
 63		 *  - system capabilities
 64		 *  - timing stuff
 65		 *  - installed fonts
 66		 */
 67		public function autoSeed():void {
 68			var b:ByteArray = new ByteArray;
 69			b.writeUnsignedInt(System.totalMemory);
 70			b.writeUTF(Capabilities.serverString);
 71			b.writeUnsignedInt(getTimer());
 72			b.writeUnsignedInt((new Date).getTime());
 73			var a:Array = Font.enumerateFonts(true);
 74			for each (var f:Font in a) {
 75				b.writeUTF(f.fontName);
 76				b.writeUTF(f.fontStyle);
 77				b.writeUTF(f.fontType);
 78			}
 79			b.position=0;
 80			while (b.bytesAvailable>=4) {
 81				seed(b.readUnsignedInt());
 82			}
 83		}
 84		
 85		
 86		public function nextBytes(buffer:ByteArray, length:int):void {
 87			while (length--) {
 88				buffer.writeByte(nextByte());
 89			}
 90		}
 91		public function nextByte():int {
 92			if (!ready) {
 93				if (!seeded) {
 94					autoSeed();
 95				}
 96				state.init(pool);
 97				pool.length = 0;
 98				pptr = 0;
 99				ready = true;
100			}
101			return state.next();
102		}
103		public function dispose():void {
104			for (var i:uint=0;i<pool.length;i++) {
105				pool[i] = Math.random()*256;
106			}
107			pool.length=0;
108			pool = null;
109			state.dispose();
110			state = null;
111			psize = 0;
112			pptr = 0;
113			Memory.gc();
114		}
115		public function toString():String {
116			return "random-"+state.toString();
117		}
118	}
119}