Selfmade_Crypto_Socket module
SecureClient
connected_users: list
property
readonly
list of all users in the self.users_keys dict
__init__(self)
special
Attributes:
Name | Type | Description |
---|---|---|
self.user |
str |
should contain the username of the Client |
self.pw |
str |
should contain the password of the Client |
self.seperators |
list |
bytes separating the the received information's |
self.own_keys |
list |
contains the Keys, first the private, second the public |
self.server_key |
RSA_Public_Key |
should contain the Key of the Server |
self.users_keys |
dict |
should contain as a key the username, as the value the public Key |
get_key(self, username)
function returns a key for a username
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str |
username used for finding the key |
required |
Returns:
Type | Description |
---|---|
RSA_PUBLIC_KEY |
returns a RSA Public key |
login_data(self, user, password)
overwrites self.user and self.pw with given arguments
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str |
username |
required |
password |
|
password |
required |
on_connect(self)
this function is executed on connection
recv_data(self)
function collects all incoming data
Returns:
Type | Description |
---|---|
Socket_Response |
tuple, first the target, second the type, third the data |
send_data(self, target, type, data, username=None, key=None, encr_rest=True)
Sends data to the the username or given socket encrypted with a key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
bytes |
information used for target |
required |
type |
bytes |
information used for type |
required |
data |
bytes |
data to send |
required |
username |
Optional[str] |
the username of the user to send, if not given, you must give a key |
None |
key |
Optional[simplesockets._support_files.RSA.RSA_PUBLIC_KEY] |
the RSA Public Key used for encryption, if not given, you must give an username |
None |
encr_rest |
bool |
if type and target should be encrypted |
True |
Returns:
Type | Description |
---|---|
bool |
returns True if the sending was successful |
setup(self, target_ip, target_port=25567, recv_buffer=2048, on_connect=None, on_disconnect=None, on_receive=None)
function sets up the Client
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_ip |
str |
IP the Client should connect to |
required |
target_port |
Optional[int] |
PORT the Client should connect to |
25567 |
recv_buffer |
Optional[int] |
PORT the Client should connect to |
2048 |
on_connect |
Optional[Callable] |
function that will be executed on connection, it takes not arguments |
None |
on_disconnect |
Optional[Callable] |
function that will be executed on disconnection, it takes no arguments |
None |
on_receive |
Optional[Callable] |
function that will be executed on receive, it takes the received data as an argument |
None |
SecureServer
__init__(self, max_connections=None)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_connections |
Optional[int] |
how many Clients can connect to the Server |
None |
Attributes:
Name | Type | Description |
---|---|---|
self.users |
|
contains the address as the key and the username as the value |
self.own_keys |
|
contains the private Key and the public Key |
self.client_keys |
|
contains the username as the key and the public Key as the value |
self.to_send_client_keys |
|
contains the username as the key and the public Key as bytes as the value |
self.seperators |
|
contains bytes separating the received information's |
self.filepath |
|
path to a json file containing usernames and their passwords |
self.indent |
|
indent used for json |
add_user(self, username, pw, get=None)
adds a username and a password to the already given json file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str |
the username |
required |
pw |
str |
the password |
required |
get |
Optional[str] |
calls the dict.get() function with the given parameter |
None |
check_user(self, username, pw, get=None)
checks if the given username and password are in the json file and are valid
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str |
username to check |
required |
pw |
str |
password to check (not yet hashed) |
required |
get |
Optional[str] |
calls the dict.get() function with the given parameter |
None |
Returns:
Type | Description |
---|---|
bool |
returns True if username and password are valid |
decrypt_data(self, data)
function can be used for decrypting the received data with the Server RSA Private Key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes |
data which should be decrypted |
required |
Returns:
Type | Description |
---|---|
bytes |
returns decrypted data |
get_address_by_user(self, user)
uses the username to get the address
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str |
username |
required |
Returns:
Type | Description |
---|---|
tuple |
returns the address |
get_client_keys(self)
Returns:
Type | Description |
---|---|
dict |
returns a copy of the dictonary containing clients and there importable keys |
get_public_key(self, username)
gets the public key from the username
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str |
username |
required |
Returns:
Type | Description |
---|---|
RSA_PUBLIC_KEY |
returns a RSA Public Key |
load_users(self, get=None)
this function loads the json file with the users and their passwords
Parameters:
Name | Type | Description | Default |
---|---|---|---|
get |
Optional[str] |
calls the dict.get() function with the given parameter |
None |
Returns:
Type | Description |
---|---|
dict |
returns a dict |
on_connect(self, address)
this function gets call when a new client connects to the Server
on_disconnect(self, address)
this function gets called when a client gets disconnected
Parameters:
Name | Type | Description | Default |
---|---|---|---|
address |
tuple |
the address, containing Ip and Port |
required |
recv_data(self, client)
function collects incoming data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
Server_Client |
client socket |
required |
Returns:
Type | Description |
---|---|
Socket_Response |
tuple: first the target, second the type, third the data |
save_users(self, users, get=None)
saves the users and their password dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
users |
dict |
the users dictionary |
required |
get |
Optional[str] |
calls the dict.get() function with the given parameter |
None |
send_data(self, target, type, data, username=None, key=None, client=None, encr_data=True, encr_rest=True)
function sends encrypted data to a user or socket
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
bytes |
information used for target |
required |
type |
bytes |
information used for type |
required |
data |
bytes |
data to send |
required |
username |
Optional[str] |
the username of the user to send, if not given, you must give a socket and a key |
None |
key |
Optional[simplesockets._support_files.RSA.RSA_PUBLIC_KEY] |
the RSA Public Key used for encryption, if not given, you must give an username |
None |
client |
Optional[simplesockets.simple_sockets.Server_Client] |
client object, if not given, you must give an username |
None |
encr_data |
Optional[bool] |
if the data should be encrypted |
True |
encr_rest |
bool |
if the target and type should be encrypted |
True |
Returns:
Type | Description |
---|---|
bool |
returns True if the sending was successful |
setup(self, ip='127.0.0.1', port=25567, listen=5, recv_buffer=2048, handle_client=None, on_connect=None, on_disconnect=None, on_receive=None, filepath=None)
prepares the Server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str |
absolut Path of the json file containing usernames and their passwords |
None |
ip |
Optional[str] |
IP of the Server |
'127.0.0.1' |
port |
Optional[int] |
PORT the Server should listen on |
25567 |
listen |
Optional[int] |
parameter for socket.listen() |
5 |
recv_buffer |
Optional[int] |
the receive buffer used for socket.recv() |
2048 |
handle_client |
Optional[Callable] |
the function for handling the Clients, should be left as None |
None |
on_connect |
Optional[Callable] |
function that will be executed on connection, it takes the address(tuple) as an argument |
None |
on_disconnect |
Optional[Callable] |
function that will be executed on disconnection, it takes the address(tuple) as an argument |
None |
on_receive |
Optional[Callable] |
function that will be executed on receive, it takes the clientsocket, address, received data |
None |
Exceptions:
Type | Description |
---|---|
Exception |
When filepath is missing and no alternative on_connect function is given |