a
   Zdq#                   @   sP   d Z ddlZddlmZ ddlmZmZ ddlmZ dd Z	G dd	 d	e
ZdS )
z,
Utility functions for dealing with primes.
    N)util)	byte_masklong)SSHExceptionc                 C   st   t | d }|d d }td|d d }t|}|dkrXt|d ||dd  }t |d}|| k r,qpq,|S )z returns a random # from 0 to N-1            r   N)r   
bit_lengthpowosurandomr   Zinflate_long)nbitsZ
byte_countZ
hbyte_maskxZnum r   3/usr/lib/python3.9/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 sp|dk sp|d@ r|dk r|dk r| j|df d S |dkrd}t|}	|	|kr|	|d kr| j|d	|f d S |	| jvrg | j|	< | j|	 ||f d S )
N   r	      r   d   z does not meet basic requirementsr   r   z"incorrectly reported bit length {})	splitintr   r   appendr   r
   formatr   )
r   lineZ	timestampmod_typeZtestsZtriessize	generatormodulusZblr   r   r   _parse_modulus?   sP    	



zModulusPack._parse_modulusc                 C   s   i | _ t|d\}|D ]F}| }t|dks|d dkr<qz| | W q   Y qY q0 qW d   n1 sr0    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 sN|dkr*|}q*|dkr|D ] }||kr`||kr`||kr`|}q`|dkr|d }||kr|d }tt| j| }| j| | S )Nr   zno moduli available)sortedr   keysr*   r   r   )r   minZprefermaxZbitsizesZgoodbr   r   r   r   get_modulus|   s"     zModulusPack.get_modulusN)__name__
__module____qualname____doc__r   r%   r-   r4   r   r   r   r   r   4   s
   .r   )r8   r   Zparamikor   Zparamiko.py3compatr   r   Zparamiko.ssh_exceptionr   r   objectr   r   r   r   r   <module>   s   