a
    ‘dbÑ  ã                   @   s8   d Z ddlZddlmZmZ dd„ Zdd„ Zdd	„ ZdS )
z0Functions that load and write PEM-encoded files.é    N)ÚbÚis_bytesc                 C   s*   t | ƒr|  d¡} td|  ƒtd|  ƒfS )z/
    Returns the start and end PEM markers
    zutf-8z-----BEGIN %s-----z-----END %s-----)r   Údecoder   )Ú
pem_marker© r   úX/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/pem.pyÚ_markers   s
    


ÿr   c                 C   sÊ   t | ƒs|  d¡} t|ƒ\}}g }d}|  ¡ D ]b}| ¡ }|s@q.||kr^|rXtd| ƒ‚d}q.|sdq.|rx||krxd} q’tdƒ|v r†q.| |¡ q.|s¢td| ƒ‚|r²td| ƒ‚tdƒ |¡}t	 
|¡S )	aÑ  Loads a PEM file.

    :param contents: the contents of the file to interpret
    :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
        when your file has '-----BEGIN RSA PRIVATE KEY-----' and
        '-----END RSA PRIVATE KEY-----' markers.

    :return: the base64-decoded content between the start and end markers.

    @raise ValueError: when the content is invalid, for example when the start
        marker cannot be found.

    ÚasciiFzSeen start marker "%s" twiceTú:zNo PEM start marker "%s" foundzNo PEM end marker "%s" foundÚ )r   Úencoder   Ú
splitlinesÚstripÚ
ValueErrorr   ÚappendÚjoinÚbase64Ústandard_b64decode)Úcontentsr   Ú	pem_startÚpem_endÚ	pem_linesZin_pem_partÚlineZpemr   r   r   Úload_pem#   s8    
r   c                 C   s‚   t |ƒ\}}t | ¡ tdƒtdƒ¡}|g}tdt|ƒdƒD ]}|||d … }| |¡ q<| |¡ | tdƒ¡ tdƒ |¡S )a`  Saves a PEM file.

    :param contents: the contents to encode in PEM format
    :param pem_marker: the marker of the PEM content, such as 'RSA PRIVATE KEY'
        when your file has '-----BEGIN RSA PRIVATE KEY-----' and
        '-----END RSA PRIVATE KEY-----' markers.

    :return: the base64-encoded content between the start and end markers.

    Ú
r   r   é@   )	r   r   Ústandard_b64encodeÚreplacer   ÚrangeÚlenr   r   )r   r   r   r   Zb64r   Zblock_startÚblockr   r   r   Úsave_peme   s    
r!   )Ú__doc__r   Zrsa._compatr   r   r   r   r!   r   r   r   r   Ú<module>   s
   B