blob: cae08002e55dfaa909dd00cd3f695a459415f907 [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
6import string
7
8def _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))
Tim Petersb64bec32001-09-18 02:26:39 +000012
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000013# The size of the digests returned by HMAC depends on the underlying
14# hashing module used.
15digest_size = None
16
Guido van Rossum8ceef412001-09-11 15:54:00 +000017class HMAC:
18 """RFC2104 HMAC class.
19
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000020 This supports the API for Cryptographic Hash Functions (PEP 247).
Tim Petersb64bec32001-09-18 02:26:39 +000021 """
Guido van Rossum8ceef412001-09-11 15:54:00 +000022
23 def __init__(self, key, msg = None, digestmod = None):
24 """Create a new HMAC object.
25
26 key: key for the keyed hash object.
27 msg: Initial input for the hash, if provided.
28 digestmod: A module supporting PEP 247. Defaults to the md5 module.
29 """
30 if digestmod == None:
31 import md5
32 digestmod = md5
33
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000034 self.digestmod = digestmod
Guido van Rossum8ceef412001-09-11 15:54:00 +000035 self.outer = digestmod.new()
36 self.inner = digestmod.new()
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000037 self.digest_size = digestmod.digest_size
Tim Peters88768482001-11-13 21:51:26 +000038
Guido van Rossum8ceef412001-09-11 15:54:00 +000039 blocksize = 64
40 ipad = "\x36" * blocksize
41 opad = "\x5C" * blocksize
42
43 if len(key) > blocksize:
44 key = digestmod.new(key).digest()
45
46 key = key + chr(0) * (blocksize - len(key))
47 self.outer.update(_strxor(key, opad))
48 self.inner.update(_strxor(key, ipad))
49 if (msg):
50 self.update(msg)
51
52## def clear(self):
53## raise NotImplementedError, "clear() method not available in HMAC."
54
55 def update(self, msg):
56 """Update this hashing object with the string msg.
57 """
58 self.inner.update(msg)
59
60 def copy(self):
61 """Return a separate copy of this hashing object.
62
63 An update to this copy won't affect the original object.
64 """
Andrew M. Kuchling1ccdff92001-11-02 21:49:20 +000065 other = HMAC("")
66 other.digestmod = self.digestmod
67 other.inner = self.inner.copy()
68 other.outer = self.outer.copy()
69 return other
Guido van Rossum8ceef412001-09-11 15:54:00 +000070
71 def digest(self):
72 """Return the hash value of this hashing object.
73
74 This returns a string containing 8-bit data. The object is
75 not altered in any way by this function; you can continue
76 updating the object after calling this function.
77 """
78 h = self.outer.copy()
79 h.update(self.inner.digest())
80 return h.digest()
81
82 def hexdigest(self):
83 """Like digest(), but returns a string of hexadecimal digits instead.
84 """
85 return "".join([string.zfill(hex(ord(x))[2:], 2)
86 for x in tuple(self.digest())])
87
88def new(key, msg = None, digestmod = None):
89 """Create a new hashing object and return it.
90
91 key: The starting key for the hash.
92 msg: if available, will immediately be hashed into the object's starting
Tim Petersb64bec32001-09-18 02:26:39 +000093 state.
Guido van Rossum8ceef412001-09-11 15:54:00 +000094
95 You can now feed arbitrary strings into the object using its update()
96 method, and can ask for the hash value at any time by calling its digest()
97 method.
98 """
99 return HMAC(key, msg, digestmod)