3 �k�ZE � @ s� d dl Z d dlZd dlZddlmZmZmZ yd dlmZm Z W n e k rT Y nX dd� Zdd� Zd d � Z dd� Zd d� Zdd� Zdd� Zdd� Zdd� Zdd� Zdd� ZdS )� N� )�binary_type�bytes_from_int� text_type)�decode_dss_signature�encode_dss_signaturec C s. t | t�r| jd�S t | t�r"| S td��d S )Nzutf-8zExpected a string value)� isinstancer �decoder � TypeError)�value� r �/usr/lib/python3.6/utils.py� force_unicode s r c C s. t | t�r| jd�S t | t�r"| S td��d S )Nzutf-8zExpected a string value)r r �encoder r )r r r r �force_bytes s r c C sB t | t�r| jd�} t| �d }|dkr8| dd| 7 } tj| �S )N�ascii� r � =)r r r �len�base64Zurlsafe_b64decode)�inputZremr r r �base64url_decode! s r c C s t j| �jdd�S )Nr � )r Zurlsafe_b64encode�replace)r r r r �base64url_encode- s r c C s0 | dk rt d��t| �}t|�dkr(d}t|�S )Nr zMust be a positive integer� )� ValueErrorr r r )�val� int_bytesr r r �to_base64url_uint1 s r c C sJ t | t�r| jd�} t| �}tjdt|� |�}tdjdd� |D ��d�S )Nr z%sB� c S s g | ]}d | �qS )z%02xr )�.0Zbyter r r � <listcomp>D s z'from_base64url_uint.<locals>.<listcomp>� ) r r r r �struct�unpackr �int�join)r �dataZbufr r r �from_base64url_uint= s r) c C sV |s| S y| j � }|j|� W n2 ttfk rP } ztd| ��W Y d d }~X nX |S )Nz-original and updates must be a dictionary: %s)�copy�update�AttributeErrorr r )ZoriginalZupdatesZmerged_options�er r r � merge_dictG s r. c C s$ dd| | f }t j|jd��}|S )Nz%0*x� r )�binasciiZa2b_hexr )Znum� num_bytesZ padded_hexZ big_endianr r r �number_to_bytesT s r2 c C s t tj| �d�S )Nr# )r&