
    IiN%                     l   S r SSKrSSKrSSKrSSKrSSKrSSKJrJrJ	r	J
r
JrJr  SSKJr  SS jrSS jrSS jrS rS	 rS
 rS rS rS rS rS rS r\R6                  " 5       qSq\R<                  " 5       qS r \4S jr! " S S5      r"\"" 5       r#S r$S r% " S S5      r&S r'S r(S S jr)S S jr*g)!z0
Useful functions used by the rest of paramiko.
    N)DEBUG	zero_byte	xffffffffmax_bytebyte_ordbyte_chr)	SSHConfigc           	         SnSnU(       d#  [        U 5      S:  a  [        U S   5      S:  a  Sn[        U 5      S-  (       a*  [        nU(       a  [        nUS[        U 5      S-  -
  -  U -   n [	        S[        U 5      S5       H(  nUS-  [
        R                  " SXUS-    5      S   -   nM*     U(       a  USS[        U 5      -  -  -  nU$ )zPturns a normalized byte string into a long-int
(adapted from Crypto.Util.number)r                >I   )lenr   r   r   rangestructunpack)salways_positiveoutnegativefilleris         3/venv/lib/python3.13/site-packages/paramiko/util.pyinflate_longr   )   s     CHA
!A$41G
1vzF a#a&1*n%)1c!fa byFMM$a!e=a@@ !qQQZ  J    c                    [        5       n[        U 5      n U S:w  a:  U S:w  a4  [        R                  " SU [        -  5      U-   nU S-  n U S:w  a  U S:w  a  M4  [        U5       H'  nU S:X  a  US   S:w  a    O,U S:X  d  M  US   S:w  d  M'    O   SnU S:X  a  [        nO[        nX#S   S nU(       aB  U S:X  a  [        US   5      S	:  a	  [        U-   nU S:X  a  [        US   5      S	:  a	  [        U-   nU$ )
zPturns a long-int into a normalized byte string
(adapted from Crypto.Util.number)r   r   r   r      )r   Nr   )	bytesintr   packr   	enumerater   r   r   )nadd_sign_paddingr   r   s       r   deflate_longr'   >   s     	AAA6RKKa)m,q0	b 6R q\F1G!A$$,	  6AA	A$&	AF!A$4/AAG(1Q4.4/1AHr   c                 (   Sn/ n[        U 5      US-   :  a8  UR                  [        XUS-    5      5        US-  n[        U 5      US-   :  a  M8  U[        U 5      :  a  UR                  [        XS  5      5        U Vs/ s H  oAU-   PM	     sn$ s  snf )Nr      )r   appendformat_binary_line)dataprefixxr   lines        r   format_binaryr0   ]   s    	A
C
d)a"f


%dq2v&678	R d)a"f
 	3t9}

%d2h/0&)*cdTMc***s   >Bc           
      D   SR                  U  Vs/ s H  nSR                  [        U5      5      PM     sn5      nSR                  U  Vs/ s H/  nSR                  [        U5      5      [        U5      S-   S-     PM1     sn5      nSR                  X#5      $ s  snf s  snf )N z{:02X} z.{:c}..?   _   z	{:50s} {})joinformatr   )r,   cleftrights       r   r+   r+   h   s    884@4aX__Xa[14@ADGGJNO$Q		(1+	&b(8R'?	@$OE d**	 AOs   $B
6Bc                     SnU  HK  n[        U5      nSUs=::  a  S::  a  O  OU[        U5      -  nM.  U[        SR                  U5      5      -  nMM     U$ )Nr   r      z%{:02X})r   r   br7   )r   r   r8   r   s       r   safe_stringr>   p   sT    
CQK>c>8A;C1Y%%a())C  Jr   c                      U R                  5       $ ! [         aU    [        U S5      n[        US   5      nUS:X  a   g[	        U5      S-  nUS-  (       d  US-  nUS-  nUS-  (       d  M  Us $ f = f)NFr   r   r   r   )
bit_lengthAttributeErrorr'   r   r   )r%   normhbytebitlens       r   r@   r@   {   s~    ||~ 	Au%a!A:TQ4<aKEaKF 4<< 	s    *A1,A1,A10A1c                      SR                  [        R                  " [        R                  " 5       6 5      R                  S5      $ )Nr3   
)r6   	tracebackformat_exceptionsysexc_infosplit r   r   
tb_stringsrM      s-    779--s||~>?EEdKKr   c                 v   [        5       n[        5       n[        U5      S:  a  USS nUS:  a  U " 5       n[        U5      S:  a  UR                  U5        UR                  [        U5      5        UR                  U5        UR	                  5       n[        U[        U5      5      nXESU -  nX7-  nUS:  a  M  U$ )a  
Given a password, passphrase, or other human-source key, scramble it
through a secure hash into some keyworthy bytes.  This specific algorithm
is used for encrypting/decrypting private key files.

:param function hash_alg: A function which creates a new hash object, such
    as ``hashlib.sha256``.
:param salt: data to salt the hash with.
:type bytes salt: Hash salt bytes.
:param str key: human-entered password or passphrase.
:param int nbytes: number of bytes to generate.
:return: Key data, as `bytes`.
r   Nr   )r!   r   updater=   digestmin)hash_algsaltkeynbyteskeydatarP   hash_objsizes           r   generate_key_bytesrY      s     gGWF
4y1}BQx
1*:v;?OOF##"63v;'%4=  1* Nr   c                     SSK Jn  U" U 5      $ )ab  
Read a file of known SSH host keys, in the format used by openssh, and
return a compound dict of ``hostname -> keytype ->`` `PKey
<paramiko.pkey.PKey>`. The hostname may be an IP address or DNS name.  The
keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``.

This type of file unfortunately doesn't exist on Windows, but on posix,
it will usually be stored in ``os.path.expanduser("~/.ssh/known_hosts")``.

Since 1.5.3, this is just a wrapper around `.HostKeys`.

:param str filename: name of the file to read host keys from
:return:
    nested dict of `.PKey` objects, indexed by hostname and then keytype
r   )HostKeys)paramiko.hostkeysr[   )filenamer[   s     r   load_host_keysr^      s      +Hr   c                 <    [        5       nUR                  U 5        U$ )z
Provided only as a backward-compatible wrapper around `.SSHConfig`.

.. deprecated:: 2.7
    Use `SSHConfig.from_file` instead.
)r	   parse)file_objconfigs     r   parse_ssh_configrc      s     [F
LLMr   c                 $    UR                  U 5      $ )zE
Provided only as a backward-compatible wrapper around `.SSHConfig`.
)lookup)hostnamerb   s     r   lookup_ssh_host_configrg      s     ==""r   c                 |    SSUpCnSSU pvnUS:  a!  XG-  nXRXX-  -
  pRXcXh-  -
  pcXtXx-  -
  ptUS:  a  M!  US:  a  X1-  nU$ )Nr   r   rL   )	r.   mu1u2u3v1v2v3qs	            r   mod_inverserq      se    AqBBAqBB
q&H"&[B"&[B"&[B	 q&
 
Av
Ir   c                       [         R                  $ ! [         aL    [           [        S-  q[        [         l        S S S 5        O! , (       d  f       O= f[         R                  s $ f = f)Nr   )_g_thread_dataidrA   _g_thread_lock_g_thread_counterrL   r   r   get_thread_idrw      sP    !    !" 1N ^^    	!s%    A(A	A(
A	A('A(c                 F   [         R                  " S5      n[        UR                  5      S:  a  gUR	                  U5        [        U S5      n[         R                  " U5      nSnUS-  nUR                  [         R                  " US5      5        UR                  U5        g)zGsend paramiko logs to a logfile,
if they're not already going somewhereparamikor   Naz>%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3dz %(name)s: %(message)sz%Y%m%d-%H:%M:%S)
logging	getLoggerr   handlerssetLevelopenStreamHandlersetFormatter	Formatter
addHandler)r]   levelloggerfhandlerfrms         r   log_to_filer      s     z*F
6??a
OOEXsA##A&G
JC##C**30ABC
gr   c                       \ rS rSrS rSrg)PFilteri  c                 "    [        5       Ul        g)NT)rw   	_threadid)selfrecords     r   filterPFilter.filter  s    (?r   rL   N)__name__
__module____qualname____firstlineno__r   __static_attributes__rL   r   r   r   r     s    r   r   c                 \    [         R                  " U 5      nUR                  [        5        U$ N)r{   r|   	addFilter_pfilter)namer   s     r   
get_loggerr     s%    t$F
XMr   c                     [        U 5      [        U5      :w  a  gSn[        [        U 5      5       H!  nU[        X   5      [        X   5      -  -  nM#     US:H  $ )NFr   )r   r   r   )rz   r=   resr   s       r   constant_time_bytes_eqr     sP    
1vQ
C3q6]x~.. !8Or   c                        \ rS rSrS rS rSrg)ClosingContextManageri  c                     U $ r   rL   )r   s    r   	__enter__ClosingContextManager.__enter__  s    r   c                 $    U R                  5         g r   )close)r   typevaluerG   s       r   __exit__ClosingContextManager.__exit__!  s    

r   rL   N)r   r   r   r   r   r   r   rL   r   r   r   r     s    r   r   c                 ,    [        U [        X5      5      $ r   )maxrQ   )minimumvalmaximums      r   clamp_valuer   %  s    wC)**r   c                      [        U 5      $ ! [         a(     U R                  5       s $ ! [         a    U s s $ f = ff = f)z2
Coerce to bytes if possible or return unchanged.
)r=   	TypeErrorasbytesrA   )r   s    r   r   r   )  sJ     t 		 99; 	 H			s    
 
?*?;?;?c                     [        U [        5      (       a  U $ [        U [        5      (       a  U R                  U5      $ [	        S[        U 5       35      e)zcast unicode or bytes to bytesExpected unicode or bytes, got )
isinstancer!   strencoder   r   r   encodings     r   r=   r=   ?  sG    !U	As		xx!!9$q'CDDr   c                     [        U [        5      (       a  U R                  U5      $ [        U [        5      (       a  U $ [	        S[        U 5       35      e)z cast bytes or unicode to unicoder   )r   r!   decoder   r   r   r   s     r   ur   J  sG    !Uxx!!	As		9$q'CDDr   )F)T)r3   )utf8)+__doc__rI   r   rG   	threadingr{   paramiko.commonr   r   r   r   r   r   paramiko.configr	   r   r'   r0   r+   r>   r@   rM   rY   r^   rc   rg   rq   localrs   rv   Lockru   rw   r   r   r   r   r   r   r   r   r=   r   rL   r   r   <module>r      s   &
       &*>++L>*	# " !! !&    9 +,EEr   