# 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
def symmetric_encrypt(self, key, plain):
skey = self.sanitize_key(key)
box = SecretBox(skey)
- cipher = box.encrypt(plain)
+ if NACL_VERSION < "1.1.0":
+ nonce = random(SecretBox.NONCE_SIZE)
+ cipher = box.encrypt(plain, nonce)
+ else:
+ cipher = box.encrypt(plain)
box = skey = None
return cipher
return plain
def asymmetric_encrypt(self, privkey, pubkey, plain):
+ if not isinstance(plain, bytes):
+ plain = plain.encode('utf-8')
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):
+ if not isinstance(cipher, bytes):
+ cipher = cipher.encode('utf-8')
box = Box(privkey, pubkey)
plain = box.decrypt(cipher)
box = None