PageRenderTime 374ms CodeModel.GetById 191ms app.highlight 7ms RepoModel.GetById 168ms 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
 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 */
12package com.hurlant.crypto.prng
13{
14	import com.hurlant.crypto.symmetric.IStreamCipher;
15	import com.hurlant.util.Memory;
16	
17	import flash.utils.ByteArray;
18
19	public class ARC4 implements IPRNG, IStreamCipher {
20		private var i:int = 0;
21		private var j:int = 0;
22		private var S:ByteArray;
23		private const psize:uint = 256;
24		public function ARC4(key:ByteArray = null){
25			S = new ByteArray;
26			if (key) {
27				init(key);
28			}
29		}
30		public function getPoolSize():uint {
31			return psize;
32		}
33		public function init(key:ByteArray):void {
34			var i:int;
35			var j:int;
36			var t:int;
37			for (i=0; i<256; ++i) {
38				S[i] = i;
39			}
40			j=0;
41			for (i=0; i<256; ++i) {
42				j = (j + S[i] + key[i%key.length]) & 255;
43				t = S[i];
44				S[i] = S[j];
45				S[j] = t;
46			}
47			this.i=0;
48			this.j=0;
49		}
50		public function next():uint {
51			var t:int;
52			i = (i+1)&255;
53			j = (j+S[i])&255;
54			t = S[i];
55			S[i] = S[j];
56			S[j] = t;
57			return S[(t+S[i])&255];
58		}
59
60		public function getBlockSize():uint {
61			return 1;
62		}
63		
64		public function encrypt(block:ByteArray):void {
65			var i:uint = 0;
66			while (i<block.length) {
67				block[i++] ^= next();
68			}
69		}
70		public function decrypt(block:ByteArray):void {
71			encrypt(block); // the beauty of XOR.
72		}
73		public function dispose():void {
74			var i:uint = 0;
75			if (S!=null) {
76				for (i=0;i<S.length;i++) {
77					S[i] = Math.random()*256;
78				}
79				S.length=0;
80				S = null;
81			}
82			this.i = 0;
83			this.j = 0;
84			Memory.gc();
85		}
86		public function toString():String {
87			return "rc4";
88		}
89	}
90}