PageRenderTime 19ms CodeModel.GetById 12ms 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/IVMode.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 110 lines | 83 code | 7 blank | 20 comment | 13 complexity | c9d563696b76adde5c8b51197b7e2ff7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * IVMode
  3. *
  4. * An abstract class for confidentialy modes that rely on an initialization vector.
  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.crypto.tests.TestCase;
  13. import com.hurlant.util.Memory;
  14. import flash.utils.ByteArray;
  15. /**
  16. * An "abtract" class to avoid redundant code in subclasses
  17. */
  18. public class IVMode
  19. {
  20. protected var key:ISymmetricKey;
  21. protected var padding:IPad;
  22. // random generator used to generate IVs
  23. protected var prng:Random;
  24. // optional static IV. used for testing only.
  25. protected var iv:ByteArray;
  26. // generated IV is stored here.
  27. protected var lastIV:ByteArray;
  28. protected var blockSize:uint;
  29. public function IVMode(key:ISymmetricKey, padding:IPad = null) {
  30. this.key = key;
  31. blockSize = key.getBlockSize();
  32. if (padding == null) {
  33. padding = new PKCS5(blockSize);
  34. } else {
  35. padding.setBlockSize(blockSize);
  36. }
  37. this.padding = padding;
  38. prng = new Random;
  39. iv = null;
  40. lastIV = new ByteArray;
  41. }
  42. public function getBlockSize():uint {
  43. return key.getBlockSize();
  44. }
  45. public function dispose():void {
  46. var i:uint;
  47. if (iv != null) {
  48. for (i=0;i<iv.length;i++) {
  49. iv[i] = prng.nextByte();
  50. }
  51. iv.length=0;
  52. iv = null;
  53. }
  54. if (lastIV != null) {
  55. for (i=0;i<iv.length;i++) {
  56. lastIV[i] = prng.nextByte();
  57. }
  58. lastIV.length=0;
  59. lastIV=null;
  60. }
  61. key.dispose();
  62. key = null;
  63. padding = null;
  64. prng.dispose();
  65. prng = null;
  66. Memory.gc();
  67. }
  68. /**
  69. * Optional function to force the IV value.
  70. * Normally, an IV gets generated randomly at every encrypt() call.
  71. * Also, use this to set the IV before calling decrypt()
  72. * (if not set before decrypt(), the IV is read from the beginning of the stream.)
  73. */
  74. public function set IV(value:ByteArray):void {
  75. iv = value;
  76. lastIV.length=0;
  77. lastIV.writeBytes(iv);
  78. }
  79. public function get IV():ByteArray {
  80. return lastIV;
  81. }
  82. protected function getIV4e():ByteArray {
  83. var vec:ByteArray = new ByteArray;
  84. if (iv) {
  85. vec.writeBytes(iv);
  86. } else {
  87. prng.nextBytes(vec, blockSize);
  88. }
  89. lastIV.length=0;
  90. lastIV.writeBytes(vec);
  91. return vec;
  92. }
  93. protected function getIV4d():ByteArray {
  94. var vec:ByteArray = new ByteArray;
  95. if (iv) {
  96. vec.writeBytes(iv);
  97. } else {
  98. throw new Error("an IV must be set before calling decrypt()");
  99. }
  100. return vec;
  101. }
  102. }
  103. }