o
    gN%                     @   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)d8d3d4Z*dS )9z0
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}|r"t}|dt | d   |  } tdt | dD ]}|d> td| ||d  d  }q6|rW|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)salways_positiveoutnegativefilleri r   G/var/www/html/api-tag/env/lib/python3.10/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 } | dkr%| dkst|D ]}| dkr7|d dkr7 n| dkrC|d dkrC nq)d}| dkrMt}nt}||d d }|ry| dkrit|d d	krit| }| dkryt|d d	k ryt| }|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   )nadd_sign_paddingr   r   r   r   r   deflate_long>   s.   r$    c                    s~   d}g }t | |d kr%|t| ||d   |d7 }t | |d ks|t | k r6|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   r0   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}..?   _   r2   r4   r   r   r   r+   k   s   * z	{:50s} {})joinr3   )r.   leftrightr   r   r   r-   h   s
   r-   c                 C   sR   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   br3   )r   r   r5   r   r   r   r   safe_stringp   s   r>   c                 C   sr   z|   W S  ty8   t| d}t|d }|dkrY dS t|d }|d@ s4|dK }|d8 }|d@ r(| Y S w )NFr   r
   r   r	   )
bit_lengthAttributeErrorr$   r   r   )r"   normhbytebitlenr   r   r   r?   {   s   

r?   c                   C   s   d tjt  dS )Nr%   
)r8   	tracebackformat_exceptionsysexc_infosplitr   r   r   r   
tb_strings   s   rJ   c                 C   s   t  }t  }t|dkr|dd }|dkrK|  }t|dkr$|| |t| || | }t|t|}||d| 7 }||8 }|dks|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)hash_algsaltkeynbyteskeydatarL   hash_objsizer   r   r   generate_key_bytes   s    


rU   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.hostkeysrV   )filenamerV   r   r   r   load_host_keys   s   rY   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   
r]   c                 C   s
   | | S )zM
    Provided only as a backward-compatible wrapper around `.SSHConfig`.
    )lookup)hostnamer\   r   r   r   lookup_ssh_host_config   s   
r`   c           	      C   s   dd|}}}dd| }}}|dkr7|| }||||  }}||||  }}||||  }}|dks|dk r?||7 }|S )Nr
   r   r   )	r/   mu1u2u3v1v2v3qr   r   r   mod_inverse   s   ri   c                   C   sT   zt jW S  ty)   t td7 att _W d    n1 sw   Y  t j Y S w )Nr
   )_g_thread_dataidr@   _g_thread_lock_g_thread_counterr   r   r   r   get_thread_id   s   
rn   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)rX   levelloggerfhandlerfrmr   r   r   log_to_file   s   



r   c                   @   s   e Zd Zdd ZdS )PFilterc                 C   s   t  |_dS )NT)rn   	_threadid)selfrecordr   r   r   filter  s   zPFilter.filterN)__name__
__module____qualname__r   r   r   r   r   r     s    r   c                 C   s   t | }|t |S N)rq   rr   	addFilter_pfilter)namer{   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   )rp   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   )r   r   r   r   	__enter__  s   zClosingContextManager.__enter__c                 C   s   |    d S r   )close)r   typevaluerE   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   )maxrM   )minimumvalmaximumr   r   r   clamp_value%  s   r   c                 C   sD   zt | W S  ty!   z|  W  Y S  ty    |  Y  Y S w w )z:
    Coerce to bytes if possible or return unchanged.
    )r=   	TypeErrorasbytesr@   )r   r   r   r   r   )  s   
r   utf8c                 C   s4   t | tr| S t | tr| |S tdt|  )zcast unicode or bytes to bytesExpected unicode or bytes, got )
isinstancer   strencoder   r   r   encodingr   r   r   r=   ?  s
   


r=   c                 C   s4   t | tr
| |S t | tr| S tdt|  )z cast bytes or unicode to unicoder   )r   r   decoder   r   r   r   r   r   r   uJ  s
   


r   )F)T)r%   )r   )+__doc__rG   r   rE   	threadingrq   paramiko.commonr   r   r   r   r   r   paramiko.configr   r   r$   r0   r-   r>   r?   rJ   rU   rY   r]   r`   ri   localrj   rm   Lockrl   rn   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   <module>   sD    




