3 œไL]์%ใ@sะddlmZmZmZddlmZddlmZm Z ddl m Z ddgZ e j ƒZe jƒZe jƒZe jƒZe jƒZe jƒZe jƒZe jƒZdd„Zdd „Zd d„Zd d „Z d d„Z!dd„Z"dd„Z#dd„Z$dd„Z%dS)้)ฺabsolute_importฺdivisionฺprint_function)ฺ exceptions)ฺffiฺlib)ฺensureฺcrypto_box_keypairฺ crypto_boxcCs`tjdtƒ}tjdtƒ}tj||ƒ}t|dkdtjdtj |tƒdd…tj |tƒdd…fS)zq Returns a randomly generated public and secret key. :rtype: (bytes(public_key), bytes(secret_key)) zunsigned char[]rzUnexpected library error)ฺraisingN) rฺnewฺcrypto_box_PUBLICKEYBYTESฺcrypto_box_SECRETKEYBYTESrr rฺexcฺ RuntimeErrorฺbuffer)ฺpkฺskฺrcฉr๚tjdƒ‚t|ƒ}t|}t j d|ƒ}t j ||||ƒ}t|dkdtj dt j||ƒdd…S) a€ Encrypts and returns a message ``message`` using an ephemeral secret key and the public key ``pk``. The ephemeral public key, which is embedded in the sealed box, is also used, in combination with ``pk``, to derive the nonce needed for the underlying box construct. :param message: bytes :param pk: bytes :rtype: bytes .. versionadded:: 1.2 zinput message must be bytes)r zpublic key must be byteszInvalid public keyzunsigned char[]rzUnexpected library errorN)rrrrrr rrฺcrypto_box_SEALBYTESrr rฺcrypto_box_sealrr)r#rฺ_mlenฺ_clenr&rrrrr1๊s       r1cCsพtt|tƒdtdtt|tƒdtdtt|tƒdtdt|ƒtkrRtjdƒ‚t|ƒtkrhtjdƒ‚t|ƒ}|t }t j d|ƒ}t j |||||ƒ}t|dkd tjdt j||ƒd d …S) a“ Decrypts and returns an encrypted message ``ciphertext``, using the recipent's secret key ``sk`` and the sender's ephemeral public key embedded in the sealed box. The box contruct nonce is derived from the recipient's public key ``pk`` and the sender's public key. :param ciphertext: bytes :param pk: bytes :param sk: bytes :rtype: bytes .. versionadded:: 1.2 zinput ciphertext must be bytes)r zpublic key must be byteszsecret key must be byteszInvalid public keyzInvalid secret keyzunsigned char[]rz/An error occurred trying to decrypt the messageN)rrrrrr rrrr0rr rฺcrypto_box_seal_openr(r)r&rrr3r2r)r*rrrr4s(          r4N)&ฺ __future__rrrZnaclrrZ nacl._sodiumrrZnacl.exceptionsrฺ__all__Zcrypto_box_secretkeybytesrZcrypto_box_publickeybytesr Zcrypto_box_seedbytesrZcrypto_box_noncebytesr Zcrypto_box_zerobytesr!Zcrypto_box_boxzerobytesr"Zcrypto_box_beforenmbytesr+Zcrypto_box_sealbytesr0r rr r'r,r.r/r1r4rrrrฺs*  &&