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

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