Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 934d31b

Browse files
committed
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%.
1 parent 1515fc2 commit 934d31b

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/hmac.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def _strxor(s1, s2):
1212
# hashing module used.
1313
digest_size = None
1414

15+
# A unique object passed by HMAC.copy() to the HMAC constructor, in order
16+
# that the latter return very quickly. HMAC("") in contrast is quite
17+
# expensive.
18+
_secret_backdoor_key = []
19+
1520
class HMAC:
1621
"""RFC2104 HMAC class.
1722
@@ -25,6 +30,10 @@ def __init__(self, key, msg = None, digestmod = None):
2530
msg: Initial input for the hash, if provided.
2631
digestmod: A module supporting PEP 247. Defaults to the md5 module.
2732
"""
33+
34+
if key is _secret_backdoor_key: # cheap
35+
return
36+
2837
if digestmod is None:
2938
import md5
3039
digestmod = md5
@@ -60,8 +69,9 @@ def copy(self):
6069
6170
An update to this copy won't affect the original object.
6271
"""
63-
other = HMAC("")
72+
other = HMAC(_secret_backdoor_key)
6473
other.digestmod = self.digestmod
74+
other.digest_size = self.digest_size
6575
other.inner = self.inner.copy()
6676
other.outer = self.outer.copy()
6777
return other

0 commit comments

Comments
 (0)