X-Git-Url: http://git.datanom.net/securemail.git/blobdiff_plain/d65fab5a133cb7769beb24b7df81d356c12a6693..b49f84de2e0655566790f5f11a8ef15ac379b8b3:/cryptonize.py diff --git a/cryptonize.py b/cryptonize.py index d375f23..7a598d3 100644 --- a/cryptonize.py +++ b/cryptonize.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU General Public License # along with SecureMail. If not, see . +from nacl import __version__ as NACL_VERSION from nacl.secret import SecretBox from nacl.public import PrivateKey, Box from nacl.utils import random, EncryptedMessage @@ -31,7 +32,11 @@ class Cryptonize: def symmetric_encrypt(self, key, plain): skey = self.sanitize_key(key) box = SecretBox(skey) - cipher = box.encrypt(plain) + if NACL_VERSION < "1.1.0": + nonce = random(SecretBox.NONCE_SIZE) + cipher = box.encrypt(plain, nonce) + else: + cipher = box.encrypt(plain) box = skey = None return cipher @@ -45,13 +50,21 @@ class Cryptonize: return plain def asymmetric_encrypt(self, privkey, pubkey, plain): + if not isinstance(plain, bytes): + plain = plain.encode('utf-8') box = Box(privkey, pubkey) - cipher = box.encrypt(plain) + if NACL_VERSION < "1.1.0": + nonce = random(Box.NONCE_SIZE) + cipher = box.encrypt(plain, nonce) + else: + cipher = box.encrypt(plain) box = None return cipher def asymmetric_decrypt(self, privkey, pubkey, cipher): + if not isinstance(cipher, bytes): + cipher = cipher.encode('utf-8') box = Box(privkey, pubkey) plain = box.decrypt(cipher) box = None