# 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":
+ 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
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
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):