PageRenderTime 35ms CodeModel.GetById 28ms app.highlight 5ms 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/tests/ARC4Test.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 58 lines | 41 code | 4 blank | 13 comment | 1 complexity | 145e2d166e2afa0be914073b183d51b3 MD5 | raw file
 1/**
 2 * ARC4Test
 3 * 
 4 * A test class for ARC4
 5 * Copyright (c) 2007 Henri Torgemane
 6 * 
 7 * See LICENSE.txt for full license information.
 8 */
 9package com.hurlant.crypto.tests
10{
11	import com.hurlant.util.Hex;
12	import flash.utils.ByteArray;
13	import com.hurlant.crypto.prng.ARC4;
14	
15	public class ARC4Test extends TestCase
16	{
17		public function ARC4Test(h:ITestHarness)
18		{
19			super(h, "ARC4 Test");
20			runTest(testLameVectors,"ARC4 Test Vectors");
21			h.endTestCase();
22		}
23		
24		/**
25		 * Sad test vectors pilfered from
26		 * http://en.wikipedia.org/wiki/RC4
27		 */
28		public function testLameVectors():void {
29			var keys:Array = [
30			Hex.fromString("Key"),
31			Hex.fromString("Wiki"),
32			Hex.fromString("Secret")];
33			var pts:Array = [
34			Hex.fromString("Plaintext"),
35			Hex.fromString("pedia"),
36			Hex.fromString("Attack at dawn")];
37			var cts:Array = [
38			"BBF316E8D940AF0AD3",
39			"1021BF0420",
40			"45A01F645FC35B383552544B9BF5"];
41			
42			for (var i:uint=0;i<keys.length;i++) {
43				var key:ByteArray = Hex.toArray(keys[i]);
44				var pt:ByteArray = Hex.toArray(pts[i]);
45				var rc4:ARC4 = new ARC4(key);
46				rc4.encrypt(pt);
47				var out:String = Hex.fromArray(pt).toUpperCase();
48				assert("comparing "+cts[i]+" to "+out, cts[i]==out);
49				// now go back to plaintext
50				rc4.init(key);
51				rc4.decrypt(pt);
52				out = Hex.fromArray(pt);
53				assert("comparing "+pts[i]+" to "+out, pts[i]==out);
54			}
55		}
56		
57	}
58}