PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 32ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 98 lines | 84 code | 3 blank | 11 comment | 0 complexity | 11159297669d126cbf08c58fc31c451a MD5 | raw file
 1/**
 2 * CFBModeTest
 3 * 
 4 * A test class for CFBMode
 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.AESKey;
12	import com.hurlant.crypto.symmetric.CFBMode;
13	import com.hurlant.crypto.symmetric.NullPad;
14	import com.hurlant.util.Hex;
15	
16	import flash.utils.ByteArray;
17
18	public class CFBModeTest extends TestCase
19	{
20		public function CFBModeTest(h:ITestHarness)
21		{
22			super(h, "CFBMode Test");
23			runTest(testCFB_AES128, "CFB AES-128 Test Vectors");
24			runTest(testCFB_AES192, "CFB AES-192 Test Vectors");
25			runTest(testCFB_AES256, "CFB AES-256 Test Vectors");
26			h.endTestCase();
27		}
28		
29		/**
30		 * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
31		 */
32		public function testCFB_AES128():void {
33			var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c");
34			var pt:ByteArray = Hex.toArray(
35				"6bc1bee22e409f96e93d7e117393172a" + 
36				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
37				"30c81c46a35ce411e5fbc1191a0a52ef" + 
38				"f69f2445df4f9b17ad2b417be66c3710");
39			var ct:ByteArray = Hex.toArray(
40				"3b3fd92eb72dad20333449f8e83cfb4a" + 
41				"c8a64537a0b3a93fcde3cdad9f1ce58b" + 
42				"26751f67a3cbb140b1808cf187a4f4df" + 
43				"c04b05357c5d1c0eeac4c66f9ff7f2e6");
44			var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad);
45			cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
46			var src:ByteArray = new ByteArray;
47			src.writeBytes(pt);
48			cfb.encrypt(src);
49			assert("CFB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
50			cfb.decrypt(src);
51			assert("CFB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
52		}
53		public function testCFB_AES192():void {
54			var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
55			var pt:ByteArray = Hex.toArray(
56				"6bc1bee22e409f96e93d7e117393172a" + 
57				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
58				"30c81c46a35ce411e5fbc1191a0a52ef" + 
59				"f69f2445df4f9b17ad2b417be66c3710");
60			var ct:ByteArray = Hex.toArray(
61				"cdc80d6fddf18cab34c25909c99a4174" + 
62				"67ce7f7f81173621961a2b70171d3d7a" + 
63				"2e1e8a1dd59b88b1c8e60fed1efac4c9" + 
64				"c05f9f9ca9834fa042ae8fba584b09ff");
65			var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad);
66			cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
67			var src:ByteArray = new ByteArray;
68			src.writeBytes(pt);
69			cfb.encrypt(src);
70			assert("CFB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
71			cfb.decrypt(src);
72			assert("CFB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
73		}
74		public function testCFB_AES256():void {
75			var key:ByteArray = Hex.toArray(
76				"603deb1015ca71be2b73aef0857d7781" + 
77				"1f352c073b6108d72d9810a30914dff4");
78			var pt:ByteArray = Hex.toArray(
79				"6bc1bee22e409f96e93d7e117393172a" + 
80				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
81				"30c81c46a35ce411e5fbc1191a0a52ef" + 
82				"f69f2445df4f9b17ad2b417be66c3710");
83			var ct:ByteArray = Hex.toArray(
84				"dc7e84bfda79164b7ecd8486985d3860" + 
85				"39ffed143b28b1c832113c6331e5407b" + 
86				"df10132415e54b92a13ed0a8267ae2f9" + 
87				"75a385741ab9cef82031623d55b1e471");
88			var cfb:CFBMode = new CFBMode(new AESKey(key), new NullPad);
89			cfb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
90			var src:ByteArray = new ByteArray;
91			src.writeBytes(pt);
92			cfb.encrypt(src);
93			assert("CFB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
94			cfb.decrypt(src);
95			assert("CFB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
96		}
97	}
98}