diff options
author | Steven D'Aprano <steve@pearwood.info> | 2016-04-15 18:33:55 (GMT) |
---|---|---|
committer | Steven D'Aprano <steve@pearwood.info> | 2016-04-15 18:33:55 (GMT) |
commit | 6dda1b14af1924a0d8f7bb891aa342c358213e8a (patch) | |
tree | 78bfc719f2bda40595b8f76b520d21f77386be60 /Lib/secrets.py | |
parent | d48a202fb6f7d4963340d1e1b6d4133cb2e90a5e (diff) | |
download | cpython-6dda1b14af1924a0d8f7bb891aa342c358213e8a.zip cpython-6dda1b14af1924a0d8f7bb891aa342c358213e8a.tar.gz cpython-6dda1b14af1924a0d8f7bb891aa342c358213e8a.tar.bz2 |
Remove python fallback for compare_digest.
See https://mail.python.org/pipermail/python-dev/2016-April/144198.html
https://mail.python.org/pipermail/python-dev/2016-April/144203.html
Diffstat (limited to 'Lib/secrets.py')
-rw-r--r-- | Lib/secrets.py | 33 |
1 files changed, 1 insertions, 32 deletions
diff --git a/Lib/secrets.py b/Lib/secrets.py index e0f2656..e4e9714 100644 --- a/Lib/secrets.py +++ b/Lib/secrets.py @@ -91,38 +91,7 @@ import base64 import binascii import os -try: - from hmac import compare_digest -except ImportError: - # Python version is too old. Fall back to a pure-Python version. - - import operator - from functools import reduce - - def compare_digest(a, b): - """Return ``a == b`` using an approach resistant to timing analysis. - - a and b must both be of the same type: either both text strings, - or both byte strings. - - Note: If a and b are of different lengths, or if an error occurs, - a timing attack could theoretically reveal information about the - types and lengths of a and b, but not their values. - """ - # For a similar approach, see - # http://codahale.com/a-lesson-in-timing-attacks/ - for T in (bytes, str): - if isinstance(a, T) and isinstance(b, T): - break - else: # for...else - raise TypeError("arguments must be both strings or both bytes") - if len(a) != len(b): - return False - # Thanks to Raymond Hettinger for this one-liner. - return reduce(operator.and_, map(operator.eq, a, b), True) - - - +from hmac import compare_digest from random import SystemRandom _sysrand = SystemRandom() |