PageRenderTime 38ms CodeModel.GetById 27ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/flash-src/third-party/com/hurlant/crypto/tests/RSAKeyTest.as

http://github.com/gimite/web-socket-js
ActionScript | 92 lines | 73 code | 6 blank | 13 comment | 0 complexity | 488777744fa411d9dc90cb833290bfbd MD5 | raw file
 1/**
 2 * RSAKeyTest
 3 * 
 4 * A test class for RSAKey
 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.rsa.RSAKey;
12	import flash.utils.ByteArray;
13	import com.hurlant.util.Hex;
14	import com.hurlant.util.der.PEM;
15	
16	public class RSAKeyTest extends TestCase
17	{
18		public function RSAKeyTest(h:ITestHarness)
19		{
20			super(h, "RSA Testing");
21			
22			runTest(testSmoke,"RSA smoke test");
23			runTest(testGenerate, "RSA Key Generation test");
24			runTest(testPEM, "RSA Private Key PEM parsing");
25			runTest(testPEM2, "RSA Public Key PEM parsing");
26			h.endTestCase();
27		}
28		
29		public function testSmoke():void {
30			var N:String ="C4E3F7212602E1E396C0B6623CF11D26204ACE3E7D26685E037AD2507DCE82FC" + 
31					"28F2D5F8A67FC3AFAB89A6D818D1F4C28CFA548418BD9F8E7426789A67E73E41";
32			var E:String = "10001";
33			var D:String = "7cd1745aec69096129b1f42da52ac9eae0afebbe0bc2ec89253598dcf454960e" + 
34					"3e5e4ec9f8c87202b986601dd167253ee3fb3fa047e14f1dfd5ccd37e931b29d";
35			var P:String = "f0e4dd1eac5622bd3932860fc749bbc48662edabdf3d2826059acc0251ac0d3b";
36			var Q:String = "d13cb38fbcd06ee9bca330b4000b3dae5dae12b27e5173e4d888c325cda61ab3";
37			var DMP1:String = "b3d5571197fc31b0eb6b4153b425e24c033b054d22b9c8282254fe69d8c8c593";
38			var DMQ1:String = "968ffe89e50d7b72585a79b65cfdb9c1da0963cceb56c3759e57334de5a0ac3f";
39			var IQMP:String = "d9bc4f420e93adad9f007d0e5744c2fe051c9ed9d3c9b65f439a18e13d6e3908";
40			// create a key.
41			var rsa:RSAKey = RSAKey.parsePrivateKey(N,E,D, P,Q,DMP1,DMQ1,IQMP);
42			var txt:String = "hello";
43			var src:ByteArray = Hex.toArray(Hex.fromString(txt));
44			var dst:ByteArray = new ByteArray;
45			var dst2:ByteArray = new ByteArray;
46			rsa.encrypt(src, dst, src.length);
47			rsa.decrypt(dst, dst2, dst.length);
48			var txt2:String = Hex.toString(Hex.fromArray(dst2));
49			assert("rsa encrypt+decrypt", txt==txt2);
50		}
51		
52		public function testGenerate():void {
53			var rsa:RSAKey = RSAKey.generate(256, "10001");
54			// same lame smoke test here.
55			var txt:String = "hello";
56			var src:ByteArray = Hex.toArray(Hex.fromString(txt));
57			var dst:ByteArray = new ByteArray;
58			var dst2:ByteArray = new ByteArray;
59			rsa.encrypt(src, dst, src.length);
60			rsa.decrypt(dst, dst2, dst.length);
61			var txt2:String = Hex.toString(Hex.fromArray(dst2));
62			assert("rsa encrypt+decrypt", txt==txt2);
63		}
64		
65		public function testPEM():void {
66			var pem:String = "-----BEGIN RSA PRIVATE KEY-----\n" + 
67					"MGQCAQACEQDJG3bkuB9Ie7jOldQTVdzPAgMBAAECEQCOGqcPhP8t8mX8cb4cQEaR\n" + 
68					"AgkA5WTYuAGmH0cCCQDgbrto0i7qOQIINYr5btGrtccCCQCYy4qX4JDEMQIJAJll\n" + 
69					"OnLVtCWk\n" + 
70					"-----END RSA PRIVATE KEY-----";
71			var rsa:RSAKey = PEM.readRSAPrivateKey(pem);
72			//trace(rsa.dump());
73
74			// obligatory use
75			var txt:String = "hello";
76			var src:ByteArray = Hex.toArray(Hex.fromString(txt));
77			var dst:ByteArray = new ByteArray;
78			var dst2:ByteArray = new ByteArray;
79			rsa.encrypt(src, dst, src.length);
80			rsa.decrypt(dst, dst2, dst.length);
81			var txt2:String = Hex.toString(Hex.fromArray(dst2));
82			assert("rsa encrypt+decrypt", txt==txt2);
83		}
84		public function testPEM2():void {
85			var pem:String = "-----BEGIN PUBLIC KEY-----\n" + 
86					"MCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAMkbduS4H0h7uM6V1BNV3M8CAwEAAQ==\n" + 
87					"-----END PUBLIC KEY-----";
88			var rsa:RSAKey = PEM.readRSAPublicKey(pem);
89			//trace(rsa.dump());
90		}
91	}
92}