
     h                         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xaxaxaad Zd Z ej        d          Zd Zd Zd Zd Z eddd          Zd Zd ZddZ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            	      R    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/     r0      @r1      Pr2     @r3     Pr4   )r          @          l       @                    @    l            l       @                      @   l           l       @               l       @    l            l       @    )r   r      r<      r=     r>      r?     r@      rA     rB   )r              l               @l       @l       @l      @          l                  l        l        @ l       @ l       @ l      @ )r   r     @ rH      rI     @rJ      rK    @ rL     rM    @rN   )r                l                 l                l                    l           l           l          l           l          l          l         )r   r    @  rT      rU   @  rV      rW   @  rX      rY   @  rZ   )r                     l                    l          l          l                   l         l         l        l          !l         !l         !l        !)r   r           r_           r`          ra      rb           rc           rd          re   )r                   l                 l         l         l                 l       " l        l       "          l       "  l         l       " ))r   r   r   r   r   r   r   r   rI   rI   rI   rI      rk   rk   rk   )r   rK   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?          rm   rm   rm   )r   r#   rf   l       rH   l       l       l      rC   l       l       l      i  @l      l      l     )r   r   r   r   r   r   r   r   r/   r/   r/   r/   @  rn   rn   rn   )r   rW   r   i  r<      i  i  r1   i  @i  @i @i  @i  @i @i @)r   r   r   r   r=   r=   r=   r=   rb   rb   rb   rb      rp   rp   rp   )r   r"   r   i   r%   r&   $   i$  r[   l       l        l       l       l      l   $    l   $   )r   rU   rO     rh   l         l         l        r^   l         l         l        l         l        l        l       )r   r)   r6   l          rE   l        @ l        `l        ` r!              l            l           l        @   l        @  l        `   l        `  )r   rT   r   i @  r\   l    @     l      @   l    @ @   r*   l    @     l      @   l    @ @   l         l    @    l      @  l    @ @  )r   r7   r9   r;   r              l         0 l         0  ri   l            l           l            l          l           l         0 l         0  )r   rP   r   l           r,   l                    l          r5   l       @         @    l       @   l       @l       @ l       @   l       @  )r   r]   rS               rD   l          l           l           r    l          (l           l          ( l         l         (l          l         ( )r   r   r   l         @rQ            l         @l        @r   r   l         @l        @l       l        l        @l       @)r   r+   rg   l         r   l         @l        @ l        @rF   l         l         l        l         A l         Al        A l        A))r   r   r   r   r   r   r   r   r=   r=   r=   r=     ry   ry   ry   )r   r?   rT   i @ rf          l    @   l    @  r"   i   i @ i @ l       l      l    @  l    @ )r   r   r   r   rU   rU   rU   rU   r1   r1   r1   r1     @r{   r{   r{   )r   r   r[   l      @ r<   i   l       l     @ rb   i   l        l      @ i  i  l       l     @ )r   r   r   r   r   r   r   r   r#   r#   r#   r#           r|   r|   r|   )r   rK   r`   l       r.   rl   l        l       r%   i  l       l      i  i l       l      )r   r   r   r   r/   r/   r/   r/   rW   rW   rW   rW     r}   r}   r}   )r   r   r_   l   @    r   r   l     @  l   @ @  rO   i@  l       l   @   i   i@  l     A  l   @ A  )r   rI   rC   i   r]   l          l          l          rS   l           l           l           rw   l           l           l           )r   r   r*            r7   rt   l           l          r   l         P l         @l         Pl         @  l         P  l         @ l         P )r   rH   r   i  @ rP   l         l          l         r   l         @l          @l         @l          Bl         Bl          Bl         B)r   r+   r,   r-   r   l           ru   l          r^   l          l         l                    l           l          l          )r   rD   rg   l       r!   l           l           l          r)   l         l         l        rs   l          l          l         )r   rQ   rF   l         r6   l        !l          l        ! r   rx   l          l         l          l        ! l          l        ! )r   ri   r   l       0 rE   l        @l       @l       0@rh   rj   l         l       0          @ l        @ l       @ l       0@ )r   r    r\            r5   l       @  l       @  l       @ r9   l          l         ( l         (r:   l       @  l       @ ( l       @ ())r     rr   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/   rW   r}   r.   r0      rb            )r   rE   r5          @@r7           @   r8          @@   rh   r          @         @@                    @         @          @@  )r   r   r   r   r               r               r               )r   rF   rD   rG   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      @       r7   r                  @     r   r7   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   r7   r   r   r   r   r.   r   r   r   r7   r   r   )@@       rh                r   r                   r        rW                 rW       r   r   r   rh   r   r   r   r   r   r          r   r   r   r   r   r   rh   rW   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r   )@     @ @       @r       @ @r    @      @ @       @     @        @r/          @ @r   r   r   r/         @      @rE   r   r   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r   r   r   r   rE   r/   r   r   r   r   r   r   r   r   r   r   r   r   r/   r   r   )@r   r5   r        @       @                  @       @ r5   r                    @         r        @ r   r   r   rv             r   r   r   r   r   r   r   rv   r   r   r   r   r   r   rv   r   r5   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   )@                @     r       @   r   rF          !  r            !      @  !        @   rr           r           @  !  r   r       @     r   rF   r   r   r   r   r   rr   r   r   r   r   r   r   rr   r   r   r   r   r   r   rr   rF   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   r   r   r   r   r   r   )@r   r^                @          @    !   @             r   rb      @      !   @      !         r           r   r   r   r   r   r   r   r   r   r      @        @    !r   r   r   r   r^   rb   r   r   r   r   r   r^   rb   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         @          @           @  r  rD   r  r  r  r  r
  r   r  r   r   r	  r   r   rD   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   rD   r  ))r   rD   rF   rG   r^   r   r   r   r   r   r   r   r   r   r   r   )r   r[   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        rS   l           l           l          l           l          l          l         )r   ri   r6   l         r   l         @ l         @ l         @ r   l          @l          @l          @l         @@l         @@l         @@l         @@)r   r<   rO   i  r?   r@   i  i rf   l       l       l      rz   l      l      l     )r   rU   rK   i  r=   i  i  i rC   i  i  i i  i i i)r   r5   rE   r   rh   r   r   r   r7   r8   r   r   r   r   r   r   )r   r`   r_   ra   r\   l         l         l        r    l         l         l        r   l        l        l       )r   rW   r/   r}   rb   r   r   r   r.   r   r0   r   r   r   r   r   )r         i  i   i  i  i i   i  i  i i  i i i)r   rQ   r)   l         rP   rR   l         l        r!   l           rs   l          l           l          l          l         )r   r   rg   l       r9   l          l          l         r]   l          l          l         l           l          l          l         )r   r   rT   i@@  rH   i@ @ i @@ i@@@ r1   i@  @i @ @i@@ @i  @@i@ @@i @@@i@@@@)r   r%   r   i  r"   r&   i  i rI   i  i  i i  i i iN)PCXROTIE3264SPECF6464)PC1ROTPC2ROTAPC2ROTBs      N/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/passlib/crypto/des.py_load_tablesr  Q   s    A
FNA
GHA
Gb g' 2g' 2g' 2g' 2	F!
FLA	
CPAFFF    c                 :    d}|D ]}||| dz           z  }| dz  } |S )zdReturns the permutation of the given 32-bit or 64-bit code with
    the specified permutation table.r      r%    )cpoutrs       r  _permuter  9  s9    
 C  q3x	aJr  z>Qc                 6    t                               |           S )N_uint64_structpackvalues    r  _pack64r&  J  s    u%%%r  c                 B    t                               |           d         S )Nr   r"  unpackr$  s    r  	_unpack64r*  M  s      ''**r  c                 F    t                               |           dd          S )Nr  r!  r$  s    r  _pack56r,  P  s    u%%abb))r  c                 H    t                               d| z             d         S )N    r   r(  r$  s    r  	_unpack56r/  S  s      511!44r  1   ic                     t           t                    r#t                     dk    rt          d          ntt           t                    rI dk     s t
          k    rt          d          t          t          t                                         S t          j
         dd          t                      t           f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   .   K   | ]}|z	  d z  dz  V  dS )   r  Nr  ).0shiftr4  s     r  	<genexpr>z!expand_des_key.<locals>.<genexpr>u  s1      OOc5jD014OOOOOOr  )
isinstancebyteslen
ValueErrorr   INT_56_MASKr*  r   r,  r   ExpectedTypeErrorr/  r   _EXPAND_ITER)r4  s   `r  r   r   c  s    #u @s88q==:;;; 	C	#	# @77cK''FGGG55666#C???
C..C OOOO,OOOOOOr  c                    t          | t                    rKt          |           dk    rt          d          t	          t          t          |                               S t          | t                    r!| dk     s| t          k    rt          d          nt          j
        | dd          | dz  } d}d}|dk     r|| d	z  |z  z  }| dz  } |d
z  }|dk     |t           z  rJ |S )zGconvert DES key from 8 bytes to 7 bytes (by discarding the parity bits)rU   zkey must be 8 bytes in sizer   'key must be 64-bit non-negative integerr3  r4  r  8   r6  r2  )r:  r;  r<  r=  r,  shrink_des_keyr*  r   INT_64_MASKr   r?  )r4  resultoffsets      r  rD  rD  w  s   #u @s88q==:;;;~inn55666	C	#	# @77cK''FGGG ( #C???AICFF
2++3:&&	! 2++ +%&&&Mr  r  c                    t          | t                    rUt          |           dk    rt          |           } n"t          |           dk    rt	          d          t          |           } nt          j        | dd          t          |t                    r2t          |          dk    rt	          d          t          |          }nt          j        |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.
    r2  rU   zkey must be 7 or 8 bytesr;  r4  zinput block must be 8 bytesinput)
r:  r;  r<  r   r=  r*  r   r?  des_encrypt_int_blockr&  )r4  rI  saltroundsrF  s        r  r	   r	     s    B #u 9s88q== %%CCXX]]7888nn#C%888 % =u::??:;;;%  #E7G<<< #3tV<<F 6??r  c                    |dk     rt          d          |dk     s|t          k    rt          d          t          | t                    st	          j        | dd          | dk     s| t          k    rt          d          t          |t                    st	          j        |dd          |dk     s|t          k    rt          d	          t          t                       t          \  }}}}}}	}
}d }t           ||                     }|dz  dz  |dz  dz  z  |dz  dz	  z  |dz  dz	  z  }|dk    rdx}}nI|dz	  dz  |dz  z  }t          |t                    }|dz	  dz  |dz	  dz  z  }t          |t                    }|r<|dz  }|D ],\  }}|dz	  |z  |z  }|dz  |z  |z  |z  }|||dz	  dz           ||dz	  dz           z  ||dz	  dz           z  ||dz	  dz           z  ||dz	  dz           z  |	|dz	  dz           z  |
|dz	  dz           z  ||dz	  dz           z  z  }|dz	  |z  |z  }|dz  |z  |z  |z  }|||dz	  dz           ||dz	  dz           z  ||dz	  dz           z  ||dz	  dz           z  ||dz	  dz           z  |	|dz	  dz           z  |
|dz	  dz           z  ||dz	  dz           z  z  }.||}}|<|dz	  dz  |d z  d!z  z  |d"z	  d#z  z  |dz  d$z  z  }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intr4  rB  rI  z)input must be 64-bit non-negative integerNc              3      K   t           D ];\  }}t          | |          }t          ||          } |t          z  | t          z  fV  <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_evens       r  _iter_key_schedulez1des_encrypt_int_block.<locals>._iter_key_schedule  s_      # 	8 	8MFEvv..Ggu--FH$fx&777777	8 	8r  ?      i     i  r  i   rW      l   *UU iUUUUr   :   2   *   "      
      l       <<xx!   l       C #   il   pa )r=  INT_24_MASKr:  r   r   r?  rE  r  r  r  listr  r  r  )r4  rI  rK  rL  SPE0SPE1SPE2SPE3SPE4SPE5SPE6SPE7rU  ks_listLRrT  rQ  kBCs                        r  rJ  rJ    s$   D zz:;;;axx4+%%CDDD c9%% D#C666	qC+%%BCCC eY'' F#E5':::	ek))DEEE ~ 692D$dD$d8 8 8 %%c**++G /b	 
/b	 	"
/a		! /b	 	" 	 zz	AArkZ'EJ,>?QrkZ'UaZ:,EFQ
  !  ' 	: 	:OGVR%1$AB!a')A$2t|$tQUDL'992t|$%'+QUDL'9:2t|$%'+QUDL'9: 2t|$% (,QT4K'89 :A
 R%1$AB!a&(A$2t|$tQUDL'992t|$%'+QUDL'9:2t|$%'+QUDL'9: 2t|$% (,QT4K'89 :AA !1-  8 d))e))+ e))	+ d))+  Avr  )r   r  )!__doc__structpasslibr   passlib.utils.compatr   r   r   r   __all__rc  r>  rE  _KDATA_MASK_KPARITY_MASKrP  r  r  r  r  r  r  Structr"  r&  r*  r,  r/  r@  r   rD  r	   rJ  r  r  r  <module>r{     s  % %\       ; ; ; ; ; ; ; ; ; ; ; ; ; ;    !"  "& % % %#_ _ _P	 	 	 t$$& & &+ + +* * *5 5 5 vbBP P P(  06 6 6 6pE E E E E Er  