]>
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
24 from nacl
import __version__
as NACL_VERSION
26 class NoSuchUser(Exception):
31 Class implementing the backend users
33 def __init__(self
, key
=None):
41 if NACL_VERSION
< "1.1.0":
42 from nacl
.utils
import random
43 from nacl
.public
import SecretBox
44 nonce
= random(SecretBox
.NONCE_SIZE
)
45 cipher
= crypto
.symmetric_encrypt(key
, pickle
.dumps(self
), nonce
)
47 cipher
= crypto
.symmetric_encrypt(key
, pickle
.dumps(self
))
48 DBI
.store_user(crypto
.generate_hash(key
), cipher
)
52 cipher
= DBI
.load_user(crypto
.generate_hash(key
))
54 raise NoSuchUser('{0}: User not found'.format(key
))
55 plain
= crypto
.symmetric_decrypt(key
, cipher
)
57 obj
= pickle
.loads(plain
)
58 self
.__dict
__.update(obj
.__dict
__)
59 except pickle
.UnpicklingError
as e
:
62 def add_pubkey(self
, email
, key
):
63 if email
not in self
.pubkeys
:
64 self
.pubkeys
[email
] = key
.encode()
66 raise KeyError('{0}: Exists'.format(email
))
68 def update_pubkey(self
, email
, key
):
69 self
.pubkeys
[email
] = key
.encode()
71 def delete_pubkey(self
, email
):
72 if email
in self
.pubkeys
:
73 del self
.pubkeys
[email
]
75 def get_pubkey(self
, email
):
76 if email
in self
.pubkeys
:
77 key
= self
.pubkeys
[email
]
97 def email(self
, email
):
105 def pubkeys(self
, pubkeys
):
106 if type(pubkeys
) is not type({}):
107 raise ValueError('Not dictionary')
108 self
._pubkeys
= pubkeys
110 if __name__
== '__main__':
113 for attr
, value
in u
.__dict
__.items():
114 print ('{0}: {1}'.format(attr
, value
))
115 print ('{0} - {1} - {2}'.format(u
.name
, u
.email
, u
.pubkeys
))
118 key
+= '{0}'.format(i
)
121 u
.email
= 'testname1@securemail.icu'
122 u
.pubkeys
= {'test': 'some test', 'test1': 'some test 1'}
127 for attr
, value
in u
.__dict
__.items():
128 print ('{0}: {1}'.format(attr
, value
))
129 print ('{0} - {1} - {2}'.format(u
.name
, u
.email
, u
.pubkeys
))
130 from nacl
.public
import Box
132 keypair1
= c
.get_key_pair()
133 keypair2
= c
.get_key_pair()
135 u
.add_pubkey('test', keypair2
[1])
137 u
.update_pubkey('test', keypair2
[1])
138 bob_box
= Box(keypair1
[0], u
.get_pubkey('test'))
139 message
= "Kill all humans æøåÅØÆ"
140 encrypted
= bob_box
.encrypt(message
.encode())
141 alice_box
= Box(keypair2
[0], keypair1
[1])
142 plaintext
= alice_box
.decrypt(encrypted
)
143 print (plaintext
.decode())
145 # key = 'æselØre' #c.get_random_key()
146 # cipher = c.symmetric_encrypt(key, pickle.dumps(u))
147 # obj = pickle.loads(c.symmetric_decrypt(key, cipher))
148 # for attr, value in obj.__dict__.items():
149 # print ('{0}: {1}'.format(attr, value))
153 u
.email
= 'testname@securemail.icu'
155 except Exception as e
:
This page took 0.178892 seconds and 6 git commands to generate.