o
    >h)                     @   s  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yO   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 )    )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/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/wamp/cryptobox.py__init__>   s2   

zKey.__init__)NNNN__name__
__module____qualname____doc__r    r   r   r   r   r   6   s    c                   @   r   )SymKeyz3
        Holds a symmetric key for an URI.
        Nc                 C   s   d S Nr   )r   rawr   r   r   r    s   s   zSymKey.__init__r'   r!   r   r   r   r   r&   n   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   sL   |du st |tst|tksJ t | _t|t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strr   _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   r0   )z
            Generate a new private key and return a pair with the hex encodings
            of (priv_key, pub_key).
            r   r1   )r   r2   r3   r
   r   r4   r5   r   r   r   generate_key_hex   r:   zKeyRing.generate_key_hexc                 C   s   t |tksJ |du st|tst |tksJ t |tkr%t||d}|dkr.|| _dS |du r?|| 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.   r-   )r   urir6   r   r   r   set_key   s   "

zKeyRing.set_keyc                 C   s>   t |tksJ || jv r| j|   d S | j|   d S 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KeyErrorr.   r   r   )r   is_originatingr=   match_exactr6   r   r   r   _get_box   s   zKeyRing._get_boxc                 C   s   t |tksJ t |tksJ |du st |ttfv sJ |du s*t |tks*J | ||}|s4d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,   listtupledictrE   r   r   
NONCE_SIZE_json_dumpsr3   encryptr	   bytesr   )r   rC   r=   rF   rG   boxpayloadnoncepayload_serpayload_encrpayload_bytespayload_keyr   r   r   r3      s"   
zKeyRing.encodec           	      C   s   t |tksJ t|tsJ |jdksJ | ||}|s"td|j|jt	d}|j
dkr7t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)``.
            rI   z/received encrypted payload, but can't find key!r   rJ   zIreceived encrypted payload, but don't know how to process serializer '{}'rH   r=   NrF   rG   )r+   r,   r*   r   enc_algorE   r   decryptrU   r	   enc_serializerformat_json_loadsr4   get)	r   rC   r=   encoded_payloadrT   rW   rU   rF   rG   r   r   r   r4      s   

zKeyRing.decoder'   )F)NN)r"   r#   r$   r%   r   r    r9   r;   r>   r@   rE   r3   r4   r   r   r   r   r   v   s"    






#N) autobahn.utilr   autobahn.wamp.interfacesr   autobahn.wamp.typesr   autobahn.wamp.serializerr   rQ   r   r_   __all__nacl.encodingr   r	   r
   nacl.publicr   r   r   
nacl.utilsr   pytrier   ImportErrorr   extendobjectr   r&   r   registerr   r   r   r   <module>   s:   7  