From: Michael Rasmussen Date: Sat, 11 Aug 2018 23:17:44 +0000 (+0200) Subject: Make backwards compatible with nacl 1.0.x X-Git-Url: http://git.datanom.net/securemail.git/commitdiff_plain/481494d3dde40f7b22f10e4d0c5b1511fb52b0ff Make backwards compatible with nacl 1.0.x --- diff --git a/cryptonize.py b/cryptonize.py index d375f23..d4b5115 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,13 @@ 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": + from nacl.utils import random + from nacl.public import SecretBox + nonce = random(SecretBox.NONCE_SIZE) + cipher = box.encrypt(plain, nonce) + else: + cipher = box.encrypt(plain) box = skey = None return cipher diff --git a/user.py b/user.py index fd3d397..64673c7 100644 --- a/user.py +++ b/user.py @@ -21,7 +21,6 @@ import pickle from db import DBInterface as DBI from cryptonize import Cryptonize from nacl.public import PublicKey -from nacl import __version__ as NACL_VERSION class NoSuchUser(Exception): pass @@ -38,13 +37,7 @@ class User: def store(self, key): crypto = Cryptonize() - if NACL_VERSION < "1.1.0": - from nacl.utils import random - from nacl.public import SecretBox - nonce = random(SecretBox.NONCE_SIZE) - cipher = crypto.symmetric_encrypt(key, pickle.dumps(self), nonce) - else: - cipher = crypto.symmetric_encrypt(key, pickle.dumps(self)) + cipher = crypto.symmetric_encrypt(key, pickle.dumps(self)) DBI.store_user(crypto.generate_hash(key), cipher) def load(self, key):