a fWc%@sdZddlmZddlZeeZddlmZddl m Z ddl m Z ddl mZdd gZd Zd Zd Zd dZdZd"ddZdadad#dd ZddZddZddZdZeeeedZd$ddZedd d!Z dS)%zg passlib.utils.scrypt -- scrypt hash frontend and help utilities XXX: add this module to public docs? )absolute_importN)warn)exc)to_bytes)PYPYvalidatescryptli?cCsh|dkrtd||dkr(td|||tkrDtd||f|dksX||d@rdtd|dS)aI helper which validates a set of scrypt config parameters. scrypt will take ``O(n * r * p)`` time and ``O(n * r)`` memory. limitations are that ``n = 2**``, ``n < 2**(16*r)``, ``r * p < 2 ** 30``. :param n: scrypt rounds :param r: scrypt block size :param p: scrypt parallel factor zr must be > 0: r=%rzp must be > 0: p=%rz!r * p must be < 2**30: r=%r, p=%rz%n must be > 1, and a power of 2: n=%rT) ValueErrorMAX_RP)nrprB/usr/lib/python3.9/site-packages/passlib/crypto/scrypt/__init__.pyr)s     ?cCs,|d|d|dt}t||}|S)a? calculate memory required for parameter combination. assumes parameters have already been validated. .. warning:: this is derived from OpenSSL's scrypt maxmem formula; and may not be correct for other implementations (additional buffers, different parallelism tradeoffs, etc). r ) UINT32_SIZEint)rrrZfudgemaxmemrrrestimate_maxmemHs rr rcCsZt|||t|dd}t|dd}|dkr4td|tkrHtdtt||||||S)a4run SCrypt key derivation function using specified parameters. :arg secret: passphrase string (unicode is encoded to bytes using utf-8). :arg salt: salt string (unicode is encoded to bytes using utf-8). :arg n: integer 'N' parameter :arg r: integer 'r' parameter :arg p: integer 'p' parameter :arg keylen: number of bytes of key to generate. defaults to 32 (the internal block size). :returns: a *keylen*-sized bytes instance SCrypt imposes a number of constraints on it's input parameters: * ``r * p < 2**30`` -- due to a limitation of PBKDF2-HMAC-SHA256. * ``keylen < (2**32 - 1) * 32`` -- due to a limitation of PBKDF2-HMAC-SHA256. * ``n`` must a be a power of 2, and > 1 -- internal limitation of scrypt() implementation :raises ValueError: if the provided parameters are invalid (see constraints above). .. warning:: Unless the third-party ``scrypt ``_ package is installed, passlib will use a builtin pure-python implementation of scrypt, which is *considerably* slower (and thus requires a much lower / less secure ``n`` value in order to be usuable). Installing the :mod:`!scrypt` package is strongly recommended. secret)Zparamsaltr zkeylen must be at least 1zkeylen too large, must be <= %d)rrr MAX_KEYLEN_scrypt)rrrrrkeylenrrrrks)    cCs.trdnd}td|tjddlm}|jS)zD Load pure-python scrypt implementation built into passlib. dzUsing builtin scrypt backend, which is %dx slower than is required for adequate security. Installing scrypt support (via 'pip install scrypt') is strongly recommendedr ) ScryptEngine)rrrZPasslibSecurityWarningZ_builtinr"Zexecute)Zslowdownr"rrr_load_builtin_backends  r#c Cszddlm}|WSty$Yn0z ddl}Wn<tyn}z$dt|vrZtdtjWYd}~nd}~00tdtjdS)z Try to import the ctypes-based scrypt hash function provided by the ``scrypt ``_ package. r)hashNrzJ'scrypt' package failed to import correctly (possible installation issue?)z5'scrypt' package is too old (lacks ``hash()`` method))rr$ ImportErrorstrrrZPasslibWarning)r$rerrrrr_load_cffi_backends     r(cs6zddlmWnty$YdS0fdd}|S)zf Attempt to load stdlib scrypt() implement and return wrapper. Returns None if not found. r)rNc s.t}|dkrt|||}|||||||dS)Nr)passwordrrrrZdklenr) SCRYPT_MAXMEMr)rrrrrrrZ stdlib_scryptrrstdlib_scrypt_wrappers  z3_load_stdlib_backend..stdlib_scrypt_wrapper)Zhashlibrr%)r,rr+r_load_stdlib_backends   r-)ZstdlibrZbuiltinFc Cs|dkr dS|dkrVtD]0}zt||dWStjyFYqYq0qtdnDt|}|srtd|f|}|std||rdS|a|adS)z set backend for scrypt(). if name not specified, loads first available. :raises ~passlib.exc.MissingBackendError: if backend can't be found .. note:: mainly intended to be called by unittests, and scrypt hash handler anyNdefaultdryrunzno scrypt backends availablezunknown scrypt backend: %rzscrypt backend %r not available) backend_values _set_backendrMissingBackendError_backend_loadersgetr backendr)namer1loaderr$rrrr3s&   r3r/cCs.zt|ddWdStjy(YdS0dS)NTr0F)r3rr4)r8rrr _has_backends  r:)r)r r)F)!__doc__Z __future__rZloggingZ getLogger__name__logwarningsrZpasslibrZ passlib.utilsrZpasslib.utils.compatr__all__r*rr rrrrr7rr#r(r-r2dictr5r3r:rrrrs<       3 !