o
    \¨Êh„  ã                   @   s¤   d Z ddlZejd dkrejd dkrddlT ddlT ddlmZ ddlZddlZddl	m
Z
 ddlmZ dd	lmZ G d
d„ deƒZdd„ ZG dd„ deƒZdS )z$Id$é    Né   é   )Ú*)Úb2a_hex)ÚClockRewindWarning)ÚSHAd256)ÚFortunaGeneratorc                   @   s>   e Zd ZdZejZdd„ Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
dS )ÚFortunaPoolag  Fortuna pool type

    This object acts like a hash object, with the following differences:

        - It keeps a count (the .length attribute) of the number of bytes that
          have been added to the pool
        - It supports a .reset() method for in-place reinitialization
        - The method to add bytes to the pool is .append(), not .update().
    c                 C   s   |   ¡  d S ©N)Úreset©Úself© r   úZ/var/www/html/env/lib/python3.10/site-packages/Crypto/Random/Fortuna/FortunaAccumulator.pyÚ__init__6   s   zFortunaPool.__init__c                 C   s"   | j  |¡ |  jt|ƒ7  _d S r
   )Ú_hÚupdateÚlengthÚlen)r   Údatar   r   r   Úappend9   s   zFortunaPool.appendc                 C   s
   | j  ¡ S r
   )r   Údigestr   r   r   r   r   =   s   
zFortunaPool.digestc                 C   s*   t jd dkrt|  ¡ ƒS t|  ¡ ƒ ¡ S )Nr   r   )ÚsysÚversion_infor   r   Údecoder   r   r   r   Ú	hexdigest@   s   zFortunaPool.hexdigestc                 C   s   t  ¡ | _d| _d S )Nr   )r   Únewr   r   r   r   r   r   r   F   s   

zFortunaPool.resetN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Údigest_sizer   r   r   r   r   r   r   r   r   r	   )   s    
r	   c                 C   sP   | dksJ ‚g }d}t dƒD ]}| |@ dkr| |¡ n |S |d> dB }q|S )a?  Return a list of pools indexes (in range(32)) that are to be included during reseed number r.

    According to _Practical Cryptography_, chapter 10.5.2 "Pools":

        "Pool P_i is included if 2**i is a divisor of r.  Thus P_0 is used
        every reseed, P_1 every other reseed, P_2 every fourth reseed, etc."
    r   r   é    )Úranger   )ÚrÚretvalÚmaskÚir   r   r   Úwhich_poolsJ   s   	ÿr(   c                   @   s>   e Zd ZdZdZdd„ Zdd„ Zdd„ Zdd
d„Zdd„ Z	d	S )ÚFortunaAccumulatoré@   gš™™™™™¹?c                 C   sF   d| _ t ¡ | _d | _dd„ tdƒD ƒ| _| jd | jd us!J ‚d S )Nr   c                 S   s   g | ]}t ƒ ‘qS r   )r	   )Ú.0r'   r   r   r   Ú
<listcomp>~   s    z/FortunaAccumulator.__init__.<locals>.<listcomp>r"   r   )Úreseed_countr   ÚAESGeneratorÚ	generatorÚlast_reseedr#   Úpoolsr   r   r   r   r   u   s
   
zFortunaAccumulator.__init__c                 C   s
   d | _ d S r
   )r0   r   r   r   r   Ú_forget_last_reseed�   s   
z&FortunaAccumulator._forget_last_reseedc                 C   sp   t   ¡ }| jd ur| j|krt dt¡ d | _| jd j| jkr2| jd u s-|| j| j kr2|  	|¡ | j
 |¡S )Nz-Clock rewind detected. Resetting last_reseed.r   )Útimer0   ÚwarningsÚwarnr   r1   r   Úmin_pool_sizeÚreseed_intervalÚ_reseedr/   Úpseudo_random_data)r   ÚbytesÚcurrent_timer   r   r   Úrandom_dataŠ   s   

zFortunaAccumulator.random_dataNc                 C   sx   |d u rt   ¡ }g }|  jd7  _|| _t| jƒD ]}| | j|  ¡ ¡ | j|  ¡  qtdƒ 	|¡}| j
 |¡ d S )Nr   Ú )r3   r-   r0   r(   r   r1   r   r   ÚbÚjoinr/   Úreseed)r   r;   Úseedr'   r   r   r   r8   –   s   zFortunaAccumulator._reseedc                 C   s˜   dt |ƒ  krdksJ ‚ J ‚d|  krdksJ ‚ J ‚d|  kr)dks,J ‚ J ‚| j|  t|ƒ¡ | j|  tt |ƒƒ¡ | j|  |¡ d S )Nr   r"   r   éÿ   é   )r   r1   r   Úbchr)r   Úsource_numberÚpool_numberr   r   r   r   Úadd_random_event£   s    z#FortunaAccumulator.add_random_eventr
   )
r   r   r   r6   r7   r   r2   r<   r8   rG   r   r   r   r   r)   _   s    	
r)   )Ú__revision__r   r   ÚCrypto.Util.py21compatÚCrypto.Util.py3compatÚbinasciir   r3   r4   ÚCrypto.pct_warningsr   r=   r   r   Úobjectr	   r(   r)   r   r   r   r   Ú<module>   s   !