o
    yK™]q  ã                   @   s|   d Z ddlZddlZe d¡Ze d¡Ze d¡Ze d¡Zddd	„Z	d
d„ Z
dd„ Zdd„ Zdd„ Zddd„Zdd„ ZdS )an  
Miscellaneous utility functions for working with tor.

.. versionadded:: 1.2.0

**Module Overview:**

::

  is_valid_fingerprint - checks if a string is a valid tor relay fingerprint
  is_valid_nickname - checks if a string is a valid tor relay nickname
  is_valid_circuit_id - checks if a string is a valid tor circuit id
  is_valid_stream_id - checks if a string is a valid tor stream id
  is_valid_connection_id - checks if a string is a valid tor connection id
  is_valid_hidden_service_address - checks if a string is a valid hidden service address
  is_hex_digits - checks if a string is only made up of hex digits
é    Nz^[a-zA-Z0-9]{1,19}$z^[a-zA-Z0-9]{1,16}$z^[a-z2-7]{16}$z^[a-z2-7]{56}$Fc                 C   s`   t | tƒrtjj | ¡} z|r | r| d dkrW dS | dd… } t| dƒW S  ty/   Y dS w )a_  
  Checks if a string is a properly formatted relay fingerprint. This checks for
  a '$' prefix if check_prefix is true, otherwise this only validates the hex
  digits.

  :param str entry: string to be checked
  :param bool check_prefix: checks for a '$' prefix

  :returns: **True** if the string could be a relay fingerprint, **False** otherwise
  r   ú$Fé   Né(   )Ú
isinstanceÚbytesÚstemÚutilÚ	str_toolsÚ_to_unicodeÚis_hex_digitsÚ	TypeError)ÚentryZcheck_prefix© r   ú5/usr/lib/python3/dist-packages/stem/util/tor_tools.pyÚis_valid_fingerprint0   s   
ÿr   c                 C   ó>   t | tƒrtjj | ¡} ztt | ¡ƒW S  t	y   Y dS w )z·
  Checks if a string is a valid format for being a nickname.

  :param str entry: string to be checked

  :returns: **True** if the string could be a nickname, **False** otherwise
  F)
r   r   r   r   r	   r
   ÚboolÚNICKNAME_PATTERNÚmatchr   ©r   r   r   r   Úis_valid_nicknameK   s   
	ÿr   c                 C   r   )z™
  Checks if a string is a valid format for being a circuit identifier.

  :returns: **True** if the string could be a circuit id, **False** otherwise
  F)
r   r   r   r   r	   r
   r   ÚCIRC_ID_PATTERNr   r   r   r   r   r   Úis_valid_circuit_id]   s   
ÿr   c                 C   ó   t | ƒS )zï
  Checks if a string is a valid format for being a stream identifier.
  Currently, this is just an alias to :func:`~stem.util.tor_tools.is_valid_circuit_id`.

  :returns: **True** if the string could be a stream id, **False** otherwise
  ©r   r   r   r   r   Úis_valid_stream_idm   ó   r   c                 C   r   )z÷
  Checks if a string is a valid format for being a connection identifier.
  Currently, this is just an alias to :func:`~stem.util.tor_tools.is_valid_circuit_id`.

  :returns: **True** if the string could be a connection id, **False** otherwise
  r   r   r   r   r   Úis_valid_connection_idx   r   r   c                 C   s¬   t | tƒrtjj | ¡} |du rd}nt |tƒr|g}nt |ttfƒs,t	dt
|ƒj ƒ‚zd|v r;tt | ¡ƒr;W dS d|v rItt | ¡ƒrIW dS W dS  tyU   Y dS w )a  
  Checks if a string is a valid format for being a hidden service address (not
  including the '.onion' suffix).

  .. versionchanged:: 1.8.0
     Added the **version** argument, and responds with **True** if a version 3
     hidden service address rather than just version 2 addresses.

  :param int,list version: versions to check for, if unspecified either v2 or v3
    hidden service address will provide **True**

  :returns: **True** if the string could be a hidden service address, **False**
    otherwise
  N)é   é   z;Hidden service version must be an integer or list, not a %sr   Tr   F)r   r   r   r   r	   r
   ÚintÚlistÚtupleÚ
ValueErrorÚtypeÚ__name__r   ÚHS_V2_ADDRESS_PATTERNr   ÚHS_V3_ADDRESS_PATTERNr   )r   Úversionr   r   r   Úis_valid_hidden_service_addressƒ   s"   

ÿr)   c              	   C   s<   zt | ƒ|kr
W dS t| dƒ W dS  ttfy   Y dS w )a#  
  Checks if a string is the given number of hex digits. Digits represented by
  letters are case insensitive.

  :param str entry: string to be checked
  :param int count: number of hex digits to be checked for

  :returns: **True** if the given number of hex digits, **False** otherwise
  Fé   T)Úlenr    r#   r   )r   Úcountr   r   r   r   ©   s   
ÿr   )F)N)Ú__doc__ÚreZstem.util.str_toolsr   Úcompiler   r   r&   r'   r   r   r   r   r   r)   r   r   r   r   r   Ú<module>   s   





&