o
    5h                     @   s   d Z ddlZddlmZ ddlmZmZmZmZmZ ddgZ	dZ
dZd	Zd
ZdZdZd a a aadd Zdd ZedZdd Zdd Zdd Zdd ZedddZdd Zdd Zd#d dZd#d!d"Z dS )$a  passlib.crypto.des -- DES block encryption routines

History
=======
These routines (which have since been drastically modified for python)
are based on a Java implementation of the des-crypt algorithm,
found at `<http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt>`_.

The copyright & license for that source is as follows::

    UnixCrypt.java 0.9 96/11/25
    Copyright (c) 1996 Aki Yoshida. All rights reserved.
    Permission to use, copy, modify and distribute this software
    for non-commercial or commercial purposes and without fee is
    hereby granted provided that this copyright notice appears in
    all copies.

    ---

    Unix crypt(3C) utility
    @version 0.9, 11/25/96
    @author  Aki Yoshida

    ---

    modified April 2001
    by Iris Van den Broeke, Daniel Deville

    ---
    Unix Crypt.
    Implements the one way cryptography used by Unix systems for
    simple password protection.
    @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
    @author Greg Wilkins (gregw)

The netbsd des-crypt implementation has some nice notes on how this all works -
    http://fxr.googlebit.com/source/lib/libcrypt/crypt.c?v=NETBSD-CURRENT
    N)exc)join_byte_valuesbyte_elem_valueiranger   	int_typesexpand_des_keydes_encrypt_blocki l   l    l   ~}{w l   l   sg c               	   C   sP   d} d}d}| |f||f||f||f||f||f||f||ffa dadadadS )z3delay loading tables until they are actually needed))r   r       r	       r
       r      r    $  r      r    $  r   )r                       l                     l                    l                   @ l         @  l        @ l        @  l         D l         D  l        D l        D  )r   r   @   r       r   @   r       r   @   r       r   @   r   )r                       @l         @          l         l          Hl         H            l           l          @ l         @ l           l          l          H l         H )r   r      r            r!          r"      r#     r$          r%         r&   )r                       l                   l         l          l                 l         l         l                 l        l         l        )r   r      r,      r-     r.      @r/      Pr0     @r1     Pr2   )r          @          l       @                    @    l            l       @                      @   l           l       @               l       @    l            l       @    )r   r      r:      r;     r<      r=     r>      r?     r@   )r              l               @l       @l       @l      @          l                  l        l        @ l       @ l       @ l      @ )r   r     @ rF      rG     @rH      rI    @ rJ     rK    @rL   )r                l                 l                l                    l           l           l          l           l          l          l         )r   r    @  rR      rS   @  rT      rU   @  rV      rW   @  rX   )r                     l                    l          l          l                   l         l         l        l          !l         !l         !l        !)r   r           r]           r^          r_      r`           ra           rb          rc   )r                   l                 l         l         l                 l       " l        l       "          l       "  l         l       " ))r   r   r   r   r   r   r   r   rG   rG   rG   rG      ri   ri   ri   )r   rI   r]   l       r	   i (  l        l    (   r,     l        l       i   i ( l        l    (   )r   r   r   r   r^   r^   r^   r^   r=   r=   r=   r=          rk   rk   rk   )r   r!   rd   l       rF   l       l       l      rA   l       l       l      i  @l      l      l     )r   r   r   r   r   r   r   r   r-   r-   r-   r-   @  rl   rl   rl   )r   rU   r   i  r:      i  i  r/   i  @i  @i @i  @i  @i @i @)r   r   r   r   r;   r;   r;   r;   r`   r`   r`   r`      rn   rn   rn   )r   r    r
   i   r#   r$   $   i$  rY   l       l        l       l       l      l   $    l   $   )r   rS   rM     rf   l         l         l        r\   l         l         l        l         l        l        l       )r   r'   r4   l          rC   l        @ l        `l        ` r              l            l           l        @   l        @  l        `   l        `  )r   rR   r   i @  rZ   l    @     l      @   l    @ @   r(   l    @     l      @   l    @ @   l         l    @    l      @  l    @ @  )r   r5   r7   r9   r              l         0 l         0  rg   l            l           l            l          l           l         0 l         0  )r   rN   r   l           r*   l                    l          r3   l       @         @    l       @   l       @l       @ l       @   l       @  )r   r[   rQ               rB   l          l           l           r   l          (l           l          ( l         l         (l          l         ( )r   r   r   l         @rO            l         @l        @r   r   l         @l        @l       l        l        @l       @)r   r)   re   l         r   l         @l        @ l        @rD   l         l         l        l         A l         Al        A l        A))r   r   r   r   r   r   r   r   r;   r;   r;   r;     rw   rw   rw   )r   r=   rR   i @ rd          l    @   l    @  r    i   i @ i @ l       l      l    @  l    @ )r   r   r   r   rS   rS   rS   rS   r/   r/   r/   r/     @ry   ry   ry   )r   r   rY   l      @ r:   i   l       l     @ r`   i   l        l      @ i  i  l       l     @ )r   r   r   r   r	   r	   r	   r	   r!   r!   r!   r!           rz   rz   rz   )r   rI   r^   l       r,   rj   l        l       r#   i  l       l      i  i l       l      )r   r   r   r   r-   r-   r-   r-   rU   rU   rU   rU     r{   r{   r{   )r   r   r]   l   @    r   r   l     @  l   @ @  rM   i@  l       l   @   i   i@  l     A  l   @ A  )r   rG   rA   i   r[   l          l          l          rQ   l           l           l           ru   l           l           l           )r   r   r(            r5   rr   l           l          r   l         P l         @l         Pl         @  l         P  l         @ l         P )r   rF   r
   i  @ rN   l         l          l         r   l         @l          @l         @l          Bl         Bl          Bl         B)r   r)   r*   r+   r   l           rs   l          r\   l          l         l                    l           l          l          )r   rB   re   l       r   l           l           l          r'   l         l         l        rq   l          l          l         )r   rO   rD   l         r4   l        !l          l        ! r   rv   l          l         l          l        ! l          l        ! )r   rg   r   l       0 rC   l        @l       @l       0@rf   rh   l         l       0          @ l        @ l       @ l       0@ )r   r   rZ            r3   l       @  l       @  l       @ r7   l          l         ( l         (r8   l       @  l       @ ( l       @ ())r     rp   i        l      l     l           l     l      l    l     l    l    l   )r                       l                  @l         Dl        @l        D         @  l         @ l         D  l         D l        @@ l        @D l        D@ l        DD )r   r-   rU   r{   r,   r.      r`            )r   rC   r3          @@r5           @   r6          @@   rf   r~          @         @@                    @         @          @@  )r   r	   r
   r   r               r               r               )r   rD   rB   rE   r                                   r\                             r}                                )r   @     @       l   D@        @i@@l   @     l   D@     @ iD@ l   @     l   DD     i D@iDD@l   @    l   DD    )r                   l                   l        "  l          l      "           l       l        l      l          l       " l         l      " ))@     @ " r   r        @ " r          "        r   r   r        @                @    r   r   r   r   r
        @           " r   r   r   r   r
   r   r        @ r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   )@   @      r             @    @                     @     r   r      @       r5   r                  @     r   r5   r,      @      r   r              r   r   r   r   r   r   r   r   r,   r   r   r   r   r   r   r   r   r   r   r   r   r,   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r,   r   r   r   r5   r   r   )@@       rf                r   r                   r        rU                 rU       r   r   r   rf   r   r   r   r   r   r          r   r   r   r   r   r   rf   rU   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rf   r   )@     @ @       @r       @ @r    @      @ @       @     @        @r-          @ @r   r   r   r-         @      @rC   r   r   r   r   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   rC   r-   r   r   r   r   r   r   r   r   r   r   r   r   r-   r   r   )@r   r3   r        @       @                  @       @ r3   r                    @         r        @ r   r   r   rt             r   r   r   r   r   r   r   rt   r   r   r   r   r   r   rt   r   r3   r   r   r   r   r   r   r   r   r   r   rt   r   r   r   r   r   r   r   r3   r   r   r   r   r   r   r   r   )@                @     r       @   r   rD          !  r            !      @  !        @   rp           r           @  !  r   r       @     r   rD   r   r   r   r   r   rp   r   r   r   r   r   r   rp   r   r   r   r   r   r   rp   rD   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   r   r   r   r   r   r   r   )@r   r\                @          @    !   @             r   r`      @      !   @      !         r           r   r   r   r   r   r   r   r   r   r      @        @    !r   r   r   r   r\   r`   r   r   r   r   r   r\   r`   r   r   r   r   r   r\   r   r   r   r   r   r   r   r   r   r   r`   r   r   r   r   r   r   r   r   )@       @                 @  r                  @                @  r   r	          r          @          @           @  r  rB   r  r  r  r  r  r   r  r	   r   r  r   r   rB   r   r   r   rB   r  r  r   r	   r   r  r   r  r  r  r   r   r  r  r  r   r  r  r  r   r  r  r   r   r  r   r	   r   rB   r  ))r   rB   rD   rE   r\   r   r   r   r   r   r   r   r}   r   r   r   )r   rY   r!   l       r   l         l         l        r(   l         l         l        r|   l        l        l       )r   r
   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   )r      i   i  i   i  i  i i   i  i  i i  i i i)r   r*   r   l         r)   r+   l         l        rQ   l           l           l          l           l          l          l         )r   rg   r4   l         r   l         @ l         @ l         @ r   l          @l          @l          @l         @@l         @@l         @@l         @@)r   r:   rM   i  r=   r>   i  i rd   l       l       l      rx   l      l      l     )r   rS   rI   i  r;   i  i  i rA   i  i  i i  i i i)r   r3   rC   r   rf   r   r~   r   r5   r6   r   r   r   r   r   r   )r   r^   r]   r_   rZ   l         l         l        r   l         l         l        r   l        l        l       )r   rU   r-   r{   r`   r   r   r   r,   r   r.   r   r   r   r   r   )r         i  i   i  i  i i   i  i  i i  i i i)r   rO   r'   l         rN   rP   l         l        r   l           rq   l          l           l          l          l         )r   r   re   l       r7   l          l          l         r[   l          l          l         l           l          l          l         )r   r   rR   i@@  rF   i@ @ i @@ i@@@ r/   i@  @i @ @i@@ @i  @@i@ @@i @@@i@@@@)r   r#   r   i  r    r$   i  i rG   i  i  i i  i i iN)PCXROTIE3264SPECF6464)PC1ROTPC2ROTAPC2ROTB r  f/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/crypto/des.py_load_tablesQ   s   	GDQ& 	r  c                 C   s*   d}|D ]}||| d@  O }| dL } q|S )zdReturns the permutation of the given 32-bit or 64-bit code with
    the specified permutation table.r      r#   r  )cpoutrr  r  r  _permute9  s
   
r  z>Qc                 C   s
   t | S )N_uint64_structpackvaluer  r  r  _pack64J  s   
r!  c                 C   s   t | d S )Nr   r  unpackr  r  r  r  	_unpack64M  s   r$  c                 C   s   t | dd  S )Nr
  r  r  r  r  r  _pack56P     r%  c                 C   s   t d|  d S )N    r   r"  r  r  r  r  	_unpack56S  r&  r(  1   ic                    s~   t  trt dkrtdn t  tr) dk s tkr!tdttt S t	
 ddt  t fddtD S )	zDconvert DES from 7 bytes to 8 bytes (by inserting empty parity bits)   zkey must be 7 bytes in sizer   z'key must be 56-bit non-negative integerbytes or intkeyc                 3   s     | ]} |? d @ d> V  qdS )   r
  Nr  ).0shiftr,  r  r  	<genexpr>u  s    z!expand_des_key.<locals>.<genexpr>)
isinstancebyteslen
ValueErrorr   INT_56_MASKr$  r   r%  r   ExpectedTypeErrorr(  r   _EXPAND_ITERr0  r  r0  r  r   c  s   

c                 C   s   t | trt| dkrtdttt| S t | tr)| dk s$| tkr(tdnt	
| dd| dL } d}d}|dk rP|| d	@ |> O }| dL } |d
7 }|dk s<|t @ rWJ |S )zGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)rS   zkey must be 8 bytes in sizer   'key must be 64-bit non-negative integerr+  r,  r
  8   r-  r*  )r2  r3  r4  r5  r%  shrink_des_keyr$  r   INT_64_MASKr   r7  )r,  resultoffsetr  r  r  r;  w  s&   

r;  r
  c                 C   s   t | trt| dkrt| } n
t| dkrtdt| } nt| ddt |tr:t|dkr5tdt|}nt|ddt| |||}t	|S )a  encrypt single block of data using DES, operates on 8-byte strings.

    :arg key:
        DES key as 7 byte string, or 8 byte string with parity bits
        (parity bit values are ignored).

    :arg input:
        plaintext block to encrypt, as 8 byte string.

    :arg salt:
        Optional 24-bit integer used to mutate the base DES algorithm in a
        manner specific to :class:`~passlib.hash.des_crypt` and its variants.
        The default value ``0`` provides the normal (unsalted) DES behavior.
        The salt functions as follows:
        if the ``i``'th bit of ``salt`` is set,
        bits ``i`` and ``i+24`` are swapped in the DES E-box output.

    :arg rounds:
        Optional number of rounds of to apply the DES key schedule.
        the default (``rounds=1``) provides the normal DES behavior,
        but :class:`~passlib.hash.des_crypt` and its variants use
        alternate rounds values.

    :raises TypeError: if any of the provided args are of the wrong type.
    :raises ValueError:
        if any of the input blocks are the wrong size,
        or the salt/rounds values are out of range.

    :returns:
        resulting 8-byte ciphertext block.
    r*  rS   zkey must be 7 or 8 bytesr3  r,  zinput block must be 8 bytesinput)
r2  r3  r4  r   r5  r$  r   r7  des_encrypt_int_blockr!  )r,  r?  saltroundsr=  r  r  r  r     s   
!



c                 C   s  |dk rt d|dk s|tkrt dt| ts t| dd| dk s(| tkr,t dt|ts8t|dd|dk s@|tkrDt d	td
u rKt  t	\}}}}}}	}
}dd }t
|| }|d@ d> |d@ d> B |d@ d? B |d@ d? B }|dkrd }}n |d? d@ |d@ B }t|t}|d? d@ |d? d@ B }t|t}|r]|d8 }|D ]\}}|d? |A |@ }|d> |A |A |A }|||d? d@  ||d? d@  A ||d? d@  A ||d? d@  A ||d? d@  A |	|d? d@  A |
|d? d@  A ||d? d@  A N }|d? |A |@ }|d> |A |A |A }|||d? d@  ||d? d@  A ||d? d@  A ||d? d@  A ||d? d@  A |	|d? d@  A |
|d? d@  A ||d? d@  A N }q||}}|s|d? d @ |d!> d"@ B |d#? d$@ B |d> d%@ B }t|tS )&a  encrypt single block of data using DES, operates on 64-bit integers.

    this function is essentially the same as :func:`des_encrypt_block`,
    except that it operates on integers, and will NOT automatically
    expand 56-bit keys if provided (since there's no way to detect them).

    :arg key:
        DES key as 64-bit integer (the parity bits are ignored).

    :arg input:
        input block as 64-bit integer

    :arg salt:
        optional 24-bit integer used to mutate the base DES algorithm.
        defaults to ``0`` (no mutation applied).

    :arg rounds:
        optional number of rounds of to apply the DES key schedule.
        defaults to ``1``.

    :raises TypeError: if any of the provided args are of the wrong type.
    :raises ValueError:
        if any of the input blocks are the wrong size,
        or the salt/rounds values are out of range.

    :returns:
        resulting ciphertext as 64-bit integer.
    r
  zrounds must be positive integerr   z(salt must be 24-bit non-negative integerintr,  r9  r?  z)input must be 64-bit non-negative integerNc                 s   s:    t D ]\}}t| |}t||} |t@ | t@ fV  qdS )zCgiven 64-bit key, iterates over the 8 (even,odd) key schedule pairsN)r  r  _KS_MASK)ks_oddp_evenp_oddks_evenr  r  r  _iter_key_schedule  s   

z1des_encrypt_int_block.<locals>._iter_key_schedule?      i     i  r	  i   rU      l   *UU iUUUUr
   :   2   *   "      
      l       <<xx!   l       C #   il   pa )r5  INT_24_MASKr2  r   r   r7  r<  r  r  r  listr  r  r  )r,  r?  rA  rB  SPE0SPE1SPE2SPE3SPE4SPE5SPE6SPE7rI  ks_listLRrH  rE  kBCr  r  r  r@    s   "








  





	r@  )r   r
  )!__doc__structpasslibr   passlib.utils.compatr   r   r   r   __all__rW  r6  r<  _KDATA_MASK_KPARITY_MASKrD  r  r  r  r  r  r  Structr  r!  r$  r%  r(  r8  r   r;  r   r@  r  r  r  r  <module>   s:    .
   k

8