PageRenderTime 18ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 86 lines | 65 code | 7 blank | 14 comment | 6 complexity | a242a511223c6b4c66f8812633d278c9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * ECBMode
  3. *
  4. * An ActionScript 3 implementation of the ECB confidentiality mode
  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 flash.utils.ByteArray;
  12. import com.hurlant.util.Memory;
  13. import com.hurlant.util.Hex;
  14. /**
  15. * ECB mode.
  16. * This uses a padding and a symmetric key.
  17. * If no padding is given, PKCS#5 is used.
  18. */
  19. public class ECBMode implements IMode, ICipher
  20. {
  21. private var key:ISymmetricKey;
  22. private var padding:IPad;
  23. public function ECBMode(key:ISymmetricKey, padding:IPad = null) {
  24. this.key = key;
  25. if (padding == null) {
  26. padding = new PKCS5(key.getBlockSize());
  27. } else {
  28. padding.setBlockSize(key.getBlockSize());
  29. }
  30. this.padding = padding;
  31. }
  32. public function getBlockSize():uint {
  33. return key.getBlockSize();
  34. }
  35. public function encrypt(src:ByteArray):void {
  36. padding.pad(src);
  37. src.position = 0;
  38. var blockSize:uint = key.getBlockSize();
  39. var tmp:ByteArray = new ByteArray;
  40. var dst:ByteArray = new ByteArray;
  41. for (var i:uint=0;i<src.length;i+=blockSize) {
  42. tmp.length=0;
  43. src.readBytes(tmp, 0, blockSize);
  44. key.encrypt(tmp);
  45. dst.writeBytes(tmp);
  46. }
  47. src.length=0;
  48. src.writeBytes(dst);
  49. }
  50. public function decrypt(src:ByteArray):void {
  51. src.position = 0;
  52. var blockSize:uint = key.getBlockSize();
  53. // sanity check.
  54. if (src.length%blockSize!=0) {
  55. throw new Error("ECB mode cipher length must be a multiple of blocksize "+blockSize);
  56. }
  57. var tmp:ByteArray = new ByteArray;
  58. var dst:ByteArray = new ByteArray;
  59. for (var i:uint=0;i<src.length;i+=blockSize) {
  60. tmp.length=0;
  61. src.readBytes(tmp, 0, blockSize);
  62. key.decrypt(tmp);
  63. dst.writeBytes(tmp);
  64. }
  65. padding.unpad(dst);
  66. src.length=0;
  67. src.writeBytes(dst);
  68. }
  69. public function dispose():void {
  70. key.dispose();
  71. key = null;
  72. padding = null;
  73. Memory.gc();
  74. }
  75. public function toString():String {
  76. return key.toString()+"-ecb";
  77. }
  78. }
  79. }