]> git.datanom.net - securemail.git/blobdiff - cryptonize.py
First working beta
[securemail.git] / cryptonize.py
index 9cc463120804f83d6a7295b4565107d609748d3b..d375f2300b07b09d3299b9a783e6fc8e802fb133 100644 (file)
@@ -19,7 +19,7 @@
 
 from nacl.secret import SecretBox
 from nacl.public import PrivateKey, Box
-from nacl.utils import random
+from nacl.utils import random, EncryptedMessage
 from nacl.encoding import HexEncoder
 import nacl.hash
 
@@ -32,7 +32,7 @@ class Cryptonize:
         skey = self.sanitize_key(key)
         box = SecretBox(skey)
         cipher = box.encrypt(plain)
-        box = None
+        box = skey = None
         
         return cipher
         
@@ -40,7 +40,7 @@ class Cryptonize:
         skey = self.sanitize_key(key)
         box = SecretBox(skey)
         plain = box.decrypt(cipher)
-        box = None
+        box = skey = None
         
         return plain
         
@@ -66,14 +66,10 @@ class Cryptonize:
             key = key.encode('utf-8')
         size = len(key)
         if size < SecretBox.KEY_SIZE:
-            """We must pad"""
-            pad = None
-            for i in range(SecretBox.KEY_SIZE - size):
-                if pad is None:
-                    pad = b'\0'
-                else:
-                    pad += b'\0'
-            newkey = key + pad
+            """ We must pad """
+            newkey = key + bytes(SecretBox.KEY_SIZE - size)
+        elif size > SecretBox.KEY_SIZE:
+            newkey = key[:SecretBox.KEY_SIZE]
         else:
             newkey = key
         
@@ -94,4 +90,9 @@ class Cryptonize:
 
         return digest.decode()
 
+    def create_EncryptedMessage(self, payload):
+        nonce = payload[:SecretBox.NONCE_SIZE]
+        ciphertext = payload[SecretBox.NONCE_SIZE:]
         
+        return EncryptedMessage._from_parts(
+            nonce, ciphertext,  nonce + ciphertext)
This page took 0.033705 seconds and 5 git commands to generate.