U vù¾fL/ã@spddlmZmZddlZddlmZddlmZddlm Z m Z m Z Gdd„dej e ƒZ Gdd „d ej e ƒZdS) é)ÚClassVarÚOptionalN)Úencoding)Ú exceptions)ÚEncryptedMessageÚ StringFixerÚrandomc@sÌeZdZUdZejjZee e d<ejj Z ee e d<ejj Zee e d<ejjZee e d<ejfeejdœdd„Zed œd d „Zd ejfeeeejed œdd„Zd ejfeeeejedœdd„Zd S)Ú SecretBoxuS The SecretBox class encrypts and decrypts messages using the given secret key. The ciphertexts generated by :class:`~nacl.secret.Secretbox` include a 16 byte authenticator which is checked as part of the decryption. An invalid authenticator will cause the decrypt function to raise an exception. The authenticator is not a signature. Once you've decrypted the message you've demonstrated the ability to create arbitrary valid message, so messages you send are repudiable. For non-repudiable messages, sign them after encryption. Encryption is done using `XSalsa20-Poly1305`_, and there are no practical limits on the number or size of messages (up to 2â¶â´ messages, each up to 2â¶â´ bytes). .. _XSalsa20-Poly1305: https://doc.libsodium.org/secret-key_cryptography/secretbox#algorithm-details :param key: The secret key used to encrypt and decrypt messages :param encoder: The encoder class used to decode the given key :cvar KEY_SIZE: The size that the key is required to be. :cvar NONCE_SIZE: The size that the nonce is required to be. :cvar MACBYTES: The size of the authentication MAC tag in bytes. :cvar MESSAGEBYTES_MAX: The maximum size of a message which can be safely encrypted with a single key/nonce pair. ÚKEY_SIZEÚ NONCE_SIZEÚMACBYTESÚMESSAGEBYTES_MAX©ÚkeyÚencodercCsF| |¡}t|tƒst d¡‚t|ƒ|jkr/opt/alt/python38/lib64/python3.8/site-packages/nacl/secret.pyÚ__init__;s   ÿzSecretBox.__init__©ÚreturncCs|jS©N©r©rrrrÚ __bytes__IszSecretBox.__bytes__N)Ú plaintextÚnoncerr!cCsn|dkrt|jƒ}t|ƒ|jkr0t d|j¡‚tj |||j¡}|  |¡}|  |¡}t   |||  ||¡¡S)aL Encrypts the plaintext message using the given `nonce` (or generates one randomly if omitted) and returns the ciphertext encoded with the encoder. .. warning:: It is **VITALLY** important that the nonce is a nonce, i.e. it is a number used only once for any given key. If you fail to do this, you compromise the privacy of the messages encrypted. Give your nonces a different prefix, or have one side use an odd counter and one an even counter. Just make sure they are different. :param plaintext: [:class:`bytes`] The plaintext message to encrypt :param nonce: [:class:`bytes`] The nonce to use in the encryption :param encoder: The encoder to use to encode the ciphertext :rtype: [:class:`nacl.utils.EncryptedMessage`] Nú'The nonce must be exactly %s bytes long) rr rrrÚnaclÚbindingsZcrypto_secretboxrÚencoderÚ _from_parts)rr&r'rÚ ciphertextÚ encoded_nonceÚencoded_ciphertextrrrÚencryptLs$ ÿÿ   ýzSecretBox.encrypt)r-r'rr!cCsb| |¡}|dkr.|d|j…}||jd…}t|ƒ|jkrLt d|j¡‚tj |||j¡}|S©aá Decrypts the ciphertext using the `nonce` (explicitly, when passed as a parameter or implicitly, when omitted, as part of the ciphertext) and returns the plaintext message. :param ciphertext: [:class:`bytes`] The encrypted message to decrypt :param nonce: [:class:`bytes`] The nonce used when encrypting the ciphertext :param encoder: The encoder used to decode the ciphertext. :rtype: [:class:`bytes`] Nr() rr rrrr)r*Zcrypto_secretbox_openr)rr-r'rr&rrrÚdecryptws ÿÿzSecretBox.decrypt)Ú__name__Ú __module__Ú __qualname__Ú__doc__r)r*Zcrypto_secretbox_KEYBYTESr rÚintÚ__annotations__Zcrypto_secretbox_NONCEBYTESr Zcrypto_secretbox_MACBYTESr Z!crypto_secretbox_MESSAGEBYTES_MAXr rÚ RawEncoderrÚEncoderrr%rrr0r2rrrrr s< þÿÿÿ üû .üûr c@s¢eZdZdZejjZejjZ ejj Z ejj Z ejfeejdœdd„Zedœdd„Zdd ejfeeeeejed œd d „Zdd ejfeeeeejed œdd„Zd S)ÚAeaduñ The AEAD class encrypts and decrypts messages using the given secret key. Unlike :class:`~nacl.secret.SecretBox`, AEAD supports authenticating non-confidential data received alongside the message, such as a length or type tag. Like :class:`~nacl.secret.Secretbox`, this class provides authenticated encryption. An inauthentic message will cause the decrypt function to raise an exception. Likewise, the authenticator should not be mistaken for a (public-key) signature: recipients (with the ability to decrypt messages) are capable of creating arbitrary valid message; in particular, this means AEAD messages are repudiable. For non-repudiable messages, sign them after encryption. The cryptosystem used is `XChacha20-Poly1305`_ as specified for `standardization`_. There are `no practical limits`_ to how much can safely be encrypted under a given key (up to 2â¶â´ messages each containing up to 2â¶â´ bytes). .. _standardization: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha .. _XChacha20-Poly1305: https://doc.libsodium.org/secret-key_cryptography/aead#xchacha-20-poly1305 .. _no practical limits: https://doc.libsodium.org/secret-key_cryptography/aead#limitations :param key: The secret key used to encrypt and decrypt messages :param encoder: The encoder class used to decode the given key :cvar KEY_SIZE: The size that the key is required to be. :cvar NONCE_SIZE: The size that the nonce is required to be. :cvar MACBYTES: The size of the authentication MAC tag in bytes. :cvar MESSAGEBYTES_MAX: The maximum size of a message which can be safely encrypted with a single key/nonce pair. rcCsF| |¡}t|tƒst d¡‚t|ƒ|jkrs