PageRenderTime 386ms CodeModel.GetById 368ms 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/symmetric/XTeaKey.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 94 lines | 71 code | 11 blank | 12 comment | 3 complexity | c5442dd20e405b5d3272eb04e1ab9565 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * XTeaKey
  3. *
  4. * An ActionScript 3 implementation of the XTea algorithm
  5. * Copyright (c) 2007 Henri Torgemane
  6. *
  7. * See LICENSE.txt for full license information.
  8. */
  9. package com.hurlant.crypto.symmetric
  10. {
  11. import com.hurlant.crypto.prng.Random;
  12. import com.hurlant.util.Memory;
  13. import flash.utils.ByteArray;
  14. public class XTeaKey implements ISymmetricKey
  15. {
  16. public const NUM_ROUNDS:uint = 64;
  17. private var k:Array;
  18. public function XTeaKey(a:ByteArray) {
  19. a.position=0;
  20. k = [a.readUnsignedInt(),a.readUnsignedInt(),a.readUnsignedInt(),a.readUnsignedInt()];
  21. }
  22. /**
  23. * K is an hex string with 32 digits.
  24. */
  25. public static function parseKey(K:String):XTeaKey {
  26. var a:ByteArray = new ByteArray;
  27. a.writeUnsignedInt(parseInt(K.substr(0,8),16));
  28. a.writeUnsignedInt(parseInt(K.substr(8,8),16));
  29. a.writeUnsignedInt(parseInt(K.substr(16,8),16));
  30. a.writeUnsignedInt(parseInt(K.substr(24,8),16));
  31. a.position = 0;
  32. return new XTeaKey(a);
  33. }
  34. public function getBlockSize():uint {
  35. return 8;
  36. }
  37. public function encrypt(block:ByteArray, index:uint=0):void {
  38. block.position = index;
  39. var v0:uint = block.readUnsignedInt();
  40. var v1:uint = block.readUnsignedInt();
  41. var i:uint;
  42. var sum:uint =0;
  43. var delta:uint = 0x9E3779B9;
  44. for (i=0; i<NUM_ROUNDS; i++) {
  45. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  46. sum += delta;
  47. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  48. }
  49. block.position-=8;
  50. block.writeUnsignedInt(v0);
  51. block.writeUnsignedInt(v1);
  52. }
  53. public function decrypt(block:ByteArray, index:uint=0):void {
  54. block.position = index;
  55. var v0:uint = block.readUnsignedInt();
  56. var v1:uint = block.readUnsignedInt();
  57. var i:uint;
  58. var delta:uint = 0x9E3779B9;
  59. var sum:uint = delta*NUM_ROUNDS;
  60. for (i=0; i<NUM_ROUNDS; i++) {
  61. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  62. sum -= delta;
  63. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  64. }
  65. block.position-=8;
  66. block.writeUnsignedInt(v0);
  67. block.writeUnsignedInt(v1);
  68. }
  69. public function dispose():void {
  70. //private var k:Array;
  71. var r:Random = new Random;
  72. for (var i:uint=0;i<k.length;i++) {
  73. k[i] = r.nextByte();
  74. delete k[i];
  75. }
  76. k = null;
  77. Memory.gc();
  78. }
  79. public function toString():String {
  80. return "xtea";
  81. }
  82. }
  83. }