hmac.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """HMAC (Keyed-Hashing for Message Authentication) Python module.
  2. Implements the HMAC algorithm as described by RFC 2104.
  3. """
  4. def _strxor(s1, s2):
  5. """Utility method. XOR the two strings s1 and s2 (must have same length).
  6. """
  7. return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
  8. # The size of the digests returned by HMAC depends on the underlying
  9. # hashing module used.
  10. digest_size = None
  11. # A unique object passed by HMAC.copy() to the HMAC constructor, in order
  12. # that the latter return very quickly. HMAC("") in contrast is quite
  13. # expensive.
  14. _secret_backdoor_key = []
  15. class HMAC:
  16. """RFC2104 HMAC class.
  17. This supports the API for Cryptographic Hash Functions (PEP 247).
  18. """
  19. def __init__(self, key, msg = None, digestmod = None):
  20. """Create a new HMAC object.
  21. key: key for the keyed hash object.
  22. msg: Initial input for the hash, if provided.
  23. digestmod: A module supporting PEP 247. Defaults to the md5 module.
  24. """
  25. if key is _secret_backdoor_key: # cheap
  26. return
  27. if digestmod is None:
  28. import md5
  29. digestmod = md5
  30. self.digestmod = digestmod
  31. self.outer = digestmod.new()
  32. self.inner = digestmod.new()
  33. self.digest_size = digestmod.digest_size
  34. blocksize = 64
  35. ipad = "\x36" * blocksize
  36. opad = "\x5C" * blocksize
  37. if len(key) > blocksize:
  38. key = digestmod.new(key).digest()
  39. key = key + chr(0) * (blocksize - len(key))
  40. self.outer.update(_strxor(key, opad))
  41. self.inner.update(_strxor(key, ipad))
  42. if msg is not None:
  43. self.update(msg)
  44. ## def clear(self):
  45. ## raise NotImplementedError, "clear() method not available in HMAC."
  46. def update(self, msg):
  47. """Update this hashing object with the string msg.
  48. """
  49. self.inner.update(msg)
  50. def copy(self):
  51. """Return a separate copy of this hashing object.
  52. An update to this copy won't affect the original object.
  53. """
  54. other = HMAC(_secret_backdoor_key)
  55. other.digestmod = self.digestmod
  56. other.digest_size = self.digest_size
  57. other.inner = self.inner.copy()
  58. other.outer = self.outer.copy()
  59. return other
  60. def digest(self):
  61. """Return the hash value of this hashing object.
  62. This returns a string containing 8-bit data. The object is
  63. not altered in any way by this function; you can continue
  64. updating the object after calling this function.
  65. """
  66. h = self.outer.copy()
  67. h.update(self.inner.digest())
  68. return h.digest()
  69. def hexdigest(self):
  70. """Like digest(), but returns a string of hexadecimal digits instead.
  71. """
  72. return "".join([hex(ord(x))[2:].zfill(2)
  73. for x in tuple(self.digest())])
  74. def new(key, msg = None, digestmod = None):
  75. """Create a new hashing object and return it.
  76. key: The starting key for the hash.
  77. msg: if available, will immediately be hashed into the object's starting
  78. state.
  79. You can now feed arbitrary strings into the object using its update()
  80. method, and can ask for the hash value at any time by calling its digest()
  81. method.
  82. """
  83. return HMAC(key, msg, digestmod)