]> git.datanom.net - securemail.git/blame - cryptonize.py
Make backwards compatible with nacl 1.0.x
[securemail.git] / cryptonize.py
CommitLineData
8c4f590c
MR
1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2018 Michael Rasmussen <mir@datanom.net>
4
5# This file is part of SecureMail.
6
7# SecureMail is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# SecureMail is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with SecureMail. If not, see <https://www.gnu.org/licenses/>.
19
481494d3 20from nacl import __version__ as NACL_VERSION
8c4f590c
MR
21from nacl.secret import SecretBox
22from nacl.public import PrivateKey, Box
d65fab5a 23from nacl.utils import random, EncryptedMessage
8c4f590c
MR
24from nacl.encoding import HexEncoder
25import nacl.hash
26
27class Cryptonize:
28 """
29 Encrypt and decrypt objects
30 """
31
32 def symmetric_encrypt(self, key, plain):
33 skey = self.sanitize_key(key)
34 box = SecretBox(skey)
481494d3
MR
35 if NACL_VERSION < "1.1.0":
36 from nacl.utils import random
37 from nacl.public import SecretBox
38 nonce = random(SecretBox.NONCE_SIZE)
39 cipher = box.encrypt(plain, nonce)
40 else:
41 cipher = box.encrypt(plain)
d65fab5a 42 box = skey = None
8c4f590c
MR
43
44 return cipher
45
46 def symmetric_decrypt(self, key, cipher):
47 skey = self.sanitize_key(key)
48 box = SecretBox(skey)
49 plain = box.decrypt(cipher)
d65fab5a 50 box = skey = None
8c4f590c
MR
51
52 return plain
53
54 def asymmetric_encrypt(self, privkey, pubkey, plain):
55 box = Box(privkey, pubkey)
56 cipher = box.encrypt(plain)
57 box = None
58
59 return cipher
60
61 def asymmetric_decrypt(self, privkey, pubkey, cipher):
62 box = Box(privkey, pubkey)
63 plain = box.decrypt(cipher)
64 box = None
65
66 return plain
67
68 def get_random_key(self):
69 return random(SecretBox.KEY_SIZE)
70
71 def sanitize_key(self, key):
72 if not isinstance(key, bytes):
73 key = key.encode('utf-8')
74 size = len(key)
75 if size < SecretBox.KEY_SIZE:
d65fab5a
MR
76 """ We must pad """
77 newkey = key + bytes(SecretBox.KEY_SIZE - size)
78 elif size > SecretBox.KEY_SIZE:
79 newkey = key[:SecretBox.KEY_SIZE]
8c4f590c
MR
80 else:
81 newkey = key
82
83
84 return newkey
85
86 def get_key_pair(self):
87 privkey = PrivateKey.generate()
88 pubkey = privkey.public_key
89
90 return (privkey, pubkey)
91
92 def generate_hash(self, key):
93 if not isinstance(key, bytes):
94 key = key.encode('utf-8')
95 HASHER = nacl.hash.sha512
96 digest = HASHER(key, encoder=HexEncoder)
97
98 return digest.decode()
99
d65fab5a
MR
100 def create_EncryptedMessage(self, payload):
101 nonce = payload[:SecretBox.NONCE_SIZE]
102 ciphertext = payload[SecretBox.NONCE_SIZE:]
8c4f590c 103
d65fab5a
MR
104 return EncryptedMessage._from_parts(
105 nonce, ciphertext, nonce + ciphertext)
This page took 0.181433 seconds and 5 git commands to generate.