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   ú8/usr/local/lib/python3.10/dist-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    




