a
   G`                    @   sl  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ d3dd	ZerendZerend
Zd4ddZd5ddZdd Zdd Zdd Zdd Zdd Zdd Z dd Z!dd  Z"d!d" Z#i a$da%e& a'd#d$ Z(e
fd%d&Z)G d'd( d(e*Z+e+ Z,d)d* Z-d+d, Z.d-d. Z/G d/d0 d0e*Z0d1d2 Z1dS )6z0
Useful functions used by the rest of paramiko.
    )
generatorsN)DEBUG	zero_byte	xffffffffmax_byte)PY2longbyte_chrbyte_ordb)	SSHConfigFc              	   C   s   t d}d}|s0t| dkr0t| d dkr0d}t| d r`t}|rHt}|dt| d   |  } tdt| dD ](}|d> td| ||d  d  }qp|r|t ddt|  > 8 }|S )zTturns a normalized byte string into a long-int
    (adapted from Crypto.Util.number)r                >I   )r   lenr
   r   r   rangestructunpack)sZalways_positiveoutnegativeZfilleri r   1/usr/lib/python3.9/site-packages/paramiko/util.pyinflate_long%   s     &r      Tc                 C   s   t  }t| } | dkr<| dkr<td| t@ | }| dL } qt|D ]4}| dkr`|d tkr` q| dkrD|d tkrD qqDd}| dkrt}nt	}||d d }|r| dkrt
|d dkrt| }| dkrt
|d dk rt	| }|S )	zTturns a long-int into a normalized byte string
    (adapted from Crypto.Util.number)r   r   r   r   )r   Nr   )bytesr   r   packr   	enumeratedeflate_zero
deflate_ffr   r   r
   )nZadd_sign_paddingr   r   r   r   r   deflate_long>   s*    
r&    c                    sp   d}g }t | |d kr<|t| ||d   |d7 }q|t | k r^|t| |d    fdd|D S )Nr      c                    s   g | ]} | qS r   r   ).0lineprefixr   r   
<listcomp>e       z!format_binary.<locals>.<listcomp>)r   appendformat_binary_line)datar,   xr   r   r+   r   format_binary]   s    
r3   c                 C   s4   d dd | D }d dd | D }d||S )N c                 S   s   g | ]}d  t|qS )z{:02X}formatr
   r)   cr   r   r   r-   i   r.   z&format_binary_line.<locals>.<listcomp>r'   c                 S   s*   g | ]"}d  t|t|d d  qS )z.{:c}..?   _   r5   r7   r   r   r   r-   k   r.   z	{:50s} {})joinr6   )r1   leftrightr   r   r   r0   h   s
    r0   c                 C   sR   d}| D ]D}t |}d|  kr(dkr:n n|t|7 }q|td|7 }q|S )Nr.   r      z%{:02X})r
   r	   r   r6   )r   r   r8   r   r   r   r   safe_stringp   s    r?   c                 C   sp   z
|   W S  tyj   t| d}t|d }|dkr<Y dS t|d }|d@ sb|dK }|d8 }qH| Y S 0 d S )NFr   r   r   r   )
bit_lengthAttributeErrorr&   r
   r   )r%   ZnormZhbyteZbitlenr   r   r   r@   {   s    


r@   c                   C   s   d tjt  dS )Nr'   
)r;   	tracebackformat_exceptionsysexc_infosplitr   r   r   r   
tb_strings   s    rH   c                 C   s   t  }t  }t|dkr$|dd }|dkr|  }t|dkrH|| |t| || | }t|t|}||d| 7 }||8 }q$|S )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 salt: byte string
    :param str key: human-entered password or passphrase.
    :param int nbytes: number of bytes to generate.
    :return: Key data `str`
    r   Nr   )r    r   updater   digestmin)Zhash_algZsaltkeynbytesZkeydatarJ   Zhash_objsizer   r   r   generate_key_bytes   s    


rO   c                 C   s   ddl m} || S )a  
    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)Zparamiko.hostkeysrP   )filenamerP   r   r   r   load_host_keys   s    rR   c                 C   s   t  }||  |S )z
    Provided only as a backward-compatible wrapper around `.SSHConfig`.

    .. deprecated:: 2.7
        Use `SSHConfig.from_file` instead.
    )r   parse)Zfile_objconfigr   r   r   parse_ssh_config   s    
rU   c                 C   s
   | | S )zM
    Provided only as a backward-compatible wrapper around `.SSHConfig`.
    )lookup)hostnamerT   r   r   r   lookup_ssh_host_config   s    rX   c           	      C   s|   dd|  }}}dd|   }}}|dkrh|| }||||   }}||||   }}||||   }}q |dk rx||7 }|S )Nr   r   r   )	r2   mZu1Zu2Zu3Zv1Zv2Zv3qr   r   r   mod_inverse   s    r[   c                  C   sf   t t } z
t|  W S  ty`   t  z td7 at }t| < W t  n
t  0 | Y S 0 d S )Nr   )	id	threadingcurrentThread_g_thread_idsKeyError_g_thread_lockacquire_g_thread_counterrelease)tidretr   r   r   get_thread_id   s    
rg   c                 C   sf   t d}t|jdkrdS || t| d}t |}d}|d7 }|t |d |	| dS )zKsend paramiko logs to a logfile,
    if they're not already going somewhereZparamikor   Naz>%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3dz %(name)s: %(message)sz%Y%m%d-%H:%M:%S)
logging	getLoggerr   handlersZsetLevelopenZStreamHandlerZsetFormatterZ	FormatterZ
addHandler)rQ   levelloggerfZhandlerZfrmr   r   r   log_to_file   s    



rp   c                   @   s   e Zd Zdd ZdS )PFilterc                 C   s   t  |_dS )NT)rg   Z	_threadid)selfrecordr   r   r   filter	  s    zPFilter.filterN)__name__
__module____qualname__rt   r   r   r   r   rq     s   rq   c                 C   s   t | }|t |S N)ri   rj   Z	addFilter_pfilter)namern   r   r   r   
get_logger  s    

r{   c              
   C   sB   z|  W S  t y: } z|jtjkr& W Y d}~q d}~0 0 q dS )z6Retries function until it doesn't raise an EINTR errorN)EnvironmentErrorerrnoZEINTR)functioner   r   r   retry_on_signal  s
    r   c                 C   sV   t | t |krdS d}tr tntt | D ] }|t| | t|| A O }q,|dkS )NFr   )r   r   Zxranger   r
   )rh   r   resr   r   r   r   constant_time_bytes_eq!  s    r   c                   @   s   e Zd Zdd Zdd ZdS )ClosingContextManagerc                 C   s   | S rx   r   )rr   r   r   r   	__enter__,  s    zClosingContextManager.__enter__c                 C   s   |    d S rx   )close)rr   typevaluerC   r   r   r   __exit__/  s    zClosingContextManager.__exit__N)ru   rv   rw   r   r   r   r   r   r   r   +  s   r   c                 C   s   t | t||S rx   )maxrK   )ZminimumvalZmaximumr   r   r   clamp_value3  s    r   )F)T)r'   )2__doc__Z
__future__r   r}   rE   r   rC   r]   ri   Zparamiko.commonr   r   r   r   Zparamiko.py3compatr   r   r	   r
   r   Zparamiko.configr   r   r#   r$   r&   r3   r0   r?   r@   rH   rO   rR   rU   rX   r[   r_   rc   Lockra   rg   rp   objectrq   ry   r{   r   r   r   r   r   r   r   r   <module>   sH   




