U
    •uÄcÈ  ã                   @   sx   d dl Z d dlmZ d dlZd dlZdeeee edœdd„Zdeeee edœdd„Zdeeeee ed	œd
d„Z	dS )é    N)ÚOptional)ÚemailÚpasswordÚsaltÚreturnc              
   C   sh   |s,t  ¡ }| |  d¡¡ | ¡ dd… }t|ƒdks<t‚tjj	| d¡|ddddtjj
jdd	�}|S )
a]  
    Compute argon2id based secret from user email and password only. This uses Argon2id
    for stretching a potentially weak user password/PIN and subsequent HKDF based key
    extending to derive private key material (PKM) for different usage contexts.

    The Argon2 parameters used are the following:

    * kdf ``argon2id-13``
    * time cost ``4096``
    * memory cost ``512``
    * parallelism ``1``

    See `draft-irtf-cfrg-argon2 <https://datatracker.ietf.org/doc/draft-irtf-cfrg-argon2/>`__ and
    `argon2-cffi <https://argon2-cffi.readthedocs.io/en/stable/>`__.

    :param email: User email.
    :param password: User password.
    :param salt: Optional salt to use (must be 16 bytes long). If none is given, compute salt
        from email as ``salt = SHA256(email)[:16]``.
    :return: The computed private key material (256b, 32 octets).
    Úutf8Né   i   i   é   é    é   )Zsecretr   Z	time_costZmemory_costZparallelismZhash_lenÚtypeÚversion)ÚhashlibÚsha256ÚupdateÚencodeÚdigestÚlenÚAssertionErrorÚargon2Z	low_levelZhash_secret_rawÚTypeÚID)r   r   r   ÚmÚpkm© r   úV/var/www/html/NKSPORTS/school/venv/lib/python3.8/site-packages/autobahn/xbr/_wallet.pyÚstretch_argon2_secret!   s     ør   )r   Úcontextr   r   c                 C   s$   t j|| tjd�}|j|dd�}|S )zö

    Expand ``pkm`` and ``context`` into a key of length ``bytes`` using
    HKDF's expand function based on HMAC SHA-512). See the HKDF draft RFC and paper for usage notes.

    :param pkm:
    :param context:
    :param salt:
    :return:
    )r   Zinput_key_materialÚhashr
   )ÚinfoÚlength)ÚhkdfZHkdfr   Úsha512Úexpand)r   r   r   ZkdfÚkeyr   r   r   Úexpand_argon2_secretK   s    r%   )r   r   r   r   r   c                 C   sf   |s,t  ¡ }| |  d¡¡ | ¡ dd… }t|ƒdks<t‚| d¡}t| ||d�}t|||d�}|S )z_

    :param email:
    :param password:
    :param context:
    :param salt:
    :return:
    r   Nr   )r   r   r   )r   r   r   )	r   r   r   r   r   r   r   r   r%   )r   r   r   r   r   r   r$   r   r   r   Úpkm_from_argon2_secret[   s    	
r&   )N)N)N)
r   Útypingr   r   r!   ÚstrÚbytesr   r%   r&   r   r   r   r   Ú<module>   s   *