/flash-src/third-party/com/hurlant/crypto/tests/DESKeyTest.as
ActionScript | 112 lines | 94 code | 3 blank | 15 comment | 1 complexity | 97b6888c17f8f5f3143ccefa73ee4d1b MD5 | raw file
1/** 2 * DesKeyTest 3 * 4 * A test class for DesKey 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.DESKey; 12 import com.hurlant.util.Hex; 13 import flash.utils.ByteArray; 14 15 public class DESKeyTest extends TestCase 16 { 17 public function DESKeyTest(h:ITestHarness) 18 { 19 super(h, "DESKey Test"); 20 runTest(testECB,"DES ECB Test Vectors"); 21 h.endTestCase(); 22 } 23 24 /** 25 * Test vectors mostly grabbed from 26 * http://csrc.nist.gov/publications/nistpubs/800-17/800-17.pdf 27 * (Appendix A and B) 28 * incomplete. 29 */ 30 public function testECB():void { 31 var keys:Array = [ 32 "3b3898371520f75e", // grabbed from the output of some js implementation out there 33 "10316E028C8F3B4A", // appendix A vector 34 "0101010101010101", // appendix B Table 1, round 0 35 "0101010101010101", // round 1 36 "0101010101010101", // 2 37 "0101010101010101", 38 "0101010101010101", 39 "0101010101010101", 40 "0101010101010101", 41 "0101010101010101", 42 "0101010101010101", // round 8 43 "8001010101010101", // app B, tbl 2, round 0 44 "4001010101010101", 45 "2001010101010101", 46 "1001010101010101", 47 "0801010101010101", 48 "0401010101010101", 49 "0201010101010101", 50 "0180010101010101", 51 "0140010101010101", // round 8 52 ]; 53 var pts:Array = [ 54 "0000000000000000", // js 55 "0000000000000000", // App A 56 "8000000000000000", // App B, tbl 1, rnd0 57 "4000000000000000", 58 "2000000000000000", 59 "1000000000000000", 60 "0800000000000000", // rnd 4 61 "0400000000000000", 62 "0200000000000000", 63 "0100000000000000", 64 "0080000000000000", // round 8 65 "0000000000000000", // App B, tbl2, rnd0 66 "0000000000000000", 67 "0000000000000000", 68 "0000000000000000", 69 "0000000000000000", 70 "0000000000000000", 71 "0000000000000000", 72 "0000000000000000", 73 "0000000000000000", // rnd 8 74 ]; 75 var cts:Array = [ 76 "83A1E814889253E0", // js 77 "82DCBAFBDEAB6602", // App A 78 "95F8A5E5DD31D900", // App b, tbl 1, rnd 0 79 "DD7F121CA5015619", 80 "2E8653104F3834EA", 81 "4BD388FF6CD81D4F", 82 "20B9E767B2FB1456", 83 "55579380D77138EF", 84 "6CC5DEFAAF04512F", 85 "0D9F279BA5D87260", 86 "D9031B0271BD5A0A", // rnd 8 87 "95A8D72813DAA94D", // App B, tbl 2, rnd 0 88 "0EEC1487DD8C26D5", 89 "7AD16FFB79C45926", 90 "D3746294CA6A6CF3", 91 "809F5F873C1FD761", 92 "C02FAFFEC989D1FC", 93 "4615AA1D33E72F10", 94 "2055123350C00858", 95 "DF3B99D6577397C8", // rnd 8 96 ]; 97 98 for (var i:uint=0;i<keys.length;i++) { 99 var key:ByteArray = Hex.toArray(keys[i]); 100 var pt:ByteArray = Hex.toArray(pts[i]); 101 var des:DESKey = new DESKey(key); 102 des.encrypt(pt); 103 var out:String = Hex.fromArray(pt).toUpperCase(); 104 assert("comparing "+cts[i]+" to "+out, cts[i]==out); 105 // now go back to plaintext 106 des.decrypt(pt); 107 out = Hex.fromArray(pt).toUpperCase(); 108 assert("comparing "+pts[i]+" to "+out, pts[i]==out); 109 } 110 } 111 } 112}