/flash-src/third-party/com/hurlant/crypto/hash/MAC.as

http://github.com/gimite/web-socket-js · ActionScript · 137 lines · 73 code · 21 blank · 43 comment · 10 complexity · 0bedba9be73ae40adeb26ca35c8237f4 MD5 · raw file

  1. /**
  2. * MAC
  3. *
  4. * An ActionScript 3 implementation of MAC, Message Authentication Code
  5. * for use with SSL 3.0.
  6. * Loosely copyrighted by Bobby Parker.
  7. * As3crypto copyrighted by Henri Torgemane.
  8. *
  9. * See LICENSE.txt for full license information.
  10. */
  11. package com.hurlant.crypto.hash
  12. {
  13. import flash.utils.ByteArray;
  14. import com.hurlant.util.Hex;
  15. public class MAC implements IHMAC
  16. {
  17. private var hash:IHash;
  18. private var bits:uint;
  19. private var pad_1:ByteArray;
  20. private var pad_2:ByteArray;
  21. private var innerHash:ByteArray;
  22. private var outerHash:ByteArray;
  23. private var outerKey:ByteArray;
  24. private var innerKey:ByteArray;
  25. /**
  26. * Create a MAC object (for SSL 3.0 ) and
  27. * optionally a number of bits to return.
  28. * The MAC will be truncated to that size if needed.
  29. */
  30. public function MAC(hash:IHash, bits:uint=0) {
  31. this.hash = hash;
  32. this.bits = bits;
  33. innerHash = new ByteArray();
  34. outerHash = new ByteArray();
  35. innerKey = new ByteArray();
  36. outerKey = new ByteArray();
  37. if (hash != null) {
  38. var pad_size:int = hash.getPadSize();
  39. pad_1 = new ByteArray();
  40. pad_2 = new ByteArray();
  41. for (var x:int = 0; x < pad_size; x++) {
  42. pad_1.writeByte(0x36);
  43. pad_2.writeByte(0x5c);
  44. }
  45. }
  46. }
  47. public function setPadSize(pad_size:int) : void { }
  48. public function getHashSize():uint {
  49. if (bits!=0) {
  50. return bits/8;
  51. } else {
  52. return hash.getHashSize();
  53. }
  54. }
  55. /**
  56. * Compute a MAC using a key and some data.
  57. *
  58. */
  59. public function compute(key:ByteArray, data:ByteArray):ByteArray {
  60. // take that incoming key and do hash(key + pad_2 + hash(key + pad_1 + sequence + length + record)
  61. // note that data = (sequence + type + length + record)
  62. if (pad_1 == null) {
  63. var pad_size:int = hash.getPadSize();
  64. pad_1 = new ByteArray();
  65. pad_2 = new ByteArray();
  66. for (var x:int = 0; x < pad_size; x++) {
  67. pad_1.writeByte(0x36);
  68. pad_2.writeByte(0x5c);
  69. }
  70. }
  71. // Do some preliminary checking on stuff
  72. /*
  73. if (key.length > hash.getInputSize()) {
  74. hashKey = hash.hash(key);
  75. } else {
  76. hashKey = new ByteArray;
  77. hashKey.writeBytes(key);
  78. }
  79. while (hashKey.length < hash.getInputSize() ) {
  80. hashKey[hashKey.length] = 0;
  81. } */
  82. // Henri's conventions work just fine here..
  83. innerKey.length = 0;
  84. outerKey.length = 0;
  85. // trace("MAC Key: " + Hex.fromArray(key));
  86. // trace("Key Length: " + key.length);
  87. // trace("Pad_1 : " + Hex.fromArray(pad_1));
  88. // inner hash calc
  89. innerKey.writeBytes(key);
  90. innerKey.writeBytes(pad_1);
  91. innerKey.writeBytes(data);
  92. // trace("MAC Inner Key: " + Hex.fromArray(innerKey));
  93. innerHash = hash.hash(innerKey);
  94. // trace("MAC Inner Hash: " + Hex.fromArray(innerHash));
  95. // outer hash calc
  96. outerKey.writeBytes(key);
  97. outerKey.writeBytes(pad_2);
  98. outerKey.writeBytes(innerHash);
  99. // trace("MAC Outer Key: " + Hex.fromArray(outerKey));
  100. outerHash = hash.hash(outerKey);
  101. if (bits > 0 && bits < 8*outerHash.length) {
  102. outerHash.length = bits/8;
  103. }
  104. // trace("MAC for record: " + Hex.fromArray(outerHash));
  105. return outerHash;
  106. }
  107. public function dispose():void {
  108. hash = null;
  109. bits = 0;
  110. }
  111. public function toString():String {
  112. return "mac-"+(bits>0?bits+"-":"")+hash.toString();
  113. }
  114. }
  115. }