PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 90 lines | 75 code | 4 blank | 11 comment | 6 complexity | 57358af97724932fc04d0f571e1bcce7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * ARC4
  3. *
  4. * An ActionScript 3 implementation of RC4
  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 com.hurlant.crypto.symmetric.IStreamCipher;
  15. import com.hurlant.util.Memory;
  16. import flash.utils.ByteArray;
  17. public class ARC4 implements IPRNG, IStreamCipher {
  18. private var i:int = 0;
  19. private var j:int = 0;
  20. private var S:ByteArray;
  21. private const psize:uint = 256;
  22. public function ARC4(key:ByteArray = null){
  23. S = new ByteArray;
  24. if (key) {
  25. init(key);
  26. }
  27. }
  28. public function getPoolSize():uint {
  29. return psize;
  30. }
  31. public function init(key:ByteArray):void {
  32. var i:int;
  33. var j:int;
  34. var t:int;
  35. for (i=0; i<256; ++i) {
  36. S[i] = i;
  37. }
  38. j=0;
  39. for (i=0; i<256; ++i) {
  40. j = (j + S[i] + key[i%key.length]) & 255;
  41. t = S[i];
  42. S[i] = S[j];
  43. S[j] = t;
  44. }
  45. this.i=0;
  46. this.j=0;
  47. }
  48. public function next():uint {
  49. var t:int;
  50. i = (i+1)&255;
  51. j = (j+S[i])&255;
  52. t = S[i];
  53. S[i] = S[j];
  54. S[j] = t;
  55. return S[(t+S[i])&255];
  56. }
  57. public function getBlockSize():uint {
  58. return 1;
  59. }
  60. public function encrypt(block:ByteArray):void {
  61. var i:uint = 0;
  62. while (i<block.length) {
  63. block[i++] ^= next();
  64. }
  65. }
  66. public function decrypt(block:ByteArray):void {
  67. encrypt(block); // the beauty of XOR.
  68. }
  69. public function dispose():void {
  70. var i:uint = 0;
  71. if (S!=null) {
  72. for (i=0;i<S.length;i++) {
  73. S[i] = Math.random()*256;
  74. }
  75. S.length=0;
  76. S = null;
  77. }
  78. this.i = 0;
  79. this.j = 0;
  80. Memory.gc();
  81. }
  82. public function toString():String {
  83. return "rc4";
  84. }
  85. }
  86. }