blob: db9b404c9a97a0bb2057a00fce65e9d8f65d65b4 [file] [log] [blame]
Guido van Rossum8ceef412001-09-11 15:54:00 +00001"""HMAC (Keyed-Hashing for Message Authentication) Python module.
2
3Implements the HMAC algorithm as described by RFC 2104.
4"""
5
Guido van Rossum8ceef412001-09-11 15:54:00 +00006def _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))
Tim Petersb64bec32001-09-18 02:26:39 +000010
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000011# The size of the digests returned by HMAC depends on the underlying
12# hashing module used.
13digest_size = None
14
Guido van Rossum8ceef412001-09-11 15:54:00 +000015class HMAC:
16 """RFC2104 HMAC class.
17
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000018 This supports the API for Cryptographic Hash Functions (PEP 247).
Tim Petersb64bec32001-09-18 02:26:39 +000019 """
Guido van Rossum8ceef412001-09-11 15:54:00 +000020
21 def __init__(self, key, msg = None, digestmod = None):
22 """Create a new HMAC object.
23
24 key: key for the keyed hash object.
25 msg: Initial input for the hash, if provided.
26 digestmod: A module supporting PEP 247. Defaults to the md5 module.
27 """
Raymond Hettinger7fdfc2d2002-05-31 17:49:10 +000028 if digestmod is None:
Guido van Rossum8ceef412001-09-11 15:54:00 +000029 import md5
30 digestmod = md5
31
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000032 self.digestmod = digestmod
Guido van Rossum8ceef412001-09-11 15:54:00 +000033 self.outer = digestmod.new()
34 self.inner = digestmod.new()
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000035 self.digest_size = digestmod.digest_size
Tim Peters88768482001-11-13 21:51:26 +000036
Guido van Rossum8ceef412001-09-11 15:54:00 +000037 blocksize = 64
38 ipad = "\x36" * blocksize
39 opad = "\x5C" * blocksize
40
41 if len(key) > blocksize:
42 key = digestmod.new(key).digest()
43
44 key = key + chr(0) * (blocksize - len(key))
45 self.outer.update(_strxor(key, opad))
46 self.inner.update(_strxor(key, ipad))
Raymond Hettinger094662a2002-06-01 01:29:16 +000047 if msg is not None:
Guido van Rossum8ceef412001-09-11 15:54:00 +000048 self.update(msg)
49
50## def clear(self):
51## raise NotImplementedError, "clear() method not available in HMAC."
52
53 def update(self, msg):
54 """Update this hashing object with the string msg.
55 """
56 self.inner.update(msg)
57
58 def copy(self):
59 """Return a separate copy of this hashing object.
60
61 An update to this copy won't affect the original object.
62 """
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000063 other = HMAC("")
64 other.digestmod = self.digestmod
65 other.inner = self.inner.copy()
66 other.outer = self.outer.copy()
67 return other
Guido van Rossum8ceef412001-09-11 15:54:00 +000068
69 def digest(self):
70 """Return the hash value of this hashing object.
71
72 This returns a string containing 8-bit data. The object is
73 not altered in any way by this function; you can continue
74 updating the object after calling this function.
75 """
76 h = self.outer.copy()
77 h.update(self.inner.digest())
78 return h.digest()
79
80 def hexdigest(self):
81 """Like digest(), but returns a string of hexadecimal digits instead.
82 """
Walter Dörwald65230a22002-06-03 15:58:32 +000083 return "".join([hex(ord(x))[2:].zfill(2)
Guido van Rossum8ceef412001-09-11 15:54:00 +000084 for x in tuple(self.digest())])
85
86def new(key, msg = None, digestmod = None):
87 """Create a new hashing object and return it.
88
89 key: The starting key for the hash.
90 msg: if available, will immediately be hashed into the object's starting
Tim Petersb64bec32001-09-18 02:26:39 +000091 state.
Guido van Rossum8ceef412001-09-11 15:54:00 +000092
93 You can now feed arbitrary strings into the object using its update()
94 method, and can ask for the hash value at any time by calling its digest()
95 method.
96 """
97 return HMAC(key, msg, digestmod)