
    af                         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Zdded	edee         defd
Zddeded	edee         def
dZ	dS )    N)Optionalemailpasswordsaltreturnc           
      ~   |sWt          j                    }|                    |                     d                     |                                dd         }t          |          dk    sJ t          j                            |                    d          |ddddt          j        j	        j
        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pkms        F/var/www/html/env/lib/python3.11/site-packages/autobahn/xbr/_wallet.pystretch_argon2_secretr#   !   s    ,  N	f%%&&&xxzz#2#t99????


*
*v&&"% + 	 	C J    r!   contextc                 v    t          j        || t          j                  }|                    |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keys        r"   expand_argon2_secretr1   K   s6     )#GN
K
K
KC
**'"*
-
-CJr$   c                 T   |sWt          j                    }|                    |                     d                     |                                dd         }t          |          dk    sJ |                    d          }t          | ||          }t          |||          }|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#   r1   )r   r   r%   r   r    r!   r0   s          r"   pkm_from_argon2_secretr3   [   s      N	f%%&&&xxzz#2#t99????nnV$$G
ehT
J
J
JC
3d
C
C
CCJr$   )N)
r   typingr   r   r+   strbytesr#   r1   r3    r$   r"   <module>r8      s   6         ' ' ' '8E? 'V[ ' ' ' 'T e e 8E? V[      #  s (SX/ ej      r$   