/gdata/tlslite/utils/hmac.py

http://radioappz.googlecode.com/ · Python · 104 lines · 47 code · 14 blank · 43 comment · 7 complexity · db9b1a48e4d95d87c10755bef5c1a9d3 MD5 · raw file

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