]> git.datanom.net - securemail.git/commitdiff
Make backwards compatible with nacl 1.0.x
authorMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 23:17:44 +0000 (01:17 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 23:17:44 +0000 (01:17 +0200)
cryptonize.py
user.py

index d375f2300b07b09d3299b9a783e6fc8e802fb133..d4b511596e803b308995bf7c472b81a929054ae7 100644 (file)
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with SecureMail.  If not, see <https://www.gnu.org/licenses/>.
 
+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 fd3d39724e34770b1a0551a5b8797688789f9619..64673c7c7ce58f5484d271cb7f85c9f5623221c3 100644 (file)
--- 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):
This page took 0.032981 seconds and 5 git commands to generate.