a fWc_W@s$dZddlZddlZeeZddlmZddlm Z m Z m Z ddl m Z mZddlmZmZmZmZmZddlmZddlmmZgdZd Zd d Zd d ZddZddZ Gdddej!ej"ej#ej$Z%Gdddej"ej&ej#ej$Z'Gdddej#ej$Z(Gdddej!ej#ej$Z)dS)zFpasslib.handlers.des_crypt - traditional unix (DES) crypt and variantsN)warn) safe_crypt test_crypt to_unicode)h64h64big)byte_elem_valueu uascii_to_strunicodesuppress_cause)des_encrypt_int_block) des_crypt bsdi_cryptbigcryptcrypt16cCstddt|ddDS)zconvert secret to 64-bit DES key. this only uses the first 8 bytes of the secret, and discards the high 8th bit of each byte at that. a null parity bit is inserted after every 7th bit of the output. css*|]"\}}t|d@d|d>VqdS)9N)r).0icr>/usr/lib/python3.9/site-packages/passlib/handlers/des_crypt.py (sz'_crypt_secret_to_key..Nr)sum enumerate)secretrrr_crypt_secret_to_keys rcCsRt|}t|tr|d}t|vr2tjt t |}t |d|d}t |S)z pure-python backed for des_cryptutf-8r)r decode_int12 isinstancer encode_BNULLuhexcNullPasswordErrorrrr r encode_int64)rsalt salt_value key_valueresultrrr_raw_des_crypt+s    r.cCsLt|}d}t|}||krH|d}t|||}t|||A}|}q|S)z,convert secret to DES key used by bsdi_cryptr)rlenr )rr,idxendnextZ tmp_valuerrr_bsdi_secret_to_keyIsr3cCsRt|}t|tr|d}t|vr2tjt t |}t |d||}t |S)z"pure-python backend for bsdi_cryptr r)r decode_int24r#r r$r%r&r'r(rr3r rr))rroundsr*r+r,r-rrr_raw_bsdi_cryptUs    r6c@seZdZdZdZdZejZdZ dZ Z ejZ dZ eedejejBZeddZd d Zd d Zd ZeddZddZeddZddZdS)raThis class implements the des-crypt password hash, and follows the :ref:`password-hash-api`. It supports a fixed-length salt. The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: :type salt: str :param salt: Optional salt string. If not specified, one will be autogenerated (this is recommended). If specified, it must be 2 characters, drawn from the regexp range ``[./0-9A-Za-z]``. :param bool truncate_error: By default, des_crypt will silently truncate passwords larger than 8 bytes. Setting ``truncate_error=True`` will cause :meth:`~passlib.ifc.PasswordHash.hash` to raise a :exc:`~passlib.exc.PasswordTruncateError` instead. .. versionadded:: 1.7 :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 ``salt`` strings that are too long. .. versionadded:: 1.6 r*Ztruncate_error rzU ^ (?P[./a-z0-9]{2}) (?P[./a-z0-9]{11})? $cCs6t|dd}|dd|dd}}|||p0ddS)Nasciihashr9r*checksum)r)clsr;r*chkrrr from_strings zdes_crypt.from_stringcCstd|j|jf}t|SNz%s%sr r*r=r selfr;rrr to_stringszdes_crypt.to_stringcCs|jr||||S)N) use_defaults_check_truncate_policyZ_calc_checksum_backendrDrrrr_calc_checksums zdes_crypt._calc_checksumZos_cryptZbuiltincCs"tddr||jdSdSdS)NtestZ abgOeLfPimXQoTFr_set_calc_checksum_backend_calc_checksum_os_cryptr>rrr_load_backend_os_crypts  z des_crypt._load_backend_os_cryptcCsTt||j}|dur||S||jr6t|dkrHtj||j||ddS)N r9)rr*_calc_checksum_builtin startswithr/r&r'CryptBackendError)rDrr;rrrrNs   z!des_crypt._calc_checksum_os_cryptcCs||jdSNTrMrRrOrrr_load_backend_builtins zdes_crypt._load_backend_builtincCst||jddSNr:)r.r*r$decoderHrrrrRsz des_crypt._calc_checksum_builtinN)__name__ __module__ __qualname____doc__name setting_kwdsr& HASH64_CHARSchecksum_chars checksum_size min_salt_size max_salt_size salt_chars truncate_sizerecompiler XI _hash_regex classmethodr@rErIbackendsrPrNrWrRrrrrrps,$      rcseZdZdZdZdZdZejZ dZ Z ejZ dZ dZdZdZeed ejejBZed d Zd d ZdZefddZefddZfddZdZeddZddZ eddZ!ddZ"Z#S)ra This class implements the BSDi-Crypt password hash, and follows the :ref:`password-hash-api`. It supports a fixed-length salt, and a variable number of rounds. The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: :type salt: str :param salt: Optional salt string. If not specified, one will be autogenerated (this is recommended). If specified, it must be 4 characters, drawn from the regexp range ``[./0-9A-Za-z]``. :type rounds: int :param rounds: Optional number of rounds to use. Defaults to 5001, must be between 1 and 16777215, inclusive. :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. .. versionadded:: 1.6 .. versionchanged:: 1.6 :meth:`hash` will now issue a warning if an even number of rounds is used (see :ref:`bsdi-crypt-security-issues` regarding weak DES keys). )r*r5r8iiZlinearz ^ _ (?P[./a-z0-9]{4}) (?P[./a-z0-9]{4}) (?P[./a-z0-9]{11})? $cCsVt|dd}|j|}|s(tj||ddd\}}}|t| d||dS)Nr:r;r5r*r?)r5r*r=) rrkmatchr&r'InvalidHashErrorgrouprr4r$)r>r;mr5r*r?rrrr@4s   zbsdi_crypt.from_stringcCs,tdt|jd|j|jf}t|S)Nz_%s%s%sr:)r rZ encode_int24r5rYr*r=r rCrrrrEAszbsdi_crypt.to_stringTc s2tt|jfi|}|jd@s.tdtjj|S)NrozHbsdi_crypt rounds should be odd, as even rounds may reveal weak DES keys)superrusingdefault_roundsrr&r'ZPasslibSecurityWarning)r>kwdssubcls __class__rrruNs  zbsdi_crypt.usingcstt|}|dBS)Nro)rtr_generate_rounds)r>r5ryrrr{Wszbsdi_crypt._generate_roundsc s$|jd@sdStt|jfi|S)NroT)r5rtr_calc_needs_update)rDrwryrrr|es zbsdi_crypt._calc_needs_updaterJcCs"tddr||jdSdSdS)NrKz_/...lLDAxARksGCHin.TFrLrOrrrrPts  z!bsdi_crypt._load_backend_os_cryptcCs^|}t||}|dur$||S||ddrBt|dkrRtj||||ddS)N )rErrRrSr/r&r'rT)rDrZconfigr;rrrrN|s  z"bsdi_crypt._calc_checksum_os_cryptcCs||jdSrUrVrOrrrrWs z bsdi_crypt._load_backend_builtincCst||j|jddSrX)r6r5r*r$rYrHrrrrRsz!bsdi_crypt._calc_checksum_builtin)$rZr[r\r]r^r_rbr&r`rarcrdrervZ min_roundsZ max_roundsZ rounds_costrgrhr rirjrkrlr@rEZ_avoid_even_roundsrur{r|rmrPrNrWrR __classcell__rrryrrs<#       rcsreZdZdZdZdZejZdZ Z ejZ e ede je jBZeddZddZdfd d Zd d ZZS)ragThis class implements the BigCrypt password hash, and follows the :ref:`password-hash-api`. It supports a fixed-length salt. The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: :type salt: str :param salt: Optional salt string. If not specified, one will be autogenerated (this is recommended). If specified, it must be 22 characters, drawn from the regexp range ``[./0-9A-Za-z]``. :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 ``salt`` strings that are too long. .. versionadded:: 1.6 )r*r9zX ^ (?P[./a-z0-9]{2}) (?P([./a-z0-9]{11})+)? $cCsDt|dd}|j|}|s(tj||dd\}}|||dSNr:r;r*r?r<rrkrpr&r'rqrrr>r;rsr*r?rrrr@s    zbigcrypt.from_stringcCstd|j|jf}t|SrArBrCrrrrEszbigcrypt.to_stringFcs0tt|j||d}t|dr,tj||S)N)relaxedr8)rtr_norm_checksumr/r&r'rq)rDr=rryrrrs  zbigcrypt._norm_checksumcCspt|tr|d}t||jd}d}t|}||krf|d}|t||||dd7}|}q2|dS)Nr r:rri)r#r r$r.r*r/rY)rDrr?r0r1r2rrrrIs  zbigcrypt._calc_checksum)F)rZr[r\r]r^r_r&r`rarcrdrergrhr rirjrkrlr@rErrIrrrryrrs    rc@sheZdZdZdZdZdZejZ dZ Z ejZ dZ eedejejBZeddZd d Zd d Zd S)raThis class implements the crypt16 password hash, and follows the :ref:`password-hash-api`. It supports a fixed-length salt. The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords: :type salt: str :param salt: Optional salt string. If not specified, one will be autogenerated (this is recommended). If specified, it must be 2 characters, drawn from the regexp range ``[./0-9A-Za-z]``. :param bool truncate_error: By default, crypt16 will silently truncate passwords larger than 16 bytes. Setting ``truncate_error=True`` will cause :meth:`~passlib.ifc.PasswordHash.hash` to raise a :exc:`~passlib.exc.PasswordTruncateError` instead. .. versionadded:: 1.7 :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 ``salt`` strings that are too long. .. versionadded:: 1.6 r7r9zU ^ (?P[./a-z0-9]{2}) (?P[./a-z0-9]{22})? $cCsDt|dd}|j|}|s(tj||dd\}}|||dSrrrrrrr@+s    zcrypt16.from_stringcCstd|j|jf}t|SrArBrCrrrrE4szcrypt16.to_stringcCst|tr|d}|jr$||zt|jd}WntyXt tdYn0t |}t |d|d}t |dd}t |d|d}t |t |}|dS) Nr r:zinvalid chars in saltrr~rr)r#r r$rFrGrr"r* ValueErrorr rr rr)rY)rDrr+Zkey1Zresult1Zkey2Zresult2r?rrrrI;s    zcrypt16._calc_checksumN)rZr[r\r]r^r_rbr&r`rarcrdrerfrgrhr rirjrkrlr@rErIrrrrrs$   r)*r]rgZloggingZ getLoggerrZlogwarningsrZ passlib.utilsrrrZpasslib.utils.binaryrrZpasslib.utils.compatrr r r r Zpasslib.crypto.desr Zpasslib.utils.handlersZutilshandlersr&__all__r%rr.r3r6Z TruncateMixinZHasManyBackendsZHasSaltZGenericHandlerrZ HasRoundsrrrrrrrs(    $S