PageRenderTime 16ms CodeModel.GetById 10ms 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/hash/HMAC.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 82 lines | 56 code | 6 blank | 20 comment | 8 complexity | c96c9bc0e0b64eed7e2d8c27dc29c561 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * HMAC
  3. *
  4. * An ActionScript 3 implementation of HMAC, Keyed-Hashing for Message
  5. * Authentication, as defined by RFC-2104
  6. * Copyright (c) 2007 Henri Torgemane
  7. *
  8. * See LICENSE.txt for full license information.
  9. */
  10. package com.hurlant.crypto.hash
  11. {
  12. import flash.utils.ByteArray;
  13. import com.hurlant.util.Hex;
  14. public class HMAC implements IHMAC
  15. {
  16. private var hash:IHash;
  17. private var bits:uint;
  18. /**
  19. * Create a HMAC object, using a Hash function, and
  20. * optionally a number of bits to return.
  21. * The HMAC will be truncated to that size if needed.
  22. */
  23. public function HMAC(hash:IHash, bits:uint=0) {
  24. this.hash = hash;
  25. this.bits = bits;
  26. }
  27. public function getHashSize():uint {
  28. if (bits!=0) {
  29. return bits/8;
  30. } else {
  31. return hash.getHashSize();
  32. }
  33. }
  34. /**
  35. * Compute a HMAC using a key and some data.
  36. * It doesn't modify either, and returns a new ByteArray with the HMAC value.
  37. */
  38. public function compute(key:ByteArray, data:ByteArray):ByteArray {
  39. var hashKey:ByteArray;
  40. if (key.length>hash.getInputSize()) {
  41. hashKey = hash.hash(key);
  42. } else {
  43. hashKey = new ByteArray;
  44. hashKey.writeBytes(key);
  45. }
  46. while (hashKey.length<hash.getInputSize()) {
  47. hashKey[hashKey.length]=0;
  48. }
  49. var innerKey:ByteArray = new ByteArray;
  50. var outerKey:ByteArray = new ByteArray;
  51. for (var i:uint=0;i<hashKey.length;i++) {
  52. innerKey[i] = hashKey[i] ^ 0x36;
  53. outerKey[i] = hashKey[i] ^ 0x5c;
  54. }
  55. // inner + data
  56. innerKey.position = hashKey.length;
  57. innerKey.writeBytes(data);
  58. var innerHash:ByteArray = hash.hash(innerKey);
  59. // outer + innerHash
  60. outerKey.position = hashKey.length;
  61. outerKey.writeBytes(innerHash);
  62. var outerHash:ByteArray = hash.hash(outerKey);
  63. if (bits>0 && bits<8*outerHash.length) {
  64. outerHash.length = bits/8;
  65. }
  66. return outerHash;
  67. }
  68. public function dispose():void {
  69. hash = null;
  70. bits = 0;
  71. }
  72. public function toString():String {
  73. return "hmac-"+(bits>0?bits+"-":"")+hash.toString();
  74. }
  75. }
  76. }