summaryrefslogtreecommitdiff
path: root/passlib
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-12-05 14:44:39 -0500
committerEli Collins <elic@assurancetechnologies.com>2011-12-05 14:44:39 -0500
commit038b57325743dddbaf37c57adfa7979efb6b0bb7 (patch)
tree12606a63eb399a3c1d1fc9d88ea321aef305bbef /passlib
parent35ceb3862ff6ab8187122003421670d263ced591 (diff)
downloadpasslib-038b57325743dddbaf37c57adfa7979efb6b0bb7.tar.gz
tweaked consteq so it should now take precisely the same time regardless of length differences
Diffstat (limited to 'passlib')
-rw-r--r--passlib/utils/__init__.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index bc035fc..a08cacb 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -495,13 +495,15 @@ def consteq(left, right):
raise TypeError("inputs must be both unicode or bytes")
# do size comparison.
- # XXX: this does one extra branch instruction when the string lengths don't
- # match, compared to when they do. would be nice to equalize the cases.
- if len(left) == len(right):
+ # NOTE: the double-if construction below is done deliberately, to ensure
+ # the same number of operations (including branches) is performed regardless
+ # of whether left & right are the same size.
+ same = (len(left) == len(right))
+ if same:
# if sizes are the same, setup loop to perform actual check of contents.
tmp = left
result = 0
- else:
+ if not same:
# if sizes aren't the same, set 'result' so equality will fail regardless
# of contents. then, to ensure we do exactly 'len(right)' iterations
# of the loop, just compare 'right' against itself.