U
    weN%                     @   s@  d 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
mZmZ ddlmZ d5ddZd6d	d
Zd7d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e adae ad d! Z efd"d#Z!G d$d% d%Z"e" Z#d&d' Z$d(d) Z%G d*d+ d+Z&d,d- Z'd.d/ Z(d8d1d2Z)d9d3d4Z*dS ):z0
Useful functions used by the rest of paramiko.
    N)DEBUG	zero_byte	xffffffffmax_bytebyte_ordbyte_chr)	SSHConfigFc              	   C   s   d}d}|s,t | dkr,t| d dkr,d}t | d r\t}|rDt}|dt | d   |  } tdt | dD ](}|d> td| ||d  d  }ql|r|ddt |  > 8 }|S )zTturns a normalized byte string into a long-int
    (adapted from Crypto.Util.number)r                >I   )lenr   r   r   rangestructunpack)sZalways_positiveoutnegativefilleri r   l/mounts/lovelace/software/anaconda3/envs/qiime2-amplicon-2024.2/lib/python3.8/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 dkr` q| dkrD|d d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	   )	bytesintr   packr   	enumerater   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   s     z!format_binary.<locals>.<listcomp>)r   appendformat_binary_line)datar(   xr   r   r'   r   format_binary]   s    
r.   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   s     z&format_binary_line.<locals>.<listcomp>r#   c                 S   s*   g | ]"}d  t|t|d d  qS )z.{:c}..?   _   r0   r2   r   r   r   r)   k   s     z	{:50s} {})joinr1   )r,   leftrightr   r   r   r+   h   s
    r+   c                 C   sR   d}| D ]D}t |}d|  kr(dkr:n n|t|7 }q|td|7 }q|S )N    r      z%{:02X})r   r   br1   )r   r   r3   r   r   r   r   safe_stringp   s    r<   c                 C   sr   z
|   W S  tk
rl   t| d}t|d }|dkr>Y dS t|d }|d@ sd|dK }|d8 }qJ| Y S X d S )NFr   r
   r   r	   )
bit_lengthAttributeErrorr"   r   r   )r!   normZhbyteZbitlenr   r   r   r=   {   s    


r=   c                   C   s   d tjt  dS )Nr#   
)r6   	tracebackformat_exceptionsysexc_infosplitr   r   r   r   
tb_strings   s    rF   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 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)Zhash_algZsaltkeynbytesZkeydatarH   Zhash_objsizer   r   r   generate_key_bytes   s    


rM   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)paramiko.hostkeysrN   )filenamerN   r   r   r   load_host_keys   s    rQ   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)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   )	r-   mu1u2u3v1v2Zv3qr   r   r   mod_inverse   s    r`   c                   C   sF   zt jW S  tk
r@   t td7 att _W 5 Q R X t j Y S X d S )Nr
   )_g_thread_dataidr>   _g_thread_lock_g_thread_counterr   r   r   r   get_thread_id   s    re   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 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)rP   levelloggerfhandlerfrmr   r   r   log_to_file   s    



rv   c                   @   s   e Zd Zdd ZdS )PFilterc                 C   s   t  |_dS )NT)re   Z	_threadid)selfrecordr   r   r   filter  s    zPFilter.filterN)__name__
__module____qualname__rz   r   r   r   r   rw     s   rw   c                 C   s   t | }|t |S N)rh   ri   	addFilter_pfilter)namerr   r   r   r   
get_logger  s    

r   c                 C   sN   t | t |krdS d}tt | D ] }|t| | t|| A O }q$|dkS )NFr   )r   r   r   )rg   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 r~   r   )rx   r   r   r   	__enter__  s    zClosingContextManager.__enter__c                 C   s   |    d S r~   )close)rx   typevaluerA   r   r   r   __exit__!  s    zClosingContextManager.__exit__N)r{   r|   r}   r   r   r   r   r   r   r     s   r   c                 C   s   t | t||S r~   )maxrI   )minimumvalmaximumr   r   r   clamp_value%  s    r   c                 C   sP   z
t | W S  tk
rJ   z|  W  Y S  tk
rD   |  Y  Y S X Y nX dS )z:
    Coerce to bytes if possible or return unchanged.
    N)r;   	TypeErrorasbytesr>   )r   r   r   r   r   )  s    
r   utf8c                 C   s8   t | tr| S t | tr"| |S tdt|  dS )zcast unicode or bytes to bytesExpected unicode or bytes, got N)
isinstancer   strencoder   r   r   encodingr   r   r   r;   ?  s
    


r;   c                 C   s8   t | tr| |S t | tr"| S tdt|  dS )z cast bytes or unicode to unicoder   N)r   r   decoder   r   r   r   r   r   r   uJ  s
    


r   )F)T)r#   )r   )r   )+__doc__rC   r   rA   	threadingrh   paramiko.commonr   r   r   r   r   r   paramiko.configr   r   r"   r.   r+   r<   r=   rF   rM   rQ   rU   rX   r`   localra   rd   Lockrc   re   rv   rw   r   r   r   r   r   r   r;   r   r   r   r   r   <module>   sB    




