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 rcCspt|dksJt|}t|tr.|d}t|ts[./a-z0-9]{2}) (?P[./a-z0-9]{11})? $cCs6t|dd}|dd|dd}}|||p0ddS)Nasciihashr r-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_backendrErrrr_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 r )rr-_calc_checksum_builtin startswithr#r)r*CryptBackendError)rErr<rrrrOs   z!des_crypt._calc_checksum_os_cryptcCs||jdSNTrNrSrPrrr_load_backend_builtins zdes_crypt._load_backend_builtincCst||jddSNr;)r1r-r&decoderIrrrrSsz 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 classmethodrArFrJbackendsrQrOrXrSrrrrrps,$      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-r7r:iiZlinearz ^ _ (?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<r7r-r@)r7r-r>) rrlmatchr)r*InvalidHashErrorgrouprr6r&)r?r<mr7r-r@rrrrA4s   zbsdi_crypt.from_stringcCs,tdt|jd|j|jf}t|S)Nz_%s%s%sr;)r rZ encode_int24r7rZr-r>r rDrrrrFAszbsdi_crypt.to_stringTc s2tt|jfi|}|jd@s.tdtjj|S)NrpzHbsdi_crypt rounds should be odd, as even rounds may reveal weak DES keys)superrusingdefault_roundsrr)r*ZPasslibSecurityWarning)r?kwdssubcls __class__rrrvNs  zbsdi_crypt.usingcstt|}|dBS)Nrp)rur_generate_rounds)r?r7rzrrr|Wszbsdi_crypt._generate_roundsc s$|jd@sdStt|jfi|S)NrpT)r7rur_calc_needs_update)rErxrzrrr}es zbsdi_crypt._calc_needs_updaterKcCs"tddr||jdSdSdS)NrLz_/...lLDAxARksGCHin.TFrMrPrrrrQts  z!bsdi_crypt._load_backend_os_cryptcCs^|}t||}|dur$||S||ddrBt|dkrRtj||||ddS)N )rFrrSrTr#r)r*rU)rErZconfigr<rrrrO|s  z"bsdi_crypt._calc_checksum_os_cryptcCs||jdSrVrWrPrrrrXs z bsdi_crypt._load_backend_builtincCst||j|jddSrY)r8r7r-r&rZrIrrrrSsz!bsdi_crypt._calc_checksum_builtin)$r[r\r]r^r_r`rcr)rarbrdrerfrwZ min_roundsZ max_roundsZ rounds_costrhrir rjrkrlrmrArFZ_avoid_even_roundsrvr|r}rnrQrOrXrS __classcell__rrrzrrs<#       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-r zX ^ (?P[./a-z0-9]{2}) (?P([./a-z0-9]{11})+)? $cCsDt|dd}|j|}|s(tj||dd\}}|||dSNr;r<r-r@r=rrlrqr)r*rrrsr?r<rtr-r@rrrrAs    zbigcrypt.from_stringcCstd|j|jf}t|SrBrCrDrrrrFszbigcrypt.to_stringFcs0tt|j||d}t|dr,tj||S)N)relaxedr:)rur_norm_checksumr#r)r*rr)rEr>rrzrrrs  zbigcrypt._norm_checksumcCspt|tr|d}t||jd}d}t|}||krf|d}|t||||dd7}|}q2|dS)Nr!r;rri)r%r r&r1r-r#rZ)rErr@r2r3r4rrrrJs  zbigcrypt._calc_checksum)F)r[r\r]r^r_r`r)rarbrdrerfrhrir rjrkrlrmrArFrrJrrrrzrrs    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 r9r zU ^ (?P[./a-z0-9]{2}) (?P[./a-z0-9]{22})? $cCsDt|dd}|j|}|s(tj||dd\}}|||dSrrrrrrrA+s    zcrypt16.from_stringcCstd|j|jf}t|SrBrCrDrrrrF4szcrypt16.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 saltrrrr)r%r r&rGrHrr$r- ValueErrorr rr rr,rZ)rErr.Zkey1Zresult1Zkey2Zresult2r@rrrrJ;s    zcrypt16._calc_checksumN)r[r\r]r^r_r`rcr)rarbrdrerfrgrhrir rjrkrlrmrArFrJrrrrrs$   r)*r^rhZloggingZ getLoggerr[logwarningsrZ passlib.utilsrrrZpasslib.utils.binaryrrZpasslib.utils.compatrr r r r Zpasslib.crypto.desr Zpasslib.utils.handlersZutilshandlersr)__all__r(rr1r5r8Z TruncateMixinZHasManyBackendsZHasSaltZGenericHandlerrZ HasRoundsrrrrrrrs(    $S