summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-12-28 17:38:39 -0500
committerEli Collins <elic@assurancetechnologies.com>2011-12-28 17:38:39 -0500
commit92eff681b7858966e82453f9a0a03a86d09e1045 (patch)
treedbec58c3cdc728ab4374f283418c64f4dd6d4f78
parentfec9d1c45fcd201eea26f5ce276825302f10dce0 (diff)
downloadpasslib-92eff681b7858966e82453f9a0a03a86d09e1045.tar.gz
mvt code now uses time.clock() under win32
-rw-r--r--passlib/context.py17
-rw-r--r--passlib/utils/__init__.py36
2 files changed, 41 insertions, 12 deletions
diff --git a/passlib/context.py b/passlib/context.py
index 7e6e7fd..9454b77 100644
--- a/passlib/context.py
+++ b/passlib/context.py
@@ -9,16 +9,9 @@ import re
import hashlib
from math import log as logb, ceil
import logging; log = logging.getLogger(__name__)
-##import sys
-##if sys.platform == "win32":
-## # On Windows, the best timer is time.clock()
-## from time import clock as timer
-##else:
-## # On most other platforms the best timer is time.time()
-## from time import time as timer
-import time
import os
import re
+from time import sleep
from warnings import warn
#site
try:
@@ -30,7 +23,7 @@ except ImportError:
from passlib.registry import get_crypt_handler, _validate_handler_name
from passlib.utils import to_bytes, to_unicode, bytes, \
is_crypt_handler, rng, \
- PasslibPolicyWarning
+ PasslibPolicyWarning, timer
from passlib.utils.compat import is_mapping, iteritems, num_types, \
PY3, PY_MIN_32, unicode, bytes
from passlib.utils.compat.aliases import SafeConfigParser, StringIO, BytesIO
@@ -1025,12 +1018,12 @@ class _CryptRecord(object):
"verify helper - adds min_verify_time delay"
mvt = self._min_verify_time
assert mvt
- start = time.time()
+ start = timer()
ok = self.handler.verify(secret, hash, **context)
- end = time.time()
+ end = timer()
delta = mvt + start - end
if delta > 0:
- time.sleep(delta)
+ sleep(delta)
elif delta < 0:
#warn app they aren't being protected against timing attacks...
warn("CryptContext: verify exceeded min_verify_time: "
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index 604a476..a3d6b8b 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -51,6 +51,7 @@ __all__ = [
"xor_bytes",
#random
+ 'timer',
'rng',
'getrandbytes',
'getrandstr',
@@ -398,6 +399,7 @@ _add_doc(to_native_str,
:returns: :class:`str` instance
""")
+# DEPRECATED
def to_hash_str(source, encoding="ascii"):
"deprecated, use to_native_str() instead"
warn("to_hash_str() is deprecated, and will be removed in passlib 1.7",
@@ -490,6 +492,7 @@ def consteq(left, right):
result |= ord(l) ^ ord(r)
return result == 0
+# DEPRECATED
def splitcomma(source, sep=","):
"""split comma-separated string into list of elements,
stripping whitespace and discarding empty elements.
@@ -805,6 +808,39 @@ def adapted_b64_decode(data, sixthree="."):
#randomness
#=================================================================================
+# pick best timer function to expose as "timer" - lifted from timeit module.
+if sys.platform == "win32":
+ # On Windows, the best timer is time.clock()
+ from time import clock as timer
+else:
+ # On most other platforms the best timer is time.time()
+ from time import time as timer
+
+# works but not used
+##def _get_timer_resolution(timer=timer, repeat=3):
+## best = None
+## i = 0
+## while i < repeat:
+## start = end = timer()
+## while start == end:
+## end = timer()
+## delta = end-start
+## if delta < 0:
+## # probably NTP adjust or some such.
+## log.error("timer jumped backwards! (%r => %r)", start, end)
+## continue
+## if delta > 1:
+## # should have at least this resolution,
+## # so probably NTP adjust or some such.
+## log.error("timer jumped too far! (%r => %r)", start, end)
+## continue
+## if best is None or delta < best:
+## best = delta
+## i += 1
+## return best
+##
+##timer_resolution = _get_timer_resolution()
+
#-----------------------------------------------------------------------
# setup rng for generating salts
#-----------------------------------------------------------------------