def _prepare_rounds(self, handler, opts, settings): "helper for prepare_default_settings" mn = opts.get("min_rounds") mx = opts.get("max_rounds") rounds = settings.get("rounds") if rounds is None: df = opts.get("default_rounds") or mx or mn if df is not None: vr = opts.get("vary_rounds") if vr: if isinstance(vr, str): rc = getattr(handler, "rounds_cost", "linear") vr = int(vr.rstrip("%")) #NOTE: deliberately strip >1 %, #in case an interpolation-escaped %% #makes it through to here. assert 0 <= vr < 100 if rc == "log2": #let % variance scale the number of actual rounds, not the logarithmic value df = 2**df vr = int(df*vr/100) lower = int(logb(df-vr,2)+.5) #err on the side of strength - round up upper = int(logb(df+vr,2)) else: assert rc == "linear" vr = int(df*vr/100) lower = df-vr upper = df+vr else: lower = df-vr upper = df+vr if lower < 1: lower = 1 if mn and lower < mn: lower = mn if mx and upper > mx: upper = mx if lower > upper: #NOTE: this mainly happens when default_rounds>max_rounds, which shouldn't usually happen rounds = upper warn("vary default rounds: lower bound > upper bound, using upper bound (%d > %d)" % (lower, upper)) else: rounds = rng.randint(lower, upper) else: rounds = df if rounds is not None: if mx and rounds > mx: rounds = mx if mn and rounds < mn: #give mn predence if mn > mx rounds = mn settings['rounds'] = rounds
def guess_rounds(rps, target): return int(logb(rps*target,2)+.5)
'sys_bits', 'unix_crypt_schemes', ] #================================================================================= #constants #================================================================================= #: detect what we're running on pypy_vm = hasattr(sys, "pypy_version_info") jython_vm = sys.platform.startswith('java') py3k_lang = sys.version_info >= (3,0) py32_lang = sys.version_info >= (3,2) #: number of bits in system architecture sys_bits = int(logb(sys.maxint,2)+1.5) assert sys_bits in (32,64), "unexpected sys_bits value: %r" % (sys_bits,) #: list of names of hashes found in unix crypt implementations... unix_crypt_schemes = [ "sha512_crypt", "sha256_crypt", "sha1_crypt", "bcrypt", "md5_crypt", "bsdi_crypt", "des_crypt" ] #: list of rounds_cost constants rounds_cost_values = [ "linear", "log2" ] #: special byte string containing all possible byte values, used in a few places. #XXX: treated as singleton by some of the code for efficiency.