]> git.datanom.net - securemail.git/blobdiff - cryptonize.py
Make backwards compatible with nacl 1.0.x
[securemail.git] / cryptonize.py
index d4b511596e803b308995bf7c472b81a929054ae7..7a598d32cfd77cb672415d30d908d76b31b84814 100644 (file)
@@ -33,8 +33,6 @@ class Cryptonize:
         skey = self.sanitize_key(key)
         box = SecretBox(skey)
         if NACL_VERSION < "1.1.0":
         skey = self.sanitize_key(key)
         box = SecretBox(skey)
         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:
             nonce = random(SecretBox.NONCE_SIZE)
             cipher = box.encrypt(plain, nonce)
         else:
@@ -52,13 +50,21 @@ class Cryptonize:
         return plain
         
     def asymmetric_encrypt(self, privkey, pubkey, plain):
         return plain
         
     def asymmetric_encrypt(self, privkey, pubkey, plain):
+        if not isinstance(plain, bytes):
+            plain = plain.encode('utf-8')
         box = Box(privkey, pubkey)
         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):
         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
         box = Box(privkey, pubkey)
         plain = box.decrypt(cipher)
         box = None
This page took 0.032535 seconds and 5 git commands to generate.