Skip to content

Commit

Permalink
Merge pull request #55 from gridhead/cmnt
Browse files Browse the repository at this point in the history
Refine the docstrings are made available across all functions and classes
  • Loading branch information
gridhead authored Nov 11, 2024
2 parents e3b0242 + ab2b78e commit 3aa6a39
Show file tree
Hide file tree
Showing 17 changed files with 608 additions and 66 deletions.
36 changes: 35 additions & 1 deletion expedite/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@


def derive_code(password: str = standard.client_pswd, salt: bytes = standard.client_salt) -> bytes:
"""
Derive byte convertion from password and salt composition
:param password: Password provided by the clients
:param salt: Byte array for additional protection
:return: Cryptography HMAC code
"""
kdfo = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000, backend=default_backend())
return kdfo.derive(password.encode())


def encr_bite(data: bytes = b"", code: bytes = standard.client_code, invc: bytes = standard.client_invc) -> bytes:
"""
Encrypt file chunks using converted cryptography HMAC code
:param data: Chunks to read, encrypt and deliver
:param code: Generated cryptography HMAC code
:param invc: Initialization vector for added protection
:return: Encrypted bytes
"""
cipher = Cipher(algorithms.AES(code), modes.CBC(invc), backend=default_backend())
encrob = cipher.encryptor()
padder = padding.PKCS7(algorithms.AES.block_size).padder()
Expand All @@ -47,6 +62,14 @@ def encr_bite(data: bytes = b"", code: bytes = standard.client_code, invc: bytes


def decr_bite(data: bytes = b"", code: bytes = standard.client_code, invc: bytes = standard.client_invc) -> bytes:
"""
Decrypt file chunks using converted cryptography HMAC code
:param data: Chunks to collect, decrypt and save
:param code: Generated cryptography HMAC code
:param invc: Initialization vector for added protection
:return: Decrypted bytes
"""
try:
cipher = Cipher(algorithms.AES(code), modes.CBC(invc), backend=default_backend())
decrob = cipher.decryptor()
Expand All @@ -58,6 +81,11 @@ def decr_bite(data: bytes = b"", code: bytes = standard.client_code, invc: bytes


def encr_metadata() -> bytes:
"""
Encrypt metadata to deliver before file contents
:return: Encrypted bytes
"""
standard.client_invc, standard.client_salt = os.urandom(16), os.urandom(16)
data = dumps({"call": "meta", "name": standard.client_filename, "size": standard.client_filesize, "chks": len(standard.client_bind)-1})
standard.client_code = derive_code(standard.client_pswd, standard.client_salt)
Expand All @@ -66,7 +94,13 @@ def encr_metadata() -> bytes:
return standard.client_invc + standard.client_salt + endt


def decr_metadata(pack: bytes = b"") -> tuple:
def decr_metadata(pack: bytes = b"") -> tuple[str, int, bytes]:
"""
Decrypt metadata to collect before file contents
:param pack: Chunks to decrypt
:return: Decrypted chunks
"""
standard.client_invc, standard.client_salt = pack[0:16], pack[16:32]
data = pack[32:]
standard.client_code = derive_code(standard.client_pswd, standard.client_salt)
Expand Down
37 changes: 35 additions & 2 deletions expedite/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,36 @@


from os.path import basename, exists, getsize
from typing import Union

from expedite.client.auth import decr_bite, encr_bite
from expedite.config import standard


def find_size() -> int:
"""
Retrieve the file size using the file location
:return: Size of the intended file
"""
return getsize(standard.client_file)


def find_name() -> str:
"""
Retrieve the file name using the file location
:return: Name of the intended file
"""
return basename(standard.client_file)


def ease_size(size: Union[int, float]) -> str:
def ease_size(size: int | float) -> str:
"""
Retrieve the file size in human-readable format
:param size: Size in byte count format
:return: Size in human-readable format
"""
unitlist = ["B", "KB", "MB", "GB", "TB", "PB"]
indx, opsz = 0, size
if size == 0:
Expand All @@ -48,6 +63,11 @@ def ease_size(size: Union[int, float]) -> str:


def bite_file() -> list:
"""
Retrieve the list of read ranges from chunk size
:return: List of read ranges
"""
init, size, bite = 0, standard.client_filesize, []
while init < size:
bite.append(init)
Expand All @@ -60,6 +80,13 @@ def bite_file() -> list:


def read_file(init: int = 0, fina: int = 0) -> bytes:
"""
Retrieve the chunk from the provided byte range
:param init: Starting byte index for reading
:param fina: Stopping byte index for reading
:return: Chunks to read
"""
if exists(standard.client_file):
with open(standard.client_file, "rb") as file:
file.seek(init)
Expand All @@ -72,6 +99,12 @@ def read_file(init: int = 0, fina: int = 0) -> bytes:


def fuse_file(pack: bytes = b"") -> bool:
"""
Create and join the chunks on the storage device
:param pack: Chunks to save
:return:
"""
if not standard.client_fileinit:
mode, standard.client_fileinit = "wb", True
else:
Expand Down
14 changes: 12 additions & 2 deletions expedite/client/bridge/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
from expedite.client.bridge.room import MainWindow


def load_custom_font():
def load_custom_font() -> None:
"""
Populate the application database with custom fonts
:return:
"""
fontlist = [
":font/font/sans-bold.ttf",
":font/font/sans-rlar.ttf",
Expand All @@ -42,7 +47,12 @@ def load_custom_font():
QFontDatabase.addApplicationFont(indx)


def main():
def main() -> None:
"""
Start the worker module to start the transfer service
:return:
"""
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
QApplication.setStyle("Fusion")
app = QApplication(sys.argv)
Expand Down
Loading

0 comments on commit 3aa6a39

Please sign in to comment.