PageRenderTime 429ms CodeModel.GetById 31ms RepoModel.GetById 20ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 58 lines | 43 code | 7 blank | 8 comment | 4 complexity | afede4e57e6ec738b4d24cca54b25290 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * CTRMode
  3. *
  4. * An ActionScript 3 implementation of the counter 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. public class CTRMode extends IVMode implements IMode
  13. {
  14. public function CTRMode(key:ISymmetricKey, padding:IPad = null) {
  15. super(key, padding);
  16. }
  17. public function encrypt(src:ByteArray):void
  18. {
  19. padding.pad(src);
  20. var vector:ByteArray = getIV4e();
  21. core(src, vector);
  22. }
  23. public function decrypt(src:ByteArray):void
  24. {
  25. var vector:ByteArray = getIV4d();
  26. core(src, vector);
  27. padding.unpad(src);
  28. }
  29. private function core(src:ByteArray, iv:ByteArray):void {
  30. var X:ByteArray = new ByteArray;
  31. var Xenc:ByteArray = new ByteArray;
  32. X.writeBytes(iv);
  33. for (var i:uint=0;i<src.length;i+=blockSize) {
  34. Xenc.position=0;
  35. Xenc.writeBytes(X);
  36. key.encrypt(Xenc);
  37. for (var j:uint=0;j<blockSize;j++) {
  38. src[i+j] ^= Xenc[j];
  39. }
  40. for (j=blockSize-1;j>=0;--j) {
  41. X[j]++;
  42. if (X[j]!=0)
  43. break;
  44. }
  45. }
  46. }
  47. public function toString():String {
  48. return key.toString()+"-ctr";
  49. }
  50. }
  51. }