PageRenderTime 34ms CodeModel.GetById 14ms RepoModel.GetById 1ms 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
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. */
  12. package 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. public class Random
  24. {
  25. private var state:IPRNG;
  26. private var ready:Boolean = false;
  27. private var pool:ByteArray;
  28. private var psize:int;
  29. private var pptr:int;
  30. private var seeded:Boolean = false;
  31. public function Random(prng:Class = null) {
  32. if (prng==null) prng = ARC4;
  33. state = new prng as IPRNG;
  34. psize= state.getPoolSize();
  35. pool = new ByteArray;
  36. pptr = 0;
  37. while (pptr <psize) {
  38. var t:uint = 65536*Math.random();
  39. pool[pptr++] = t >>> 8;
  40. pool[pptr++] = t&255;
  41. }
  42. pptr=0;
  43. seed();
  44. }
  45. public function seed(x:int = 0):void {
  46. if (x==0) {
  47. x = new Date().getTime();
  48. }
  49. pool[pptr++] ^= x & 255;
  50. pool[pptr++] ^= (x>>8)&255;
  51. pool[pptr++] ^= (x>>16)&255;
  52. pool[pptr++] ^= (x>>24)&255;
  53. pptr %= psize;
  54. seeded = true;
  55. }
  56. /**
  57. * Gather anything we have that isn't entirely predictable:
  58. * - memory used
  59. * - system capabilities
  60. * - timing stuff
  61. * - installed fonts
  62. */
  63. public function autoSeed():void {
  64. var b:ByteArray = new ByteArray;
  65. b.writeUnsignedInt(System.totalMemory);
  66. b.writeUTF(Capabilities.serverString);
  67. b.writeUnsignedInt(getTimer());
  68. b.writeUnsignedInt((new Date).getTime());
  69. var a:Array = Font.enumerateFonts(true);
  70. for each (var f:Font in a) {
  71. b.writeUTF(f.fontName);
  72. b.writeUTF(f.fontStyle);
  73. b.writeUTF(f.fontType);
  74. }
  75. b.position=0;
  76. while (b.bytesAvailable>=4) {
  77. seed(b.readUnsignedInt());
  78. }
  79. }
  80. public function nextBytes(buffer:ByteArray, length:int):void {
  81. while (length--) {
  82. buffer.writeByte(nextByte());
  83. }
  84. }
  85. public function nextByte():int {
  86. if (!ready) {
  87. if (!seeded) {
  88. autoSeed();
  89. }
  90. state.init(pool);
  91. pool.length = 0;
  92. pptr = 0;
  93. ready = true;
  94. }
  95. return state.next();
  96. }
  97. public function dispose():void {
  98. for (var i:uint=0;i<pool.length;i++) {
  99. pool[i] = Math.random()*256;
  100. }
  101. pool.length=0;
  102. pool = null;
  103. state.dispose();
  104. state = null;
  105. psize = 0;
  106. pptr = 0;
  107. Memory.gc();
  108. }
  109. public function toString():String {
  110. return "random-"+state.toString();
  111. }
  112. }
  113. }