a
    ވa                     @   s   d 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	m
Z
 e
dZe
dZdddZdd	d
Zdd ZdddZg dZdS )z10MQ authentication related functions and classes.    N)bytesunicodebuz#   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE **Secret** Certificate
#   DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions.

a
  #   ****  Generated on {0} by pyzmq  ****
#   ZeroMQ CURVE Public Certificate
#   Exchange securely, or use a secure mechanism to verify the contents
#   of this file after exchange. Store public certificates in your home
#   directory, in the .curve subdirectory.

utf-8c           	      C   s  t |tr||}t |tr(||}tj| ddd}||tj  |t	d |r|
 D ]F\}}t |tr||}t |tr||}|t	d|| qj|t	d |t	d| |r|t	d| W d	   n1 s0    Y  d	S )
zCreate a certificate filewutf8)encodingz	metadata
z    {0} = {1}
zcurve
z    public-key = "{0}"
z    secret-key = "{0}"
N)
isinstancer   decodeioopenwriteformatdatetimenowr   items)	Zkey_filenameZbanner
public_key
secret_keymetadatar	   fkv r   -lib/python3.9/site-packages/zmq/auth/certs.py_write_key_file"   s$    







r   c           	      C   sj   t  \}}tj| |}d|}d|}tj }t|t	|| t|t
||||d ||fS )zeCreate zmq certificates.

    Returns the file paths to the public and secret certificate files.
    z{0}.key_secretz{0}.key)r   r   )zmqZcurve_keypairospathjoinr   r   r   r   _cert_public_banner_cert_secret_banner)	Zkey_dirnamer   r   r   base_filenameZsecret_key_fileZpublic_key_filer   r   r   r   create_certificates=   s    


r$   c                 C   s   d}d}t j| s"td| t| dz}|D ]d}| }|drJq2|drj|ddd d}|d	r|ddd d}|r2|r2 qq2W d   n1 s0    Y  |du rt	d
|  ||fS )a  Load public and secret key from a zmq certificate.

    Returns (public_key, secret_key)

    If the certificate file only contains the public key,
    secret_key will be None.

    If there is no public key found in the file, ValueError will be raised.
    NzInvalid certificate file: {0}rb   #s
   public-key   =   s    	'"s
   secret-keyzNo public key found in %s)
r   r   existsIOErrorr   r   strip
startswithsplit
ValueError)filenamer   r   r   liner   r   r   load_certificateU   s$    



$r1   .c                 C   s\   i }t j| std| t j| d}t|}|D ]}t|\}}|r:d||< q:|S )z5Load public keys from all certificates in a directoryz"Invalid certificate directory: {0}z*.keyT)r   r   isdirr*   r   r   globr1   )Z	directoryZcertsZglob_stringZ
cert_filesZ	cert_filer   _r   r   r   load_certificatesv   s    

r6   )r$   r1   r6   )NNr   )N)r2   )__doc__r   r4   r   r   r   Zzmq.utils.strtypesr   r   r   r   r!   r    r   r$   r1   r6   __all__r   r   r   r   <module>   s$    

!
