PageRenderTime 455ms CodeModel.GetById 141ms app.highlight 185ms RepoModel.GetById 118ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 184 lines | 158 code | 7 blank | 19 comment | 11 complexity | dcfbb36644a93d0d1761fe12ad4ccf83 MD5 | raw file
  1/**
  2 * HMACTest
  3 * 
  4 * A test class for HMAC
  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.hash.HMAC;
 12	import com.hurlant.crypto.hash.MD5;
 13	import com.hurlant.crypto.hash.SHA1;
 14	import com.hurlant.crypto.hash.SHA224;
 15	import com.hurlant.crypto.hash.SHA256;
 16	import com.hurlant.util.Hex;
 17	
 18	import flash.utils.ByteArray;
 19	
 20	public class HMACTest extends TestCase
 21	{
 22		public function HMACTest(h:ITestHarness)
 23		{
 24			super(h, "HMAC Test");
 25			runTest(testHMAC_MD5,"HMAC MD5 Test Vectors");
 26			runTest(testHMAC_SHA_1,"HMAC SHA-1 Test Vectors");
 27			runTest(testHMAC_SHA_2,"HMAC SHA-224/SHA-256 Test Vectors");
 28			runTest(testHMAC96_MD5,"HMAC-96 MD5 Test Vectors");
 29			runTest(testHMAC96_SHA_1,"HMAC-96 SHA-1 Test Vectors");
 30			runTest(testHMAC128_SHA_2,"HMAC-128 SHA-224/SHA-256 Test Vectors");
 31			h.endTestCase();
 32		}
 33		/**
 34		 * Test vectors taking from RFC2202
 35		 * http://tools.ietf.org/html/rfc2202
 36		 * Yes, it's from an RFC, jefe! Now waddayawant?
 37		 */
 38		public function testHMAC_SHA_1():void {
 39			var keys:Array = [
 40			"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
 41			Hex.fromString("Jefe"),
 42			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
 43			"0102030405060708090a0b0c0d0e0f10111213141516171819",
 44			"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
 45			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
 46			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];
 47			var pts:Array = [
 48			Hex.fromString("Hi There"),
 49			Hex.fromString("what do ya want for nothing?"),
 50			"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
 51			"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
 52			Hex.fromString("Test With Truncation"),
 53			Hex.fromString("Test Using Larger Than Block-Size Key - Hash Key First"),
 54			Hex.fromString("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data")];
 55			var cts:Array = [
 56			"b617318655057264e28bc0b6fb378c8ef146be00",
 57			"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
 58			"125d7342b9ac11cd91a39af48aa17b4f63f175d3", 
 59			"4c9007f4026250c6bc8414f9bf50c86c2d7235da", 
 60			"4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
 61			"aa4ae5e15272d00e95705637ce8a3b55ed402112",
 62			"e8e99d0f45237d786d6bbaa7965c7808bbff1a91"];
 63			
 64			var hmac:HMAC = new HMAC(new SHA1());
 65			for (var i:uint=0;i<keys.length;i++) {
 66				var key:ByteArray = Hex.toArray(keys[i]);
 67				var pt:ByteArray = Hex.toArray(pts[i]);
 68				var digest:ByteArray = hmac.compute(key, pt);
 69				assert("HMAC-SHA-1 test "+i, Hex.fromArray(digest) == cts[i]);
 70			}
 71		}
 72		public function testHMAC96_SHA_1():void {
 73			var hmac:HMAC = new HMAC(new SHA1, 96);
 74			var key:ByteArray = Hex.toArray("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
 75			var pt:ByteArray = Hex.toArray(Hex.fromString("Test With Truncation"));
 76			var ct:String = "4c1a03424b55e07fe7f27be1";
 77			var digest:ByteArray = hmac.compute(key, pt);
 78			assert("HMAC96-SHA-1 test", Hex.fromArray(digest) == ct);
 79		}
 80		
 81		public function testHMAC_MD5():void {
 82			var keys:Array = [
 83			Hex.fromString("Jefe"),
 84			"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
 85			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
 86			"0102030405060708090a0b0c0d0e0f10111213141516171819",
 87			"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
 88			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
 89			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];
 90			var pts:Array = [
 91			Hex.fromString("what do ya want for nothing?"),
 92			Hex.fromString("Hi There"),
 93			"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
 94			"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
 95			Hex.fromString("Test With Truncation"),
 96			Hex.fromString("Test Using Larger Than Block-Size Key - Hash Key First"),
 97			Hex.fromString("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data")];
 98			var cts:Array = [
 99			"750c783e6ab0b503eaa86e310a5db738",
100			"9294727a3638bb1c13f48ef8158bfc9d",
101			"56be34521d144c88dbb8c733f0e8b3f6", 
102			"697eaf0aca3a3aea3a75164746ffaa79",
103			"56461ef2342edc00f9bab995690efd4c",
104			"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
105			"6f630fad67cda0ee1fb1f562db3aa53e"];
106
107			var hmac:HMAC = new HMAC(new MD5());
108			for (var i:uint=0;i<keys.length;i++) {
109				var key:ByteArray = Hex.toArray(keys[i]);
110				var pt:ByteArray = Hex.toArray(pts[i]);
111				var digest:ByteArray = hmac.compute(key, pt);
112				assert("HMAC-MD5 test "+i, Hex.fromArray(digest) == cts[i]);
113			}
114		}
115		public function testHMAC96_MD5():void {
116			var hmac:HMAC = new HMAC(new MD5, 96);
117			var key:ByteArray = Hex.toArray("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
118			var pt:ByteArray = Hex.toArray(Hex.fromString("Test With Truncation"));
119			var ct:String = "56461ef2342edc00f9bab995";
120			var digest:ByteArray = hmac.compute(key, pt);
121			assert("HMAC96-MD5 test", Hex.fromArray(digest) == ct);
122		}
123		
124		/**
125		 * Test vectors for HMAC-SHA-2 taken from RFC4231
126		 * http://www.ietf.org/rfc/rfc4231.txt
127		 * Still the same lame strings, but hidden in hex. why not.
128		 */
129		public function testHMAC_SHA_2():void {
130			var keys:Array = [
131			"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
132			"4a656665",
133			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
134			"0102030405060708090a0b0c0d0e0f10111213141516171819",
135			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
136			"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];
137			var pts:Array = [
138			"4869205468657265",
139			"7768617420646f2079612077616e7420666f72206e6f7468696e673f",
140			"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
141			"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
142			"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374",
143			"5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e"];
144			var cts224:Array = [
145			"896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
146			"a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44",
147			"7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea",
148			"6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a",
149			"95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e",
150			"3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1"];
151			var cts256:Array = [
152			"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
153			"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
154			"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
155			"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
156			"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
157			"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"];
158			// 384 and 512 will be added. someday. if I ever figure how to do 64bit computations half efficiently in as3
159
160			var hmac224:HMAC = new HMAC(new SHA224);
161			var hmac256:HMAC = new HMAC(new SHA256);
162			for (var i:uint=0;i<keys.length;i++) {
163				var key:ByteArray = Hex.toArray(keys[i]);
164				var pt:ByteArray = Hex.toArray(pts[i]);
165				var digest224:ByteArray = hmac224.compute(key, pt);
166				assert("HMAC-SHA-224 test "+i, Hex.fromArray(digest224) == cts224[i]);
167				var digest256:ByteArray = hmac256.compute(key, pt);
168				assert("HMAC-SHA-256 test "+i, Hex.fromArray(digest256) == cts256[i]);
169			}
170		}
171		public function testHMAC128_SHA_2():void {
172			var hmac224:HMAC = new HMAC(new SHA224,128);
173			var hmac256:HMAC = new HMAC(new SHA256,128);
174			var key:ByteArray = Hex.toArray("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
175			var pt:ByteArray = Hex.toArray("546573742057697468205472756e636174696f6e");
176			var ct224:String = "0e2aea68a90c8d37c988bcdb9fca6fa8";
177			var ct256:String = "a3b6167473100ee06e0c796c2955552b";
178			var digest224:ByteArray = hmac224.compute(key, pt);
179			assert("HMAC128-SHA-224 test", Hex.fromArray(digest224) == ct224);
180			var digest256:ByteArray = hmac256.compute(key, pt);
181			assert("HMAC128-SHA-256 test", Hex.fromArray(digest256) == ct256);
182		}
183	}
184}