X-Git-Url: http://git.datanom.net/securemail.git/blobdiff_plain/8c4f590c61472aa754a180e918ca5de7d1af5ad6..d65fab5a133cb7769beb24b7df81d356c12a6693:/cryptonize.py diff --git a/cryptonize.py b/cryptonize.py index 9cc4631..d375f23 100644 --- a/cryptonize.py +++ b/cryptonize.py @@ -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)