PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 33ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 148 lines | 133 code | 3 blank | 12 comment | 1 complexity | f1a80a6cd92cab6228f23252fca0a66c MD5 | raw file
  1/**
  2 * BlowFishKeyTest
  3 * 
  4 * A test class for BlowFishKey
  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.symmetric.BlowFishKey;
 12	import com.hurlant.util.Hex;
 13	import flash.utils.ByteArray;
 14	
 15	public class BlowFishKeyTest extends TestCase
 16	{
 17		public function BlowFishKeyTest(h:ITestHarness)
 18		{
 19			super(h, "BlowFishKey Test");
 20			runTest(testECB,"BlowFish ECB Test Vectors");
 21			h.endTestCase();
 22		}
 23		
 24		/**
 25		 * Test vectors from http://www.schneier.com/code/vectors.txt
 26		 */
 27		public function testECB():void {
 28			var keys:Array = [
 29			"0000000000000000",
 30			"FFFFFFFFFFFFFFFF",
 31			"3000000000000000",
 32			"1111111111111111",
 33			"0123456789ABCDEF",
 34			"1111111111111111",
 35			"0000000000000000",
 36			"FEDCBA9876543210",
 37			"7CA110454A1A6E57",
 38			"0131D9619DC1376E",
 39			"07A1133E4A0B2686",
 40			"3849674C2602319E",
 41			"04B915BA43FEB5B6",
 42			"0113B970FD34F2CE",
 43			"0170F175468FB5E6",
 44			"43297FAD38E373FE",
 45			"07A7137045DA2A16",
 46			"04689104C2FD3B2F",
 47			"37D06BB516CB7546",
 48			"1F08260D1AC2465E",
 49			"584023641ABA6176",
 50			"025816164629B007",
 51			"49793EBC79B3258F",
 52			"4FB05E1515AB73A7",
 53			"49E95D6D4CA229BF",
 54			"018310DC409B26D6",
 55			"1C587F1C13924FEF",
 56			"0101010101010101",
 57			"1F1F1F1F0E0E0E0E",
 58			"E0FEE0FEF1FEF1FE",
 59			"0000000000000000",
 60			"FFFFFFFFFFFFFFFF",
 61			"0123456789ABCDEF",
 62			"FEDCBA9876543210" ];
 63			var pts:Array = [
 64			"0000000000000000",
 65			"FFFFFFFFFFFFFFFF",
 66			"1000000000000001",
 67			"1111111111111111",
 68			"1111111111111111",
 69			"0123456789ABCDEF",
 70			"0000000000000000",
 71			"0123456789ABCDEF",
 72			"01A1D6D039776742",
 73			"5CD54CA83DEF57DA",
 74			"0248D43806F67172",
 75			"51454B582DDF440A",
 76			"42FD443059577FA2",
 77			"059B5E0851CF143A",
 78			"0756D8E0774761D2",
 79			"762514B829BF486A",
 80			"3BDD119049372802",
 81			"26955F6835AF609A",
 82			"164D5E404F275232",
 83			"6B056E18759F5CCA",
 84			"004BD6EF09176062",
 85			"480D39006EE762F2",
 86			"437540C8698F3CFA",
 87			"072D43A077075292",
 88			"02FE55778117F12A",
 89			"1D9D5C5018F728C2",
 90			"305532286D6F295A",
 91			"0123456789ABCDEF",
 92			"0123456789ABCDEF",
 93			"0123456789ABCDEF",
 94			"FFFFFFFFFFFFFFFF",
 95			"0000000000000000",
 96			"0000000000000000",
 97			"FFFFFFFFFFFFFFFF" ];
 98			var cts:Array = [
 99			"4EF997456198DD78",
100			"51866FD5B85ECB8A",
101			"7D856F9A613063F2",
102			"2466DD878B963C9D",
103			"61F9C3802281B096",
104			"7D0CC630AFDA1EC7",
105			"4EF997456198DD78",
106			"0ACEAB0FC6A0A28D",
107			"59C68245EB05282B",
108			"B1B8CC0B250F09A0",
109			"1730E5778BEA1DA4",
110			"A25E7856CF2651EB",
111			"353882B109CE8F1A",
112			"48F4D0884C379918",
113			"432193B78951FC98",
114			"13F04154D69D1AE5",
115			"2EEDDA93FFD39C79",
116			"D887E0393C2DA6E3",
117			"5F99D04F5B163969",
118			"4A057A3B24D3977B",
119			"452031C1E4FADA8E",
120			"7555AE39F59B87BD",
121			"53C55F9CB49FC019",
122			"7A8E7BFA937E89A3",
123			"CF9C5D7A4986ADB5",
124			"D1ABB290658BC778",
125			"55CB3774D13EF201",
126			"FA34EC4847B268B2",
127			"A790795108EA3CAE",
128			"C39E072D9FAC631D",
129			"014933E0CDAFF6E4",
130			"F21E9A77B71C49BC",
131			"245946885754369A",
132			"6B5C5A9C5D9E0A5A" ];
133
134			for (var i:uint=0;i<keys.length;i++) {
135				var key:ByteArray = Hex.toArray(keys[i]);
136				var pt:ByteArray = Hex.toArray(pts[i]);
137				var bf:BlowFishKey = new BlowFishKey(key);
138				bf.encrypt(pt);
139				var out:String = Hex.fromArray(pt).toUpperCase();
140				assert("comparing "+cts[i]+" to "+out, cts[i]==out);
141				// now go back to plaintext
142				bf.decrypt(pt);
143				out = Hex.fromArray(pt).toUpperCase();
144				assert("comparing "+pts[i]+" to "+out, pts[i]==out);
145			}
146		}
147	}
148}