o
    iP                     @   s   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZmZ ddlmZmZmZ ddlmZ dZdd	d
Zdd ZedddZdddZdS )z'
Password generation for the Notebook.
    )contextmanagerN)
cast_bytesstr_to_bytescast_unicode)ConfigConfigFileNotFoundJSONFileConfigLoader)jupyter_config_dir   argon2c           	      C   s   | du r%t dD ]}td}td}||kr|}  n	td qtd|dkrEdd	lm} |d
ddd}|| }d|t|dfS t	
|}tdt dt d}|t| dt|d  d||| fS )a  Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.password` to
    the generated string.

    Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`, or 'argon2').

    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.

    Examples
    --------
    >>> passwd('mypassword', algorithm='sha1')
    'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

    N   zEnter password: zVerify password: zPasswords do not match.z'No matching passwords found. Giving up.r   r   )PasswordHasheri (  
      )memory_cost	time_costparallelism:ascii   0xutf-8)rangegetpassprint
ValueErrorr   r   hashjoinr   hashlibnewrandomgetrandbitssalt_lenupdater   r   	hexdigest)	
passphrase	algorithmip0p1r   phhsalt r.   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/auth/security.pypasswd   s,   




r0   c              	   C   s   |  dr)ddl}ddl}| }z|| dd |W S  |jjy(   Y dS w z| dd\}}}W n tt	fy@   Y dS w zt
|}W n
 tyR   Y dS w t|dkr[dS |t|dt|d	  | |kS )
a  Verify that a given passphrase matches its hashed version.

    Parameters
    ----------
    hashed_passphrase : str
        Hashed password, in the format returned by `passwd`.
    passphrase : str
        Passphrase to validate.

    Returns
    -------
    valid : bool
        True if the passphrase matches the hash.

    Examples
    --------
    >>> from notebook.auth.security import passwd_check
    >>> passwd_check('argon2:...', 'mypassword')
    True

    >>> passwd_check('argon2:...', 'otherpassword')
    False

    >>> passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
    ...              'mypassword')
    True
    zargon2:r   N   Fr      r   r   )
startswithr   argon2.exceptionsr   verify
exceptionsVerificationErrorsplitr   	TypeErrorr   r    lenr$   r   r%   )hashed_passphraser&   r   r+   r'   r-   	pw_digestr,   r.   r.   r/   passwd_checkO   s.   
r=     c              
   c   s   | du rt jt d} t jt j| dd tt j| t j| }z| }W n t	y7   t
 }Y nw |V  t| ddd}|ttj|dd	 W d   n1 sYw   Y  z	t | | W dS  ty } zt }td
|  d| t W Y d}~dS d}~ww )zContext manager that can be used to modify a config object

    On exit of the context manager, the config will be written back to disk,
    by default with user-only (600) permissions.
    Nzjupyter_notebook_config.jsonT)exist_okwutf8)encodingr2   )indentzFailed to set permissions on z:
)ospathr   r	   makedirsdirnamer   basenameload_configr   r   openwriter   jsondumpschmod	Exception	traceback
format_excwarningswarnRuntimeWarning)config_filemodeloaderconfigfetbr.   r.   r/   persist_config   s.   
r\   c                 C   s>   t | }t|}||j_W d   dS 1 sw   Y  dS )zCAsk user for password, store it in notebook json configuration fileN)r0   r\   NotebookApppassword)r^   rU   hashed_passwordrX   r.   r.   r/   set_password   s   

"r`   )Nr   )Nr>   )NN)__doc__
contextlibr   r   r   rL   rD   r!   rP   rR   ipython_genutils.py3compatr   r   r   traitlets.configr   r   r   jupyter_core.pathsr	   r#   r0   r=   r\   r`   r.   r.   r.   r/   <module>   s$    
86