]>
git.datanom.net - securemail.git/blob - cryptonize.py
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2018 Michael Rasmussen <mir@datanom.net>
5 # This file is part of SecureMail.
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.
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.
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/>.
20 from nacl
.secret
import SecretBox
21 from nacl
.public
import PrivateKey
, Box
22 from nacl
.utils
import random
23 from nacl
.encoding
import HexEncoder
28 Encrypt and decrypt objects
31 def symmetric_encrypt(self
, key
, plain
):
32 skey
= self
.sanitize_key(key
)
34 cipher
= box
.encrypt(plain
)
39 def symmetric_decrypt(self
, key
, cipher
):
40 skey
= self
.sanitize_key(key
)
42 plain
= box
.decrypt(cipher
)
47 def asymmetric_encrypt(self
, privkey
, pubkey
, plain
):
48 box
= Box(privkey
, pubkey
)
49 cipher
= box
.encrypt(plain
)
54 def asymmetric_decrypt(self
, privkey
, pubkey
, cipher
):
55 box
= Box(privkey
, pubkey
)
56 plain
= box
.decrypt(cipher
)
61 def get_random_key(self
):
62 return random(SecretBox
.KEY_SIZE
)
64 def sanitize_key(self
, key
):
65 if not isinstance(key
, bytes
):
66 key
= key
.encode('utf-8')
68 if size
< SecretBox
.KEY_SIZE
:
71 for i
in range(SecretBox
.KEY_SIZE
- size
):
83 def get_key_pair(self
):
84 privkey
= PrivateKey
.generate()
85 pubkey
= privkey
.public_key
87 return (privkey
, pubkey
)
89 def generate_hash(self
, key
):
90 if not isinstance(key
, bytes
):
91 key
= key
.encode('utf-8')
92 HASHER
= nacl
.hash.sha512
93 digest
= HASHER(key
, encoder
=HexEncoder
)
95 return digest
.decode()
This page took 0.068275 seconds and 6 git commands to generate.