PageRenderTime 183ms CodeModel.GetById 80ms app.highlight 8ms RepoModel.GetById 93ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 101 lines | 84 code | 5 blank | 12 comment | 0 complexity | d4d2ade0e121dd437abd7bb15f71f192 MD5 | raw file
  1/**
  2 * OFBModeTest
  3 * 
  4 * A test class for OFBMode
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * See LICENSE.txt for full license information.
  8 */
  9package com.hurlant.crypto.tests
 10{
 11	import flash.utils.ByteArray;
 12	import com.hurlant.util.Hex;
 13	import com.hurlant.crypto.symmetric.OFBMode;
 14	import com.hurlant.crypto.symmetric.AESKey;
 15	import com.hurlant.crypto.symmetric.NullPad;
 16	
 17	public class OFBModeTest extends TestCase
 18	{
 19		public function OFBModeTest(h:ITestHarness)
 20		{
 21			super(h, "OFBMode Test");
 22			runTest(testOFB_AES128,"OFB AES-128 Test Vectors");
 23			runTest(testOFB_AES192,"OFB AES-192 Test Vectors");
 24			runTest(testOFB_AES256,"OFB AES-256 Test Vectors");
 25			h.endTestCase();
 26		}
 27		
 28		/**
 29		 * Nist Vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
 30		 * Section F.4.1 and below.
 31		 */
 32		public function testOFB_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				"7789508d16918f03f53c52dac54ed825" + 
 42				"9740051e9c5fecf64344f7a82260edcc" + 
 43				"304c6528f659c77866a510d9c1d6ae5e");
 44			var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad);
 45			ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 46			var src:ByteArray = new ByteArray;
 47			src.writeBytes(pt);
 48			ofb.encrypt(src);
 49			assert("OFB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 50			ofb.decrypt(src);
 51			assert("OFB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 52		}
 53		
 54		public function testOFB_AES192():void {
 55			var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
 56			var pt:ByteArray = Hex.toArray(
 57				"6bc1bee22e409f96e93d7e117393172a" + 
 58				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 59				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 60				"f69f2445df4f9b17ad2b417be66c3710");
 61			var ct:ByteArray = Hex.toArray(
 62				"cdc80d6fddf18cab34c25909c99a4174" + 
 63				"fcc28b8d4c63837c09e81700c1100401" + 
 64				"8d9a9aeac0f6596f559c6d4daf59a5f2" + 
 65				"6d9f200857ca6c3e9cac524bd9acc92a");
 66			var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad);
 67			ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 68			var src:ByteArray = new ByteArray;
 69			src.writeBytes(pt);
 70			ofb.encrypt(src);
 71			assert("OFB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 72			ofb.decrypt(src);
 73			assert("OFB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 74		}
 75		
 76		public function testOFB_AES256():void {
 77			var key:ByteArray = Hex.toArray(
 78				"603deb1015ca71be2b73aef0857d7781" + 
 79				"1f352c073b6108d72d9810a30914dff4");
 80			var pt:ByteArray = Hex.toArray(
 81				"6bc1bee22e409f96e93d7e117393172a" + 
 82				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 83				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 84				"f69f2445df4f9b17ad2b417be66c3710");
 85			var ct:ByteArray = Hex.toArray(
 86				"dc7e84bfda79164b7ecd8486985d3860" + 
 87				"4febdc6740d20b3ac88f6ad82a4fb08d" + 
 88				"71ab47a086e86eedf39d1c5bba97c408" + 
 89				"0126141d67f37be8538f5a8be740e484");
 90			var ofb:OFBMode = new OFBMode(new AESKey(key), new NullPad);
 91			ofb.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 92			var src:ByteArray = new ByteArray;
 93			src.writeBytes(pt);
 94			ofb.encrypt(src);
 95			assert("OFB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 96			ofb.decrypt(src);
 97			assert("OFB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 98		}
 99		
100	}
101}