summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2004-03-20 20:11:29 (GMT)
committerTim Peters <tim.peters@gmail.com>2004-03-20 20:11:29 (GMT)
commit934d31b1d340f1d309d6ec6a861fa94077033b0f (patch)
tree71b1c73badd16ae2d00737200f8fd8cae38f8733 /Lib
parent1515fc2a013b01819df823a155c6c19e35b9f71a (diff)
downloadcpython-934d31b1d340f1d309d6ec6a861fa94077033b0f.zip
cpython-934d31b1d340f1d309d6ec6a861fa94077033b0f.tar.gz
cpython-934d31b1d340f1d309d6ec6a861fa94077033b0f.tar.bz2
Speed HMAC.copy() by installing a secret backdoor argument to
HMAC.__init__(). Adapted from SF patch 895445 "hmac.HMAC.copy() speedup" by Trevor Perrin, who reported that this approach increased throughput of his hmac-intensive app by 30%.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/hmac.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py
index db9b404..11b0fb3 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -12,6 +12,11 @@ def _strxor(s1, s2):
# hashing module used.
digest_size = None
+# A unique object passed by HMAC.copy() to the HMAC constructor, in order
+# that the latter return very quickly. HMAC("") in contrast is quite
+# expensive.
+_secret_backdoor_key = []
+
class HMAC:
"""RFC2104 HMAC class.
@@ -25,6 +30,10 @@ class HMAC:
msg: Initial input for the hash, if provided.
digestmod: A module supporting PEP 247. Defaults to the md5 module.
"""
+
+ if key is _secret_backdoor_key: # cheap
+ return
+
if digestmod is None:
import md5
digestmod = md5
@@ -60,8 +69,9 @@ class HMAC:
An update to this copy won't affect the original object.
"""
- other = HMAC("")
+ other = HMAC(_secret_backdoor_key)
other.digestmod = self.digestmod
+ other.digest_size = self.digest_size
other.inner = self.inner.copy()
other.outer = self.outer.copy()
return other