o
    g                     @   sJ   d Z ddlZddlmZ ddlmZ ddlmZ dd ZG dd	 d	Z	dS )
z,
Utility functions for dealing with primes.
    N)util)	byte_mask)SSHExceptionc                 C   sv   t | d }|d d }td|d d }	 t|}|dkr-t|d ||dd  }t |d}|| k r:	 |S q)z returns a random # from 0 to N-1            Tr   N)r   
bit_lengthpowosurandomr   inflate_long)nbits
byte_count
hbyte_maskxnum r   I/var/www/html/api-tag/env/lib/python3.10/site-packages/paramiko/primes.py_roll_random   s   
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ModulusPackzx
    convenience object for holding the contents of the /etc/ssh/moduli file,
    on systems that have such a file.
    c                 C   s   i | _ g | _d S )N)pack	discarded)selfr   r   r   __init__:   s   
zModulusPack.__init__c           
      C   s   |  \}}}}}}}t|}t|}t|}t|}t|}t|d}|dk s8|dk s8|d@ rB|dk rB|dk rB| j|df d S |dkrHd}t|}	|	|krd|	|d krd| j|d	|f d S |	| jvrng | j|	< | j|	 ||f d S )
N   r      r   d   z does not meet basic requirementsr   r   z"incorrectly reported bit length {})splitintr   appendr   r	   formatr   )
r   line	timestampmod_typeteststriessize	generatormodulusblr   r   r   _parse_modulus?   sF   	



zModulusPack._parse_modulusc                 C   sz   i | _ t|d+}|D ]}| }t|dks|d dkrqz| | W q   Y qW d   dS 1 s6w   Y  dS )zM
        :raises IOError: passed from any file operations that fail.
        rr   #N)r   openstriplenr,   )r   filenamefr#   r   r   r   	read_filem   s   "zModulusPack.read_filec                 C   s   t | j }t|dkrtdd}|D ]}||kr)||kr)||k s'|dkr)|}q|dkrA|D ]}||kr@||kr@||kr@|}q0|dkrQ|d }||krQ|d }tt| j| }| j| | S )Nr   zno moduli available)sortedr   keysr1   r   r   )r   minprefermaxbitsizesgoodbr   r   r   r   get_modulus|   s&    zModulusPack.get_modulusN)__name__
__module____qualname____doc__r   r,   r4   r>   r   r   r   r   r   4   s    .r   )
rB   r   paramikor   paramiko.commonr   paramiko.ssh_exceptionr   r   r   r   r   r   r   <module>   s   