/gdata/Crypto/Hash/HMAC.py

http://radioappz.googlecode.com/ · Python · 108 lines · 48 code · 17 blank · 43 comment · 8 complexity · bb4d0e12154cdb9aeef5dde886ce9b0b MD5 · raw file

  1. """HMAC (Keyed-Hashing for Message Authentication) Python module.
  2. Implements the HMAC algorithm as described by RFC 2104.
  3. This is just a copy of the Python 2.2 HMAC module, modified to work when
  4. used on versions of Python before 2.2.
  5. """
  6. __revision__ = "$Id: HMAC.py,v 1.5 2002/07/25 17:19:02 z3p Exp $"
  7. import string
  8. def _strxor(s1, s2):
  9. """Utility method. XOR the two strings s1 and s2 (must have same length).
  10. """
  11. return "".join(map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2))
  12. # The size of the digests returned by HMAC depends on the underlying
  13. # hashing module used.
  14. digest_size = None
  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 digestmod == None:
  26. import md5
  27. digestmod = md5
  28. self.digestmod = digestmod
  29. self.outer = digestmod.new()
  30. self.inner = digestmod.new()
  31. try:
  32. self.digest_size = digestmod.digest_size
  33. except AttributeError:
  34. self.digest_size = len(self.outer.digest())
  35. blocksize = 64
  36. ipad = "\x36" * blocksize
  37. opad = "\x5C" * blocksize
  38. if len(key) > blocksize:
  39. key = digestmod.new(key).digest()
  40. key = key + chr(0) * (blocksize - len(key))
  41. self.outer.update(_strxor(key, opad))
  42. self.inner.update(_strxor(key, ipad))
  43. if (msg):
  44. self.update(msg)
  45. ## def clear(self):
  46. ## raise NotImplementedError, "clear() method not available in HMAC."
  47. def update(self, msg):
  48. """Update this hashing object with the string msg.
  49. """
  50. self.inner.update(msg)
  51. def copy(self):
  52. """Return a separate copy of this hashing object.
  53. An update to this copy won't affect the original object.
  54. """
  55. other = HMAC("")
  56. other.digestmod = self.digestmod
  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([string.zfill(hex(ord(x))[2:], 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)