HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberPanel/lib/python3.10/site-packages/ecdsa/__pycache__/util.cpython-310.pyc
o

��hVF�@s�dZddlmZddlZddlZddlZddlZddlmZddl	m
Z
mZmZddl
mZddlmZd	Zeje�Zd
ZdZejdkrId
d�Zndd�ZejdkrWdd�Zndd�Zdd�ZdDdd�ZGdd�d�Zdd�Zdd�Zdd�Zefd d!�Zefd"d#�Z d$d%�Z!d&d'�Z"d(d)�Z#d*d+�Z$d,d-�Z%d.d/�Z&d0d1�Z'd2d3�Z(d4d5�Z)d6d7�Z*d8d9�Z+d:d;�Z,Gd<d=�d=e-�Z.d>d?�Z/d@dA�Z0dBdC�Z1dS)Ea�
This module includes some utility functions.

The methods most typically used are the sigencode and sigdecode functions
to be used with :func:`~ecdsa.keys.SigningKey.sign` and
:func:`~ecdsa.keys.VerifyingKey.verify`
respectively. See the :func:`sigencode_strings`, :func:`sigdecode_string`,
:func:`sigencode_der`, :func:`sigencode_strings_canonize`,
:func:`sigencode_string_canonize`, :func:`sigencode_der_canonize`,
:func:`sigdecode_strings`, :func:`sigdecode_string`, and
:func:`sigdecode_der` functions.
�)�divisionN)�sha256)�PY2�int2byte�next�)�der)�normalise_bytes)r�iHi='r
r)r��r�)rrrr�
)rcCs&tt�|d��dd��t|�d�S)�-Convert a bytestring to string of 0's and 1's�bigr
N�)�bin�int�
from_bytes�zfill�len��ent_256�r�@/usr/local/CyberPanel/lib/python3.10/site-packages/ecdsa/util.py�entropy_to_bits4s&rcCsd�dd�|D��S)r�css*�|]}tt|��dd��d�VqdS)r
Nr)r�ordr)�.0�xrrr�	<genexpr><s�(z"entropy_to_bits.<locals>.<genexpr>)�joinrrrrr:s)r
�cCstt|��dS)Nr
)rr�rrrr�
bit_lengthA�r$cCs|��pdS�Nr)r$r#rrrr$F�cCsdtd|�dS)Nrz%xr
)r)�orderrrr�orderlenJsr)cCsv|dksJ�|dur
tj}t|d�}|dd}	||�}t|�}t|d|�dd�d}d|kr9|kr:|Sq)aReturn a random integer k such that 1 <= k < order, uniformly
    distributed across that range. Worst case should be a mean of 2 loops at
    (2**k)+2.

    Note that this function is not declared to be forwards-compatible: we may
    change the behavior in future releases. The entropy= argument (which
    should get a callable that behaves like os.urandom) can be used to
    achieve stability within a given release (for repeatable unit tests), but
    should not be used as a long-term-compatible key generation algorithm.
    rNr
rT)�baser)�os�urandomr$rr)r(�entropy�upper_2�	upper_256r�ent_2�rand_numrrr�	randrangeNs��r2c@s$eZdZdd�Zdd�Zdd�ZdS)�PRNGcCs|�|�|_dS�N)�block_generator�	generator)�self�seedrrr�__init__lr%z
PRNG.__init__cs,�fdd�t|�D�}trd�|�St|�S)Ncsg|]}t�j��qSr)rr6)r�i�r7rr�
<listcomp>psz!PRNG.__call__.<locals>.<listcomp>r)�rangerr!�bytes)r7�numbytes�arr;r�__call__os
z
PRNG.__call__ccs6�d}	td||f�����D]}|Vq|d7}q)NrTz
prng-%d-%sr)r�encode�digest)r7r8�counter�byterrrr5ws����zPRNG.block_generatorN)�__name__�
__module__�__qualname__r9rAr5rrrrr3fsr3cCsXt|�dt|��}tt�|�d�|dd}d|kr"|ks*nJd||f��|S)Nr
�r)r3r)r�binascii�hexlify)r8r(r*�numberrrr�%randrange_from_seed__overshoot_modulo�s$rMcCsd|>dSr&r)�numbitsrrr�lsb_of_ones�r'rOcCs2tt�|dd�d�}|d}|d}|||fS)Nrr
r)r�math�log)r(�bitsr>�	extrabitsrrr�bits_and_bytes�s
rTcCsvt|�\}}}|r
|d7}||���d|�}d|t|�|}dtt�|�d�}d|kr6|ks9J�J�|S)Nr�rI)rTrCrrrJrK)r8r(�hashmodrR�_bytesrSr*rLrrr�#randrange_from_seed__truncate_bytes�srXcCs�tt�|dd�d�}|dd}||���d|�}d|t|�|}d||}|r@tt|d�t|�@�|dd�}dtt�	|�d�}d|krU|ksXJ�J�|S)Nrr
r"rrUrrI)
rrPrQrCrrrrOrJrK)r8r(rVrR�maxbytesr*�topbitsrLrrr�"randrange_from_seed__truncate_bits�s$r[cCsr|dksJ�t|�\}}}t|�}	d}|r"tt|d��t|�@�}t|||��d}d|kr7|kr8|Sq)NrT�)rTr3rrrO�string_to_number)r8r(rRr>rS�generate�	extrabyte�guessrrr� randrange_from_seed__trytryagain�s��racCsNt|�}dtd|�d}t�||���}t|�|ks%Jt|�|f��|S�Nz%0r
r)r)�strrJ�	unhexlifyrBr��numr(�l�fmt_str�stringrrr�number_to_string�s
rjcCs:t|�}dtd|�d}t�||���}|d|�Srb)r)rcrJrdrBrerrr�number_to_string_crop�srkcCstt�|�d�S�NrI)rrJrK)rirrrr]�r%r]cCs4t|�}t|�|ksJt|�|f��tt�|�d�Srl)r)rrrJrK)rir(rgrrr�string_to_number_fixedlen�srmcCst||�}t||�}||fS)aZ
    Encode the signature to a pair of strings in a tuple

    Encodes signature into raw encoding (:term:`raw encoding`) with the
    ``r`` and ``s`` parts of the signature encoded separately.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: tuple(bytes, bytes)
    )rj��r�sr(�r_str�s_strrrr�sigencode_strings�s

rscCst|||�\}}||S)a�
    Encode the signature to raw format (:term:`raw encoding`)

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: bytes
    )rsrnrrr�sigencode_stringsrtcCst�t�|�t�|��S)a�
    Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.

    Encodes the signature to the following :term:`ASN.1` structure::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: DER encoding of ECDSA signature
    :rtype: bytes
    )r�encode_sequence�encode_integer�rorpr(rrr�
sigencode_dersrxcCs||dkr
||}|S)a8
    Internal function for ensuring that the ``s`` value of a signature is in
    the "canonical" format.

    :param int s: the second parameter of ECDSA signature
    :param int order: the order of the curve over which the signatures was
        computed

    :return: canonical value of s
    :rtype: int
    r
r)rpr(rrr�	_canonize3srycC�t||�}t|||�S)a
    Encode the signature to a pair of strings in a tuple

    Encodes signature into raw encoding (:term:`raw encoding`) with the
    ``r`` and ``s`` parts of the signature encoded separately.

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: tuple(bytes, bytes)
    )ryrsrwrrr�sigencode_strings_canonizeDs
r{cCrz)aw
    Encode the signature to raw format (:term:`raw encoding`)

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: bytes
    )ryrtrwrrr�sigencode_string_canonize^s
r|cCrz)a9
    Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    Encodes the signature to the following :term:`ASN.1` structure::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: DER encoding of ECDSA signature
    :rtype: bytes
    )ryrxrwrrr�sigencode_der_canonizeus
r}c@seZdZdZdS)�MalformedSignatureaB
    Raised by decoding functions when the signature is malformed.

    Malformed in this context means that the relevant strings or integers
    do not match what a signature over provided curve would create. Either
    because the byte strings have incorrect lengths or because the encoded
    values are too large.
    N)rFrGrH�__doc__rrrrr~�s	r~cCsdt|�}t|�}t|�d|kstd�d|t|����t|d|�|�}t||d�|�}||fS)a�
    Decoder for :term:`raw encoding`  of ECDSA signatures.

    raw encoding is a simple concatenation of the two integers that comprise
    the signature, with each encoded using the same amount of bytes depending
    on curve size/order.

    It's expected that this function will be used as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param signature: encoded signature
    :type signature: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r
zWInvalid length of signature, expected {0} bytes long, provided string is {1} bytes longN)r	r)rr~�formatrm)�	signaturer(rgrorprrr�sigdecode_string�s�r�cCs�t|�dkstd�t|����|\}}t|�}t|�}t|�}t|�|ks/td�|t|����t|�|ks?td�|t|����t||�}t||�}||fS)a�
    Decode the signature from two strings.

    First string needs to be a big endian encoding of ``r``, second needs to
    be a big endian encoding of the ``s`` parameter of an ECDSA signature.

    It's expected that this function will be used as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param list rs_strings: list of two bytes-like objects, each encoding one
        parameter of signature
    :param int order: order of the curve over which the signature was computed

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r
z3Invalid number of strings provided: {0}, expected 2zjInvalid length of first string ('r' parameter), expected {0} bytes long, provided string is {1} bytes longzkInvalid length of second string ('s' parameter), expected {0} bytes long, provided string is {1} bytes long)rr~r�r	r)rm)�
rs_stringsr(rqrrrgrorprrr�sigdecode_strings�s.����

r�cCsrt|�}t�|�\}}|dkrt�dt�|���t�|�\}}t�|�\}}|dkr5t�dt�|���||fS)a
    Decoder for DER format of ECDSA signatures.

    DER format of signature is one that uses the :term:`ASN.1` :term:`DER`
    rules to encode it as a sequence of two integers::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param sig_der: encoded signature
    :type sig_der: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises UnexpectedDER: when the encoding of signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r\ztrailing junk after DER sig: %sz#trailing junk after DER numbers: %s)r	r�remove_sequence�
UnexpectedDERrJrK�remove_integer)�sig_derr(r��emptyro�restrprrr�
sigdecode_der�s��r�r4)2r�
__future__rr+rPrJ�sys�hashlibr�sixrrrrr�_compatr	�oid_ecPublicKey�
encode_oid�encoded_oid_ecPublicKey�oid_ecDH�	oid_ecMQV�version_inforr$r)r2r3rMrOrTrXr[rarjrkr]rmrsrtrxryr{r|r}�	Exceptionr~r�r�r�rrrr�<module>sX








!.