PageRenderTime 132ms CodeModel.GetById 50ms app.highlight 5ms RepoModel.GetById 36ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 66 lines | 45 code | 7 blank | 14 comment | 1 complexity | f2b74d2fe48379a963ff5732c64835d5 MD5 | raw file
 1/**
 2 * XTeaKeyTest
 3 * 
 4 * A test class for XTeaKey
 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.crypto.prng.Random;
12	import com.hurlant.crypto.symmetric.ECBMode;
13	import com.hurlant.crypto.symmetric.XTeaKey;
14	import com.hurlant.util.Hex;
15	
16	import flash.utils.ByteArray;
17	import flash.utils.getTimer;
18	
19	public class XTeaKeyTest extends TestCase
20	{
21		public function XTeaKeyTest(h:ITestHarness) {
22			super(h, "XTeaKey Test");
23			runTest(testGetBlockSize, "XTea Block Size");
24			runTest(testVectors, "XTea Test Vectors");
25			
26			h.endTestCase();
27		}
28		
29		public function testGetBlockSize():void {
30			var tea:XTeaKey = new XTeaKey(Hex.toArray("deadbabecafebeefdeadbabecafebeef"));
31			assert("tea blocksize", tea.getBlockSize()==8);
32		}
33		
34		public function testVectors():void {
35			// blah.
36			// can't find working test vectors.
37			// algorithms should not get published without vectors :(
38			var keys:Array=[
39			"00000000000000000000000000000000",
40			"2b02056806144976775d0e266c287843"];
41			var pts:Array=[
42			"0000000000000000",
43			"74657374206d652e"];
44			var cts:Array=[
45			"2dc7e8d3695b0538",
46			"7909582138198783"];
47			// self-fullfilling vectors.
48			// oh well, at least I can decrypt what I produce. :(
49			
50			for (var i:uint=0;i<keys.length;i++) {
51				var key:ByteArray = Hex.toArray(keys[i]);
52				var pt:ByteArray = Hex.toArray(pts[i]);
53				var tea:XTeaKey = new XTeaKey(key);
54				tea.encrypt(pt);
55				var out:String = Hex.fromArray(pt);
56				assert("comparing "+cts[i]+" to "+out, cts[i]==out);
57				// now go back to plaintext.
58				pt.position=0;
59				tea.decrypt(pt);
60				out = Hex.fromArray(pt);
61				assert("comparing "+pts[i]+" to "+out, pts[i]==out);
62			}
63		}
64
65	}
66}