o
    w+f                     @   s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZmZmZmZmZ d dlmZmZmZmZmZ G dd dZd	Zd
ZdZdZdS )   )Math)Point)	secp256k1getByOid)getPemContent	createPem)
hexFromIntparseDerFieldTypeencodeConstructedencodePrimitive)hexFromByteStringbyteStringFromHex
intFromHexbase64FromByteStringbyteStringFromBase64c                   @   sp   e Zd Zdd ZdddZdd Zdd	 Zd
d Zedd Z	edd Z
eedfddZeefddZdS )	PublicKeyc                 C   s   || _ || _d S )Npointcurve)selfr   r    r   J/var/www/html/venv/lib/python3.10/site-packages/ellipticcurve/publicKey.py__init__   s   
zPublicKey.__init__Fc                 C   sJ   d| j   }t| jj|}t| jj|}|| }|r#d| S |S )N   0004)r   lengthr   r   xzfilly)r   encoded
baseLengthxHexyHexstringr   r   r   toString   s   zPublicKey.toStringc                 C   s@   d| j   }| jjd dkrtnt}t| jj|}|| S )Nr       )	r   r   r   r   _evenTag_oddTagr   r   r   )r   r!   	parityTagr"   r   r   r   toCompressed   s   zPublicKey.toCompressedc                 C   s<   t t ttjtttj| jjttj| jdd}t	|S )NT)r    )
r   r   r
   object_ecdsaPublicKeyOidr   oid	bitStringr%   r   )r   hexadecimalr   r   r   toDer   s   
zPublicKey.toDerc                 C   s   |   }tt|tdS )N)contenttemplate)r0   r   r   _pemTemplate)r   derr   r   r   toPem(   s   zPublicKey.toPemc                 C   s   t |td}| t|S )N)pemr2   )r   r3   fromDerr   )clsr$   publicKeyPemr   r   r   fromPem,   s   zPublicKey.fromPemc                 C   sP   t |}t|d \}}|\}}|tkrtdjt|dt|}| j||dS )Nr&   zgThe Public Key Object Identifier (OID) should be {ecdsaPublicKeyOid}, but {actualOid} was found instead)ecdsaPublicKeyOid	actualOid)r$   r   )r   r	   r,   	Exceptionformatr   
fromString)r8   r$   r/   	curveDatapointStringpublicKeyOidcurveOidr   r   r   r   r7   1   s   zPublicKey.fromDerTc           	      C   s   d|   }t|d| kr|d d dkr|dd  }|d | }||d  }tt|t|d}t||d}|s<|S | rDtd||sVtdj|j	|j
|jdtj||j|j|j|jd	 sstd
j|j	|j
|jd|S )Nr      r   )r   r   r   zPublic Key point is at infinityz-Point ({x},{y}) is not valid for curve {name})r   r   name)pnNAPz-Point ({x},{y}) * {name}.N is not at infinity)r   lenr   r   r   isAtInfinityr=   containsr>   r   r   rE   r   multiplyrH   rI   rJ   )	r8   r$   r   validatePointr!   xsysrF   	publicKeyr   r   r   r?   >   s&    
 zPublicKey.fromStringc                 C   sZ   |d d |dd  }}|t tfvrtdt|}|j||t kd}| t|||dS )Nr   z,Compressed string should start with 02 or 03)isEvenr   )r'   r(   r=   r   r   r   )r8   r$   r   r)   r"   r   r   r   r   r   fromCompressedV   s   zPublicKey.fromCompressedN)F)__name__
__module____qualname__r   r%   r*   r0   r5   classmethodr:   r7   r   r?   rT   r   r   r   r   r   	   s    
	


r   0203)r   r   iH  i='  r   r   z?
-----BEGIN PUBLIC KEY-----
{content}
-----END PUBLIC KEY-----
N)mathr   r   r   r   r   r   	utils.pemr   r   	utils.derr   r	   r
   r   r   utils.binaryr   r   r   r   r   r   r'   r(   r,   r3   r   r   r   r   <module>   s    W