ó
írUc           @   sG   d  Z  d d l Z d d l m Z m Z d   Z d   Z d   Z d S(   s0   Functions that load and write PEM-encoded files.i˙˙˙˙N(   t   bt   is_bytesc         C   s<   t  |   r |  j d  }  n  t d |   t d |   f S(   s/   
    Returns the start and end PEM markers
    s   utf-8s   -----BEGIN %s-----s   -----END %s-----(   R   t   decodeR    (   t
   pem_marker(    (    s(   c:\Python27\lib\site-packages\rsa\pem.pyt   _markers   s    c         C   s  t  |  \ } } g  } t } xŞ |  j   D] } | j   } | sI q+ n  | | k rz | rn t d |   n  t } q+ n  | s q+ n  | r˘ | | k r˘ t } Pn  t d  | k rş q+ n  | j |  q+ W| sä t d |   n  | rý t d |   n  t d  j |  } t	 j
 |  S(   sĐ  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.

    s   Seen start marker "%s" twicet   :s   No PEM start marker "%s" founds   No PEM end marker "%s" foundt    (   R   t   Falset
   splitlinest   stript
   ValueErrort   TrueR    t   appendt   joint   base64t   decodestring(   t   contentsR   t	   pem_startt   pem_endt	   pem_linest   in_pem_partt   linet   pem(    (    s(   c:\Python27\lib\site-packages\rsa\pem.pyt   load_pem!   s4    c         C   sś   t  |  \ } } t j |   j t d  t d   } | g } x> t d t |  d  D]$ } | | | d !} | j |  q[ W| j |  | j t d   t d  j |  S(   s_  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.

    s   
R   i    i@   (	   R   R   t   encodestringt   replaceR    t   ranget   lenR   R   (   R   R   R   R   t   b64R   t   block_startt   block(    (    s(   c:\Python27\lib\site-packages\rsa\pem.pyt   save_pem_   s    '	(   t   __doc__R   t   rsa._compatR    R   R   R   R   (    (    (    s(   c:\Python27\lib\site-packages\rsa\pem.pyt   <module>   s
   		>