o
    @Bi)                     @   s  d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z d dl	mZ dd	gZz"d d
lmZmZmZ d dlmZmZmZ d dlmZ d dlmZ W n eyY   dZY n
w dZeddg ereG dd deZeG dd deZ eG dd deZ!e"e! dS dS )    )absolute_importN)public)IPayloadCodec)EncodedPayload)_dumps)_loadsHAS_CRYPTOBOXr   )Base64Encoder
RawEncoder
HexEncoder)
PrivateKey	PublicKeyBox)random)
StringTrieFTKeyKeyRingc                   @      e Zd ZdZdddZdS )r   z
        Holds originator and responder keys for an URI.

        The originator is either a caller or a publisher. The responder is either a callee or subscriber.
        Nc                 C   s  |r
t |td| _nd | _| jr!| jj| _|d u s || jks J nt|td| _|r2t |td| _nd | _| jrI| jj| _|d u sH|| jksHJ nt|td| _| jr_| jr_t| j| j| _	nd | _	| jrq| jrqt| j| j| _
nd | _
| j	s~| j
stdd S d S )NencoderzDinsufficient keys provided for at least originator or responder role)r   r	   originator_priv
public_keyoriginator_pubr   responder_privresponder_pubr   originator_boxresponder_box	Exception)selfr   r   r   r    r   W/var/www/html/Trade-python/venv/lib/python3.10/site-packages/autobahn/wamp/cryptobox.py__init__A   s2   

zKey.__init__)NNNN__name__
__module____qualname____doc__r!   r   r   r   r    r   9   s    c                   @   r   )SymKeyz3
        Holds a symmetric key for an URI.
        Nc                 C   s   d S Nr   )r   rawr   r   r    r!   v   s   zSymKey.__init__r(   r"   r   r   r   r    r'   q   s    r'   c                   @   sr   e Zd ZdZedddZedd Zedd Zed	d
 Zedd Z	dddZ
edddZedd ZdS )r   a	  
        A keyring holds (cryptobox) public-private key pairs for use with WAMP-cryptobox payload
        encryption. The keyring can be set on a WAMP session and then transparently will get used
        for encrypting and decrypting WAMP message payloads.
        Nc                 C   sP   |du st |tst|tjksJ t | _t|tjkr#t||d}|| _dS )zj

            Create a new key ring to hold public and private keys mapped from an URI space.
            Nr   r   )
isinstancer   typesix	text_typer   _uri_to_key_default_key)r   default_keyr   r   r    r!      s
   $
zKeyRing.__init__c                 C   6   t  }|jtd}|jjtd}|d|dfS )z
            Generate a new private key and return a pair with the base64 encodings
            of (priv_key, pub_key).
            r   ascii)r   generateencoder	   r   decoder   keypriv_keypub_keyr   r   r    generate_key      zKeyRing.generate_keyc                 C   r2   )z
            Generate a new private key and return a pair with the hex encodings
            of (priv_key, pub_key).
            r   r3   )r   r4   r5   r   r   r6   r7   r   r   r    generate_key_hex   r<   zKeyRing.generate_key_hexc                 C   s   t |tjks	J |du st|tst |tjksJ t |tjkr(t||d}|dkr1|| _dS |du rB|| jv r@| j|= dS dS || j|< dS )z8
            Add a key set for a given URI.
            Nr*    )r,   r-   r.   r+   r   r0   r/   )r   urir8   r   r   r    set_key   s   $

zKeyRing.set_keyc                 C   s@   t |tjks	J || jv r| j|   d S | j|   d S r(   )r,   r-   r.   r/   rotate)r   r?   r   r   r    
rotate_key   s   
zKeyRing.rotate_keyFc                 C   sX   z|r	| j | }n| j |}W n ty#   | jr| j}nY d S Y nw |r)|jS |jS r(   )r/   longest_prefix_valueKeyErrorr0   r   r   )r   is_originatingr?   match_exactr8   r   r   r    _get_box   s   zKeyRing._get_boxc                 C   s   t |tksJ t |tjksJ |du st |ttfv sJ |du s+t |tks+J | ||}|s5dS |||d}tt	j
}t|d}|j||td}	t|	}
d}t|
dd|dS )z
            Encrypt the given WAMP URI, args and kwargs into an EncodedPayload instance, or None
            if the URI should not be encrypted.
            N)r?   argskwargsutf8r   	cryptoboxjson)enc_key)r,   boolr-   r.   listtupledictrG   r   r   
NONCE_SIZE_json_dumpsr5   encryptr
   bytesr   )r   rE   r?   rH   rI   boxpayloadnoncepayload_serpayload_encrpayload_bytespayload_keyr   r   r    r5      s"   
zKeyRing.encodec           	      C   s   t |tjks	J t|tsJ |jdksJ | ||}|s#td|j|j	t
d}|jdkr8td|jt|d}|dd}|d	d}|d
d}|||fS )zm
            Decrypt the given WAMP URI and EncodedPayload into a tuple ``(uri, args, kwargs)``.
            rK   z/received encrypted payload, but can't find key!r   rL   zIreceived encrypted payload, but don't know how to process serializer '{}'rJ   r?   NrH   rI   )r,   r-   r.   r+   r   enc_algorG   r   decryptrW   r
   enc_serializerformat_json_loadsr6   get)	r   rE   r?   encoded_payloadrV   rY   rW   rH   rI   r   r   r    r6      s   

zKeyRing.decoder(   )F)NN)r#   r$   r%   r&   r   r!   r;   r=   r@   rB   rG   r5   r6   r   r   r   r    r   y   s"    






#)#
__future__r   r-   autobahn.utilr   autobahn.wamp.interfacesr   autobahn.wamp.typesr   autobahn.wamp.serializerr   rS   r   ra   __all__nacl.encodingr	   r
   r   nacl.publicr   r   r   
nacl.utilsr   pytrier   ImportErrorr   extendobjectr   r'   r   registerr   r   r   r    <module>   s>   7  