]>
git.datanom.net - securemail.git/blob - user.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/>.
21 from db
import DBInterface
as DBI
22 from cryptonize
import Cryptonize
23 from nacl
.public
import PublicKey
25 class NoSuchUser(Exception):
30 Class implementing the backend users
32 def __init__(self
, key
=None):
40 cipher
= crypto
.symmetric_encrypt(key
, pickle
.dumps(self
))
41 DBI
.store_user(crypto
.generate_hash(key
), cipher
)
45 cipher
= DBI
.load_user(crypto
.generate_hash(key
))
47 raise NoSuchUser('{0}: User not found'.format(key
))
48 plain
= crypto
.symmetric_decrypt(key
, cipher
)
50 obj
= pickle
.loads(plain
)
51 self
.__dict
__.update(obj
.__dict
__)
52 except pickle
.UnpicklingError
as e
:
55 def add_pubkey(self
, email
, key
):
56 if email
not in self
.pubkeys
:
57 self
.pubkeys
[email
] = key
.encode()
59 raise KeyError('{0}: Exists'.format(email
))
61 def update_pubkey(self
, email
, key
):
62 self
.pubkeys
[email
] = key
.encode()
64 def delete_pubkey(self
, email
):
65 if email
in self
.pubkeys
:
66 del self
.pubkeys
[email
]
68 def get_pubkey(self
, email
):
69 if email
in self
.pubkeys
:
70 key
= self
.pubkeys
[email
]
90 def email(self
, email
):
98 def pubkeys(self
, pubkeys
):
99 if type(pubkeys
) is not type({}):
100 raise ValueError('Not dictionary')
101 self
._pubkeys
= pubkeys
103 if __name__
== '__main__':
106 for attr
, value
in u
.__dict
__.items():
107 print ('{0}: {1}'.format(attr
, value
))
108 print ('{0} - {1} - {2}'.format(u
.name
, u
.email
, u
.pubkeys
))
111 key
+= '{0}'.format(i
)
114 u
.email
= 'testname1@securemail.icu'
115 u
.pubkeys
= {'test': 'some test', 'test1': 'some test 1'}
120 for attr
, value
in u
.__dict
__.items():
121 print ('{0}: {1}'.format(attr
, value
))
122 print ('{0} - {1} - {2}'.format(u
.name
, u
.email
, u
.pubkeys
))
123 from nacl
.public
import Box
125 keypair1
= c
.get_key_pair()
126 keypair2
= c
.get_key_pair()
128 u
.add_pubkey('test', keypair2
[1])
130 u
.update_pubkey('test', keypair2
[1])
131 bob_box
= Box(keypair1
[0], u
.get_pubkey('test'))
132 message
= "Kill all humans æøåÅØÆ"
133 encrypted
= bob_box
.encrypt(message
.encode())
134 alice_box
= Box(keypair2
[0], keypair1
[1])
135 plaintext
= alice_box
.decrypt(encrypted
)
136 print (plaintext
.decode())
138 # key = 'æselØre' #c.get_random_key()
139 # cipher = c.symmetric_encrypt(key, pickle.dumps(u))
140 # obj = pickle.loads(c.symmetric_decrypt(key, cipher))
141 # for attr, value in obj.__dict__.items():
142 # print ('{0}: {1}'.format(attr, value))
146 u
.email
= 'testname@securemail.icu'
148 except Exception as e
:
This page took 0.120158 seconds and 6 git commands to generate.