package { /** * Holds a number value safely to stop it being sniffed out and changed. Only use this for important * numbers as there is a cost to it. It's much slower than using a normal var (mainly because * of the getter/setter access). There's also a small margin for error because of the conversion * @author Damian Connolly - http://divillysausages.com */ public class SafeNumber { /*******************************************************************************************/ { // static constructor SafeNumber._init(); } /*******************************************************************************************/ private static var m_keys:Vector. = null; // the list of security keys to use private static var m_invKeys:Vector. = null; // the list of inv keys (quicker to multiply than divide) private static var m_currIndex:int = 0; // the current index for our number /*******************************************************************************************/ // inits the SafeNumber class, filling up our arrays // NOTE: We can't prepend the vars with "SafeNumber" as it comes out as null (not created yet?) private static function _init():void { var max:int = 100; m_keys = new Vector.( max, true ); m_invKeys = new Vector.( max, true ); // pre-generate random numbers so it's accessed quicker for ( var i:int = 0; i < max; i++ ) { var num:Number = -100.0 + Math.random() * 200.0; m_keys[i] = num; m_invKeys[i] = 1.0 / num; } } /*******************************************************************************************/ private var m_value:Number = 0.0; // our value private var m_index:int = 0; // the index of our key /*******************************************************************************************/ /** * The value for this Number. When being set, a new key is used to save it, and when being * retrieved, the inverse key is used to decrypt it */ public function get value():Number { return this.m_value * SafeNumber.m_invKeys[this.m_index]; } public function set value( n:Number ):void { this.m_index = SafeNumber.m_currIndex++; if ( SafeNumber.m_currIndex >= SafeNumber.m_keys.length ) SafeNumber.m_currIndex = 0; this.m_value = n * SafeNumber.m_keys[this.m_index]; } /*******************************************************************************************/ /** * Creates a new SafeNumber * @param n The initial value to set it at */ public function SafeNumber( n:Number = 0.0 ) { this.value = n; } } }