Viewing File: /usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyc

ó
èŽXc@ÀsñdZddlmZmZddlZejeƒZddlZddl	Z	ddl
mZeZ
eaddlmZddlmZddlmZddlmZmZdd	lmZmZmZddljjZd
gZ yddl!Z
Wne"k
reZ
nXe#e
deƒZ$e$rBe$ƒZ%e
j&j'Z(ndfd
„ƒYZ%dZ(dej)ej*ej+ej,ej-ej.fd„ƒYZ/de/fd„ƒYZ0de/fd„ƒYZ1de/fd„ƒYZ2d
e0e/fd„ƒYZ!dS(sápasslib.handlers.argon2 -- argon2 password hash wrapper

References
==========
* argon2
    - home: https://github.com/P-H-C/phc-winner-argon2
    - whitepaper: https://github.com/P-H-C/phc-winner-argon2/blob/master/argon2-specs.pdf
* argon2 cffi wrapper
    - pypi: https://pypi.python.org/pypi/argon2_cffi
    - home: https://github.com/hynek/argon2_cffi
* argon2 pure python
    - pypi: https://pypi.python.org/pypi/argon2pure
    - home: https://github.com/bwesterb/argon2pure
i(twith_statementtabsolute_importN(twarn(texc(t
MAX_UINT32(tto_bytes(tb64s_encodetb64s_decode(tutunicodet
bascii_to_strtargon2tPasswordHashert_default_settingscBÀs,eZdZdZdZdZdZdZRS(s†
        dummy object to use as source of defaults when argon2 mod not present.
        synced w/ argon2 16.1 as of 2016-6-16
        iii(t__name__t
__module__t__doc__t	time_costtmemory_costtparallelismtsalt_lenthash_len(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR
:sit
_Argon2Commonc	BÀs—eZdZdZd ZedƒZejZ	edƒed
ƒfZ
ejZdZ
eZejZdZeZdZd"ZeZd#ZdZdZeZejZeZej Z eZ!d#Z"e#d#d#d#d#d#d#d#d„ƒZ$e#d„ƒZ%e#d„ƒZ&e'j(de'j)ƒZ*e#d„ƒZ+d„Z,ed#d#d#d„Z-e#d„ƒZ.e#ed„ƒZ/d„Z0dZ1e#d„ƒZ2e#d#d#d„ƒZ3RS($s&
    Base class which implements brunt of Argon2 code.
    This is then subclassed by the various backends,
    to override w/ backend-specific methods.

    When a backend is loaded, the bases of the 'argon2' class proper
    are modified to prepend the correct backend-specific subclass.
    Rtsaltt	salt_sizeRtroundsRRRtdigest_sizeRs$argon2is	$argon2i$s	$argon2d$iitlineariiÿÿÿÿcKÀs|dk	r4d|kr'tdƒ‚n||d<n|dk	rhd|kr[tdƒ‚n||d<n|dk	r˜|dk	rtdƒ‚n|}n|dk	rÈ|dk	r¿tdƒ‚n|}ntt|ƒj|}	|jdƒ}
|dk	rLt|tjƒrt	|ƒ}ntj
|	|dd	d
tddd|
ƒ|	_n|dk	r”t|tjƒryt	|ƒ}n|	j
|d|
ƒ|	_n|	j|	j|	jƒ|dk	rt|tjƒr×t	|ƒ}n|d
kr|dkrtd|fƒ‚n||	_n|	S(NRs/'time_cost' and 'rounds' are mutually exclusiveRs1'salt_len' and 'salt_size' are mutually exclusives3'hash_len' and 'digest_size' are mutually exclusives8'checksum_size' and 'digest_size' are mutually exclusivetrelaxedtminitmaxtparamRiiÿÿÿÿs7max_threads (%d) must be -1 (unlimited), or at least 1.(tNonet	TypeErrortsuperRtusingtgett
isinstancetuhtnative_string_typestinttnorm_integerRt
checksum_sizet_norm_memory_costRt_validate_constraintsRt
ValueErrortmax_threads(tclsRRRRR*RR.tkwdstsubclsR((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR#ºsH

		cCÀs<d|}||kr8td|j|||fƒ‚ndS(NisO%s: memory_cost (%d) is too low, must be at least 8 * parallelism (8 * %d = %d)(R-tname(R/RRtmin_memory_cost((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR,òs

	cCÀstj|ƒ}|j|jƒS(N(R&tto_unicode_for_identifyt
startswithtident_values(R/thash((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pytidentifyss÷
        ^
        \$argon2(?P<type>[id])\$
        (?:
            v=(?P<version>\d+)
            \$
        )?
        m=(?P<memory_cost>\d+)
        ,
        t=(?P<time_cost>\d+)
        ,
        p=(?P<parallelism>\d+)
        (?:
            ,keyid=(?P<keyid>[^,$]+)
        )?
        (?:
            ,data=(?P<data>[^,$]+)
        )?
        (?:
            \$
            (?P<salt>[^$]+)
            (?:
                \$
                (?P<digest>.+)
            )?
        )?
        $
    cCÀs|t|tƒr!|jdƒ}nt|tƒsEtj|dƒ‚n|jj|ƒ}|sotj|ƒ‚n|j	ddddddd	d
dƒ	\	}}}}}}}	}
}|dksÐt
d|fƒ‚|råtdƒ‚n|d|d
kd|r	t|ƒnddt|ƒdt|ƒdt|ƒd
|
rEt
|
ƒndd	|	r]t
|	ƒndd|rut
|ƒndƒS(Nsutf-8R7ttypetversionRRRtkeyidtdataRtdigesttitdsunexpected type code: %rs&argon2 'keyid' parameter not supportedttype_diRtchecksum(R>R?(R%R	tencodetbytesRtExpectedStringErrort_hash_regextmatchtMalformedHashErrortgrouptAssertionErrortNotImplementedErrorR(RR (R/R7tmR9R:RRRR;R<RR=((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pytfrom_string8s*-cCÀs¶t|j|jƒ}|j}|dkr4d}n
d|}|j}|ridtt|jƒƒ}nd}d|||j|j|j	|tt|j
ƒƒtt|jƒƒfS(Nitsv=%d$s,data=s%s%sm=%d,t=%d,p=%d%s$%s$%s(tstrR6R@R:R<R
RRRRRRA(tselftidentR:tvstrR<tkdstr((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyt	to_stringSs		
	cKÀs7|jdƒ}|dk	r-t|ƒ|_ntt|ƒj|||_|dkr‚tj	||j
|jddƒs”t‚n|j|ƒ|_
|dkrÊtj	||j
|jddƒsÜt‚n|j|ƒ|_
|dkr|jdks3t‚n3t|tƒs*tjj|ddƒ‚n||_dS(NRARR:RRCR<(R$R tlenR*R"Rt__init__R@R&tvalidate_default_valueR:t
_norm_versionRIRR+R<R%RCRtExpectedTypeError(ROR@R:RR<R0RA((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRUis$	cCÀsŸt|tjƒs-tjj|ddƒ‚n|dkr[|dkr[td|fƒ‚n|jƒ}||jkr›td|j|||jfƒ‚n|S(NtintegerR:iisinvalid argon2 hash version: %dsk%s: hash version 0x%X not supported by %r backend (max version is 0x%X); try updating or switching backends(	R%R&t	int_typesRRXR-tget_backendtmax_versionR2(R/R:tbackend((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRW’sc	CÀs%tj||d|jddd|ƒS(NRRRR(R&R)R3(R/RR((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR+£scKÀsžt|ƒ}|jrtS|j}|dks=||jkrI|j}n|j|kr\tS|j|jkrrtS|j|jkrˆtSt	t
|ƒj|S(N(R9R@tTruetmin_desired_versionR R\R:RR*R"Rt_calc_needs_update(ROR0R/tminver((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR`²s		s> -- recommend you install one (e.g. 'pip install argon2_cffi')cCÀsT|j}t|tƒr$|dks*t‚|dkrPtd|tjjƒntS(sØ
        helper called by from backend mixin classes' _load_backend_mixin() --
        invoked after backend imports have been loaded, and performs
        feature detection & testing common to all backends.
        iis6%r doesn't support argon2 v1.3, and should be upgraded(	R\R%R(RIRR&RtPasslibSecurityWarningR^(t	mixin_clsR2tdryrunR\((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyt_finalize_backend_mixinÉs	!
cCÀsØ|jƒ}|dkr6|dk	r6|j|ƒ}n|dk	r…|j|j|jƒ|dkr…|jdk	r…tdƒ‚q…nt|ƒ}|dkr³d|||f}nt	|ƒ}t
j|d|ƒ‚dS(s}
        internal helper invoked when backend has hash/verification error;
        used to adapt to passlib message.
        targon2_cffis8argon2_cffi backend doesn't support the 'data' parametersDecoding faileds%s reported: %s: hash=%rtreasonN(sDecoding failed(R[R RLR,RRR<RJRNtreprRRG(R/terrR7ROR]ttextRg((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyt_adapt_backend_error×s	(	ssalts	salt_sizessalt_lensroundss	time_costsmemory_costsparallelismsdigest_sizeshash_leniiÿÿÿN(4RRRR2tsetting_kwdsRRPR
RR*R6Rtdefault_salt_sizet
min_salt_sizeRt
max_salt_sizeRtdefault_roundst
min_roundst
max_roundstrounds_costtmax_parallelismt_default_versionR\R R_R3R.tFalsetpure_use_threadsRR:RR@R<tclassmethodR#R,R8tretcompiletXRERLRSRURWR+R`t_no_backend_suggestionReRk(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRIsd
						6	)	t
_NoBackendcBÀs\eZdZed„ƒZed„ƒZejddddƒed„ƒƒZd„Z	RS(	s€
    mixin used before any backend has been loaded.
    contains stubs that force loading of one of the available backends.
    cCÀs|jƒ|j|ƒS(N(t_stub_requires_backendR7(R/tsecret((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR7s
cCÀs|jƒ|j||ƒS(N(R~tverify(R/RR7((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR€s
t
deprecateds1.7tremoveds2.0cCÀs|jƒ|j||ƒS(N(R~tgenhash(R/Rtconfig((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRƒs
cCÀs |jƒtt|ƒj|ƒS(N(R~R"Rt_calc_checksum(ROR((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR…s
(
RRRRxR7R€R&tdeprecated_methodRƒR…(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR}þst_CffiBackendcBÀsSeZdZed„ƒZed„ƒZed„ƒZed„ƒZd„ZRS(s
    argon2_cffi backend
    cCÀsRtdkrtStjj}tjdtj|ƒ||_|_	|j
||ƒS(NsOdetected 'argon2_cffi' backend, version %r, with support for 0x%x argon2 hashes(t_argon2_cffiR Rvt	low_leveltARGON2_VERSIONtlogtdebugt__version__R:R\Re(RcR2RdR\((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyt_load_backend_mixin0s	
cCÀs­tj|ƒt|dƒ}ybttjjdtjjjd|j	d|j
d|jdt|jƒƒd|j
d|ƒƒSWn(tjjk
r¨}|j|ƒ‚nXdS(	Nsutf-8R9RRRRRR(R&tvalidate_secretRR
RˆR‰thash_secrettTypetIRRpRt_generate_saltR*t
exceptionstHashingErrorRk(R/RRi((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR7>s
				cCÀsÙtj|ƒt|dƒ}t|dƒ}|jdƒrLtjjj}ntjjj}y2tjj	|||ƒ}|t
ksˆt‚t
SWnEtjj
k
r§tStjjk
rÔ}|j|d|ƒ‚nXdS(Nsutf-8tasciis	$argon2d$R7(R&RRR5RˆR‰R‘tDR’t
verify_secretR^RIR”tVerifyMismatchErrorRvtVerificationErrorRk(R/RR7R9tresultRi((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR€Qs
cCÀstj|ƒt|dƒ}|j|ƒ}|jrFtjjj}ntjjj	}yat
tjjd|d|jd|j
d|jdt|jƒd|jd|d	|jƒƒ}Wn.tjjk
ræ}|j|d
|ƒ‚nX|jdkr|jdd
ƒ}n|S(Nsutf-8R9RRRRRRR:R7is$v=16$t$(R&RRRLR@RˆR‰R‘R—R’R
RRRRRR*R:R”R•Rktreplace(R/RR„ROR9R›Ri((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRƒfs*
					cCÀstdƒ‚dS(Ns-shouldn't be called under argon2_cffi backend(RI(ROR((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR…†s(	RRRRxRŽR7R€RƒR…(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR‡(s t_PureBackendcBÀs&eZdZed„ƒZd„ZRS(s
    argon2pure backend
    cCÀs¤yddlaWntk
r$tSXyddlm}Wntk
rZtjdƒtSXtjd|ƒ|s„tdt	j
ƒn||_|_|j
||ƒS(Ni(tARGON2_DEFAULT_VERSIONs\detected 'argon2pure' backend, but package is too old (passlib requires argon2pure >= 1.2.3)sBdetected 'argon2pure' backend, with support for 0x%x argon2 hashess¦Using argon2pure backend, which is 100x+ slower than is required for adequate security. Installing argon2_cffi (via 'pip install argon2_cffi') is strongly recommended(t
argon2puret_argon2puretImportErrorRvRŸR‹twarningRŒRRRbR:R\Re(RcR2RdR\((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRŽ˜s 


	
cCÀstj|ƒt|dƒ}|jr1tj}n	tj}td|d|jd|j	d|j
d|jd|jd|d	|j
ƒ}|jd
kr¤|j|d<n|jrºt|d<n|jrÓ|j|d
<nytj|SWn+tjk
r}|j|d|ƒ‚nXdS(Nsutf-8tpasswordRRRRt
tag_lengtht	type_codeR:itthreadstuse_threadstassociated_dataRO(R&RRR@R¡tARGON2DtARGON2ItdictRRRRR*R:R.RwR^R<RtArgon2ErrorRk(RORR9R0Ri((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyR…½s0
								
	(RRRRxRŽR…(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRžs%cBÀs5eZdZdZeZied6ed6e	d6Z
RS(s[
    This class implements the Argon2 password hash [#argon2-home]_, and follows the :ref:`password-hash-api`.
    (This class only supports generating "Type I" argon2 hashes).

    Argon2 supports a variable-length salt, and variable time & memory cost,
    and a number of other configurable parameters.

    The :meth:`~passlib.ifc.PasswordHash.replace` method accepts the following optional keywords:

    :type salt: str
    :param salt:
        Optional salt string.
        If specified, the length must be between 0-1024 bytes.
        If not specified, one will be auto-generated (this is recommended).

    :type salt_size: int
    :param salt_size:
        Optional number of bytes to use when autogenerating new salts.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        This corresponds linearly to the amount of time hashing will take.

    :type time_cost: int
    :param time_cost:
        An alias for **rounds**, for compatibility with underlying argon2 library.

    :param int memory_cost:
        Defines the memory usage in kibibytes.
        This corresponds linearly to the amount of memory hashing will take.

    :param int parallelism:
        Defines the parallelization factor.
        *NOTE: this will affect the resulting hash value.*

    :param int digest_size:
        Length of the digest in bytes.

    :param int max_threads:
        Maximum number of threads that will be used.
        -1 means unlimited; otherwise hashing will use ``min(parallelism, max_threads)`` threads.

        .. note::

            This option is currently only honored by the argon2pure backend.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

    .. todo::

        * Support configurable threading limits.
    RfR (sargon2_cffis
argon2pureN(RRRtbackendsR^t_backend_mixin_targetR}R R‡Ržt_backend_mixin_map(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyRás;(3Rt
__future__RRtloggingt	getLoggerRR‹RyttypestwarningsRR RˆR¡tpasslibRtpasslib.crypto.digestRt
passlib.utilsRtpasslib.utils.binaryRRtpasslib.utils.compatRR	R
tpasslib.utils.handlerstutilsthandlersR&t__all__RR¢tgetattrt_PasswordHasherR
R‰RŠRutSubclassBackendMixintParallelismMixint	HasRoundst
HasRawSalttHasRawChecksumtGenericHandlerRR}R‡Rž(((s;/usr/lib/python2.7/site-packages/passlib/handlers/argon2.pyt<module>sB	

	
ÿ´*hQ
Back to Directory File Manager