PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 58 lines | 41 code | 4 blank | 13 comment | 1 complexity | 145e2d166e2afa0be914073b183d51b3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * ARC4Test
  3. *
  4. * A test class for ARC4
  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.util.Hex;
  12. import flash.utils.ByteArray;
  13. import com.hurlant.crypto.prng.ARC4;
  14. public class ARC4Test extends TestCase
  15. {
  16. public function ARC4Test(h:ITestHarness)
  17. {
  18. super(h, "ARC4 Test");
  19. runTest(testLameVectors,"ARC4 Test Vectors");
  20. h.endTestCase();
  21. }
  22. /**
  23. * Sad test vectors pilfered from
  24. * http://en.wikipedia.org/wiki/RC4
  25. */
  26. public function testLameVectors():void {
  27. var keys:Array = [
  28. Hex.fromString("Key"),
  29. Hex.fromString("Wiki"),
  30. Hex.fromString("Secret")];
  31. var pts:Array = [
  32. Hex.fromString("Plaintext"),
  33. Hex.fromString("pedia"),
  34. Hex.fromString("Attack at dawn")];
  35. var cts:Array = [
  36. "BBF316E8D940AF0AD3",
  37. "1021BF0420",
  38. "45A01F645FC35B383552544B9BF5"];
  39. for (var i:uint=0;i<keys.length;i++) {
  40. var key:ByteArray = Hex.toArray(keys[i]);
  41. var pt:ByteArray = Hex.toArray(pts[i]);
  42. var rc4:ARC4 = new ARC4(key);
  43. rc4.encrypt(pt);
  44. var out:String = Hex.fromArray(pt).toUpperCase();
  45. assert("comparing "+cts[i]+" to "+out, cts[i]==out);
  46. // now go back to plaintext
  47. rc4.init(key);
  48. rc4.decrypt(pt);
  49. out = Hex.fromArray(pt);
  50. assert("comparing "+pts[i]+" to "+out, pts[i]==out);
  51. }
  52. }
  53. }
  54. }