U
    %Y                     @   s   d dl Z d dlmZ d dlZd dlZd dlmZmZ dd Zdd Z	d"dd	Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )#    N)sha256)	text_typebinary_typec                 C   s:   | d krd S t | tr | dS t | tr.| S tdd S Nutf-8z!Must be a string or bytes object.)
isinstancer   encoder   	TypeErrorZstring_or_bytes r   3/usr/lib/python3/dist-packages/pymacaroons/utils.pyconvert_to_bytes	   s    


r   c                 C   s:   | d krd S t | tr| S t | tr.| dS tdd S r   )r   r   r   decoder	   r
   r   r   r   convert_to_string   s    


r   c                 C   sX   |d krd}t | }t|}||kr4t|d | S ||k rPt|d||   S | S d S )N        )	bytearraylenbytes)Zbyte_stringsizeZ
byte_arrayZlengthr   r   r   truncate_or_pad   s    r   c                 C   s
   t d| S )Ns   macaroons-key-generator)hmac_digest)keyr   r   r   generate_derived_key,   s    r   c                 C   s   t j| |td S )N)msgZ	digestmod)hmacnewr   Zdigest)r   datar   r   r   r   0   s
    r   c                 C   s   t | |}t|S N)r   binasciiZhexlify)r   r   digr   r   r   hmac_hex8   s    
r!   c                 C   s   t | }t||S r   )r   r!   )r   Z
identifierZderived_keyr   r   r   create_initial_signature=   s    r"   c                 C   s"   t | |}t | |}t| || S r   )r   r!   )r   Zdata1Zdata2Zhash1Zhash2r   r   r   hmac_concatB   s    

r#   c                 C   s
   t | |S r   )r!   )	signatureZ	predicater   r   r   sign_first_party_caveatH   s    r%   c                 C   s   t | ||S r   )r#   )r$   Zverification_idZ	caveat_idr   r   r   sign_third_party_caveatL   s    r&   c                 C   sH   t | t |krdS d}t| |D ]\}}|t|t|A O }q"|dkS )aE  
    Returns True if the two strings are equal, False otherwise.

    The time taken is independent of the number of characters that match.

    For the sake of simplicity, this function executes in constant time only
    when the two strings have the same length. It short-circuits when they
    have different lengths.
    Fr   )r   zipord)Zval1Zval2resultxyr   r   r   equalsP   s    
r,   c                 C   s   | dt |  d   S )zAdd padding to base64 encoded bytes.

    Padding can be removed when sending the messages.

    @param b bytes to be padded.
    @return a padded bytes.
       =   )r   br   r   r   add_base64_paddingb   s    r1   c                 C   s&   dsd| krt | S tt| S d S )N_-)raw_urlsafe_b64decodebase64Z	b64decoder1   sr   r   r   raw_b64decodem   s    r8   c                 C   s   t t| dS )ztBase64 decode with added padding and conversion to bytes.

    @param s string decode
    @return bytes decoded
    r   )r5   Zurlsafe_b64decoder1   r   r6   r   r   r   r4   t   s    r4   c                 C   s   t | dS )z^Base64 encode with padding removed.

    @param s string decode
    @return bytes decoded
    r-   )r5   Zurlsafe_b64encoderstripr/   r   r   r   raw_urlsafe_b64encode}   s    r:   )N)r5   Zhashlibr   r   r   Zsixr   r   r   r   r   r   r   r!   r"   r#   r%   r&   r,   r1   r8   r4   r:   r   r   r   r   <module>   s&   
	