U
    ÈZj}  ã                   @   s    d dgZ ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddd „Zdd„ Zddd„ZdS )ÚencodeÚdecodeé    N)Ú
a2b_base64Ú
b2a_base64ÚhexlifyÚ	unhexlify)ÚMD5)ÚpadÚunpad)ÚDESÚDES3ÚAES)ÚPBKDF1)Úget_random_bytes)ÚtobytesÚtostrc           	         sÐ   |dkrt }d| }|r„|dƒ}t||ddtƒ}|t|| |ddtƒ7 }t |tj|¡}|dtt|ƒ ¡ ƒ 7 }| 	t
ˆ |jƒ¡‰ n|dk	r”tdƒ‚‡ fdd	„td
tˆ ƒdƒD ƒ}|d |¡7 }|d| 7 }|S )a4  Encode a piece of binary data into PEM format.

    Args:
      data (byte string):
        The piece of binary data to encode.
      marker (string):
        The marker for the PEM block (e.g. "PUBLIC KEY").
        Note that there is no official master list for all allowed markers.
        Still, you can refer to the OpenSSL_ source code.
      passphrase (byte string):
        If given, the PEM block will be encrypted. The key is derived from
        the passphrase.
      randfunc (callable):
        Random number generation function; it accepts an integer N and returns
        a byte string of random data, N bytes long. If not given, a new one is
        instantiated.

    Returns:
      The PEM block, as a string.

    .. _OpenSSL: https://github.com/openssl/openssl/blob/master/include/openssl/pem.h
    Nz-----BEGIN %s-----
é   é   é   z2Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,%s

zEmpty passwordc                    s$   g | ]}t tˆ ||d  … ƒƒ‘qS )é0   )r   r   )Ú.0Úi©Údata© úP/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/Crypto/IO/PEM.pyÚ
<listcomp>Z   s   ÿzencode.<locals>.<listcomp>r   r   Ú z-----END %s-----)r   r   r   r   ÚnewÚMODE_CBCr   r   ÚupperZencryptr	   Ú
block_sizeÚ
ValueErrorÚrangeÚlenÚjoin)	r   ÚmarkerÚ
passphraseZrandfuncÚoutÚsaltÚkeyZobjencÚchunksr   r   r   r   /   s(    ÿ
ÿc                 C   sV   dg}|d d }t |ƒD ](}t |d |  | ¡ ¡ }| |¡ qd |¡d |… S )Nó    é   r   éÿÿÿÿ)r#   r   r   ÚdigestÚappendr%   )r   r)   Zkey_lenÚdÚmÚ_Úndr   r   r   Ú_EVP_BytesToKeya   s    r5   c                 C   sr  t  d¡}| | ¡}|s tdƒ‚| d¡}t  d¡}| | ¡}|rP| d¡|krXtdƒ‚|  dd¡ ¡ }t|ƒdk r|td	ƒ‚|d  	d
¡�r|s˜tdƒ‚|d  d¡}t|ƒdks¾|d dkrÆtdƒ‚|d  d¡\}}t
t|ƒƒ}d}	|dk�rt||dƒ}
t |
tj|¡}nþ|dk�r8t||dƒ}
t |
tj|¡}nÖ|dk�rht||dd… dƒ}
t |
tj|¡}n¦|dk�r˜t||dd… dƒ}
t |
tj|¡}nv|dk�rÈt||dd… dƒ}
t |
tj|¡}nF| ¡ dk�rt||dd… dƒ}
tj|
tj|d�}d}	ntd | ƒ‚|dd… }nd}td |dd!… ¡ƒ}d}|�rh|	�rZt| |¡|jƒ}n
| |¡}d}|||fS )"a  Decode a PEM block into binary.

    Args:
      pem_data (string):
        The PEM block.
      passphrase (byte string):
        If given and the PEM block is encrypted,
        the key will be derived from the passphrase.

    Returns:
      A tuple with the binary data, the marker string, and a boolean to
      indicate if decryption was performed.

    Raises:
      ValueError: if decoding fails, if the PEM file is encrypted and no passphrase has
                  been provided or if the passphrase is incorrect.
    z\s*-----BEGIN (.*)-----\s+zNot a valid PEM pre boundaryr   z-----END (.*)-----\s*$zNot a valid PEM post boundaryú r   é   z%A PEM file must have at least 3 lineszProc-Type:4,ENCRYPTEDz-PEM is encrypted, but no passphrase availableé   ú:r   zDEK-Infoz$PEM encryption format not supported.ú,TzDES-CBCr   zDES-EDE3-CBCé   zAES-128-CBCNr   zAES-192-CBCzAES-256-CBCé    zid-aes256-gcm)ÚnonceFz(Unsupport PEM encryption algorithm (%s).r.   )ÚreÚcompileÚmatchr"   ÚgroupÚsearchÚreplaceÚsplitr$   Ú
startswithr   r   r5   r   r   r   r   r   ÚlowerZMODE_GCMr   r%   r
   Zdecryptr!   )Zpem_datar'   Úrr2   r&   ÚlinesZDEKÚalgor)   Úpaddingr*   Zobjdecr   Zenc_flagr   r   r   r   j   sf    










)NN)N)Ú__all__r>   Úbinasciir   r   r   r   ZCrypto.Hashr   ZCrypto.Util.Paddingr	   r
   ZCrypto.Cipherr   r   r   ZCrypto.Protocol.KDFr   ZCrypto.Randomr   ZCrypto.Util.py3compatr   r   r   r5   r   r   r   r   r   Ú<module>"   s   
2	