
    hq                         d Z ddlZddlZ ej                  d      Z ej                  d      Z ej                  d      Z ej                  d      ZddZ	d Z
d	 Zd
 Zd ZddZd Zy)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}$c                     t        | t              r)t        j                  j                  j                  |       } 	 |r| r| d   dk7  ry| dd } t        | d      S # t        $ r Y yw xY 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)entrycheck_prefixs     ]/var/www/betterdocs.net/sherlock_api/venv/lib/python3.12/site-packages/stem/util/tor_tools.pyis_valid_fingerprintr   0   sj     uII++E2E	eAh#oABie##	 s   A A 	A%$A%c                     t        | t              r)t        j                  j                  j                  |       } 	 t        t        j                  |             S # t        $ r Y yw xY 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   s    r   is_valid_nicknamer   K   sV     uII++E2E &&u-..	    A 	A%$A%c                     t        | t              r)t        j                  j                  j                  |       } 	 t        t        j                  |             S # t        $ r Y yw xY w)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   s    r   is_valid_circuit_idr   ]   sU     uII++E2E%%e,--	 r   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   s    r   is_valid_stream_idr   m        
U	##    c                     t        |       S )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   s    r   is_valid_connection_idr#   x   r    r!   c                    t        | t              r)t        j                  j                  j                  |       } |d}nKt        |t              r|g}n7t        |t        t        f      s!t        dt        |      j                  z        	 d|v rt        t        j                  |             ryd|v rt        t        j                  |             ryy# t         $ r Y yw xY 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
  )      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   versions     r   is_valid_hidden_service_addressr0      s      uII++E2E_G'3iGge}-
RUYZaUbUkUkk
ll	G|288?@G|288?@	 s   "C ."C 	CCc                 f    	 t        |       |k7  ryt        | d       y# t        t        f$ r Y yw xY 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   counts     r   r   r      s9    
5zUrN
i	  s     00)F)N)__doc__restem.util.str_toolsr	   compiler   r   r-   r.   r   r   r   r   r#   r0   r    r!   r   <module>r:      s   $ 
  2::34 "**23 #

#34 "

#34 6$ $$#Lr!   