U
    ֫[                     @   s   d dl mZmZmZ d dlZd dlmZ d dlmZ d dl	m
ZmZ d dlmZmZ G dd deZG d	d
 d
ejeeZG dd dejeeZdS )    )absolute_importdivisionprint_functionN)encoding)
exceptions)
PrivateKey	PublicKey)StringFixerrandomc                   @   s4   e Zd ZdZedd Zedd Zedd ZdS )	SignedMessagezc
    A bytes subclass that holds a messaged that has been signed by a
    :class:`SigningKey`.
    c                 C   s   | |}||_ ||_|S N)
_signature_message)cls	signaturemessageZcombinedobj r   ./usr/lib/python3/dist-packages/nacl/signing.py_from_parts   s    zSignedMessage._from_partsc                 C   s   | j S )zL
        The signature contained within the :class:`SignedMessage`.
        )r   selfr   r   r   r   &   s    zSignedMessage.signaturec                 C   s   | j S )zJ
        The message contained within the :class:`SignedMessage`.
        )r   r   r   r   r   r   -   s    zSignedMessage.messageN)	__name__
__module____qualname____doc__classmethodr   propertyr   r   r   r   r   r   r      s   

r   c                   @   sV   e Zd ZdZejfddZdd Zdd Zdd	 Z	d
d Z
dejfddZdd ZdS )	VerifyKeyz
    The public key counterpart to an Ed25519 SigningKey for producing digital
    signatures.

    :param key: [:class:`bytes`] Serialized Ed25519 public key
    :param encoder: A class that is able to decode the `key`
    c                 C   sJ   | |}t|tstdt|tjjkr@t	dtjj || _
d S )Nz'VerifyKey must be created from 32 bytesz%The key must be exactly %s bytes long)decode
isinstancebytesexc	TypeErrorlennaclbindingsZcrypto_sign_PUBLICKEYBYTES
ValueError_key)r   keyencoderr   r   r   __init__>   s    


zVerifyKey.__init__c                 C   s   | j S r   )r(   r   r   r   r   	__bytes__L   s    zVerifyKey.__bytes__c                 C   s   t t| S r   hashr!   r   r   r   r   __hash__O   s    zVerifyKey.__hash__c                 C   s&   t || jsdS tjt| t|S NFr    	__class__r%   r&   Zsodium_memcmpr!   r   otherr   r   r   __eq__R   s    zVerifyKey.__eq__c                 C   s
   | |k S r   r   r3   r   r   r   __ne__W   s    zVerifyKey.__ne__Nc                 C   s*   |dk	r|| }| |}tj|| jS )aS  
        Verifies the signature of a signed message, returning the message
        if it has not been tampered with else raising
        :class:`~nacl.signing.BadSignatureError`.

        :param smessage: [:class:`bytes`] Either the original messaged or a
            signature and message concated together.
        :param signature: [:class:`bytes`] If an unsigned message is given for
            smessage then the detached signature must be provided.
        :param encoder: A class that is able to decode the secret message and
            signature.
        :rtype: :class:`bytes`
        N)r   r%   r&   Zcrypto_sign_openr(   )r   Zsmessager   r*   r   r   r   verifyZ   s    
zVerifyKey.verifyc                 C   s   t j| j}t|S )z
        Converts a :class:`~nacl.signing.VerifyKey` to a
        :class:`~nacl.public.PublicKey`

        :rtype: :class:`~nacl.public.PublicKey`
        )r%   r&   Z$crypto_sign_ed25519_pk_to_curve25519r(   _Curve25519_PublicKey)r   Zraw_pkr   r   r   to_curve25519_public_keyr   s    z"VerifyKey.to_curve25519_public_key)r   r   r   r   r   
RawEncoderr+   r,   r/   r5   r6   r7   r9   r   r   r   r   r   5   s   r   c                   @   s`   e Zd ZdZejfddZdd Zdd Zdd	 Z	d
d Z
edd ZejfddZdd ZdS )
SigningKeya  
    Private key for producing digital signatures using the Ed25519 algorithm.

    Signing keys are produced from a 32-byte (256-bit) random seed value. This
    value can be passed into the :class:`~nacl.signing.SigningKey` as a
    :func:`bytes` whose length is 32.

    .. warning:: This **must** be protected and remain secret. Anyone who knows
        the value of your :class:`~nacl.signing.SigningKey` or it's seed can
        masquerade as you.

    :param seed: [:class:`bytes`] Random 32-byte value (i.e. private key)
    :param encoder: A class that is able to decode the seed

    :ivar: verify_key: [:class:`~nacl.signing.VerifyKey`] The verify
        (i.e. public) key that corresponds with this signing key.
    c                 C   sj   | |}t|tstdt|tjjkr@t	dtjj tj
|\}}|| _|| _t|| _d S )Nz.SigningKey must be created from a 32 byte seedz&The seed must be exactly %d bytes long)r   r    r!   r"   r#   r$   r%   r&   crypto_sign_SEEDBYTESr'   Zcrypto_sign_seed_keypair_seed_signing_keyr   Z
verify_key)r   Zseedr*   Z
public_keyZ
secret_keyr   r   r   r+      s    

zSigningKey.__init__c                 C   s   | j S r   )r=   r   r   r   r   r,      s    zSigningKey.__bytes__c                 C   s   t t| S r   r-   r   r   r   r   r/      s    zSigningKey.__hash__c                 C   s&   t || jsdS tjt| t|S r0   r1   r3   r   r   r   r5      s    zSigningKey.__eq__c                 C   s
   | |k S r   r   r3   r   r   r   r6      s    zSigningKey.__ne__c                 C   s   | t tjjtjdS )z
        Generates a random :class:`~nacl.signing.SigningKey` object.

        :rtype: :class:`~nacl.signing.SigningKey`
        )r*   )r
   r%   r&   r<   r   r:   )r   r   r   r   generate   s    
zSigningKey.generatec                 C   sT   t j|| j}t jj}||d| }|||d }||}t|||S )z
        Sign a message using this key.

        :param message: [:class:`bytes`] The data to be signed.
        :param encoder: A class that is used to encode the signed message.
        :rtype: :class:`~nacl.signing.SignedMessage`
        N)r%   r&   Zcrypto_signr>   crypto_sign_BYTESencoder   r   )r   r   r*   Z
raw_signedr@   r   Zsignedr   r   r   sign   s    
zSigningKey.signc                 C   s   | j }tj|}t|S )z
        Converts a :class:`~nacl.signing.SigningKey` to a
        :class:`~nacl.public.PrivateKey`

        :rtype: :class:`~nacl.public.PrivateKey`
        )r>   r%   r&   Z$crypto_sign_ed25519_sk_to_curve25519_Curve25519_PrivateKey)r   ZskZraw_privater   r   r   to_curve25519_private_key   s    z$SigningKey.to_curve25519_private_keyN)r   r   r   r   r   r:   r+   r,   r/   r5   r6   r   r?   rB   rD   r   r   r   r   r;   }   s   
r;   )Z
__future__r   r   r   Znacl.bindingsr%   r   r   r"   Znacl.publicr   rC   r   r8   Z
nacl.utilsr	   r
   r!   r   Z	Encodableobjectr   r;   r   r   r   r   <module>   s   H