o
    >h                     @   s   d dl Z d dlmZ d dlZd dlZddededee defddZdd	ed
edee defddZddeded
edee def
ddZ	dS )    N)Optionalemailpasswordsaltreturnc              
   C   sh   |st  }|| d | dd }t|dksJ 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             )secretr   	time_costmemory_costparallelismhash_lentypeversion)hashlibsha256updateencodedigestlenargon2	low_levelhash_secret_rawTypeID)r   r   r   mpkm r    t/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/xbr/_wallet.pystretch_argon2_secret!   s    r"   r   contextc                 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   input_key_materialhashr
   )infolength)hkdfHkdfr   sha512expand)r   r#   r   kdfkeyr    r    r!   expand_argon2_secretK   s   r.   c                 C   sf   |st  }|| d | dd }t|dksJ |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!   pkm_from_argon2_secret[   s   	
r/   )N)
r   typingr   r   r(   strbytesr"   r.   r/   r    r    r    r!   <module>   s     *(