o
    gf                     @   sr  d Z g dZddlZddlZddlZddlmZmZ zddlZW n! e	yA   ddl
ZzddlmZ W n e	y>   dZY nw Y nw G dd deZeejZeejZdd	 Zd
d Zedurkdd Zdd ZdZnedZdd Zdd ZdZz
ddlmZmZ W n; e	y   ddlZzej ej W n ey   e	dw ze W n e y   dd ZY nw dd Z!dd Z"Y n	w dd Z!dd Z"zej#Z$W n< ey   zddl%m&Z& W n  e	y   ze'd d!Z(W n e)y   e	d"w d#d$ Z$Y n
w e& Z*e*fd%d$Z$Y nw ze+ j,Z,W n ey)   dd&l-m.Z.m/Z/ i Z0d0d(d)Z,Y nw d*d+ Z1d,d- Z2d1d.d/Z3dS )2a,  Module containing a cryptographic-quality source of randomness and
other cryptographically useful functionality

Python 2.4 needs no external support for this module, nor does Python
2.3 on a system with /dev/urandom.

Other configurations will need a quality source of random bytes and
access to a function that will convert binary strings to long
integers. This module will work with the Python Cryptography Toolkit
(pycrypto) if it is present. pycrypto can be found with a search
engine, but is currently found at:

http://www.amk.ca/python/code/crypto
)
base64ToLongbinaryToLonghmacSha1
hmacSha256longToBase64longToBinaryrandomString	randrangesha1sha256    N)toBase64
fromBase64)SHA256c                   @   s   e Zd Zdd ZdS )HashContainerc                 C   s   || _ | j| _d S N)newdigest_size)selfhash_constructor r   Q/var/www/html/humari/django-venv/lib/python3.10/site-packages/openid/cryptutil.py__init__/   s   zHashContainer.__init__N)__name__
__module____qualname__r   r   r   r   r   r   .   s    r   c                 C      t | |t S r   )hmacr   sha1_moduledigestkeytextr   r   r   r   6      r   c                 C      t |  S r   )r   r   r   sr   r   r   r	   9      r	   c                 C   r   r   )r   r   sha256_moduler   r   r   r   r   r   =   r"   r   c                 C   r#   r   )r'   r   r   r$   r   r   r   r
   @   r&   r
   TzAUse Python 2.5, install pycrypto or install hashlib to use SHA256c                 C      t r   
_no_sha256)
unused_keyunused_textr   r   r   r   I      c                 C   r(   r   r)   r$   r   r   r   r
   L   r-   F)long_to_bytesbytes_to_longz4No functionality for serializing long integers foundc                 C   s   t | jtt| d ddS )N   )map__getitem__xrangelen)seqr   r   r   reversedc   s   r7   c                 C   s    | dkrdS d tt| S )Nr     )joinr7   pickleencode_longlr   r   r   r   f   s   r   c                 C   s   t dt| S )Nr9   )r;   decode_longr:   r7   r$   r   r   r   r   l   s   r   c                 C   s4   | dk rt dt| }t|d dkrd| S |S )Nr   -This function only supports positive integers   r8   )
ValueErrorr.   ord)r>   bytesr   r   r   r   q   s   c                 C   s,   | st dt| d dkrt dt| S )Nz Empty string passed to strToLongr   rA   r@   )rB   rC   r/   )rD   r   r   r   r   {   s
   )
RandomPoolz/dev/urandomrbz'No adequate source of randomness found!c                 C   sB   g }| rt | }| t|8 } || | dksJ | sd|S )Nr   r9   )_urandomreadr5   appendr:   )nrD   chunkr   r   r   getBytes   s   


rL   c                 C   s   |j | k r	|  || S r   )entropy	randomize	get_bytes)rJ   poolr   r   r   rL      s   

)logceilr0   c           
      C   s   |d u r| }d} ||  | }zt | \}}W n6 tyL   t|}|d dkr.t|d }nt|}d| }|| }tt dkrDt   ||ft |< Y nw 	 dt| }t|}	|	|kr]nqN| |	| |  S )Nr   r8   r0      
   )_duplicate_cacheKeyErrorr   r5   clearrL   r   )
startstopstepr	duplicatenbytesrbytesmxrandrD   rJ   r   r   r   r      s0   r   c                 C      t t| S r   )r   r   r=   r   r   r   r         r   c                 C   r`   r   )r   r   r$   r   r   r   r      ra   r   c                    s6    du rt | S t d fddt| D S )z:Produce a string of length random bytes, chosen from chrs.Nr9   c                    s   g | ]} t  qS r   )r   ).0_chrsrJ   r   r   
<listcomp>   s    z randomString.<locals>.<listcomp>)rL   r5   r:   r4   )lengthre   r   rd   r   r      s   r   )Nr0   r   )4__doc____all__r   osrandomopenid.oidutilr   r   hashlibImportErrorshar   Crypto.Hashr   r'   objectr   r	   r
   r   r   SHA256_AVAILABLENotImplementedErrorr*   Crypto.Util.numberr.   r/   r;   r<   r?   AttributeErrorr7   	NameErrorr   r   urandomrL   Crypto.Util.randpoolrE   filerG   IOError_poolSystemRandomr   mathrQ   rR   rU   r   r   r   r   r   r   r   <module>   s    	





+