Skip to content

Commit

Permalink
add typing and validation using pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
vchrisb committed Oct 25, 2023
1 parent 48d8c66 commit 9993787
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 236 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black flake8 flake8-docstrings isort
pip install black flake8 flake8-docstrings isort pyright
- name: Run flake8
run: flake8
- name: Run isort
run: isort ./ --check
- name: Run black
run: black ./ --check
- name: Run pyright
run: pyright
- name: Run test install
run: pip install .
21 changes: 12 additions & 9 deletions e3dc/_RSCPEncryptDecrypt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations # required for python < 3.9

import math

from py3rijndael import RijndaelCbc, ZeroPadding
from py3rijndael import RijndaelCbc, ZeroPadding # type: ignore

KEY_SIZE = 32
BLOCK_SIZE = 32
Expand All @@ -12,7 +14,7 @@ class ParameterError(Exception):
pass


def zeroPad_multiple(string, value):
def zeroPad_multiple(string: bytes, value: int) -> bytes:
"""Zero padding string."""
length = len(string)
if length % value == 0:
Expand All @@ -21,7 +23,7 @@ def zeroPad_multiple(string, value):
return string.ljust(newL, b"\x00")


def truncate_multiple(string, value):
def truncate_multiple(string: bytes, value: int) -> bytes:
"""Truncating sting."""
length = len(string)
if length % value == 0:
Expand All @@ -33,22 +35,21 @@ def truncate_multiple(string, value):
class RSCPEncryptDecrypt:
"""A class for encrypting and decrypting RSCP data."""

def __init__(self, key):
def __init__(self, key: bytes):
"""Constructor of a RSCP encryption and decryption class.
Args:
key (str): RSCP encryption key
key (bytes): RSCP encryption key
"""
if len(key) > KEY_SIZE:
raise ParameterError("Key must be <%d bytes" % (KEY_SIZE))

self.key = key.ljust(KEY_SIZE, b"\xff")
self.encryptIV = b"\xff" * BLOCK_SIZE
self.decryptIV = b"\xff" * BLOCK_SIZE
self.remainingData = b""
self.oldDecrypt = b""

def encrypt(self, plainText):
def encrypt(self, plainText: bytes) -> bytes:
"""Method to encryt plain text."""
encryptor = RijndaelCbc(
self.key,
Expand All @@ -60,7 +61,9 @@ def encrypt(self, plainText):
self.encryptIV = encText[-BLOCK_SIZE:]
return encText

def decrypt(self, encText, previouslyProcessedData=None):
def decrypt(
self, encText: bytes, previouslyProcessedData: int | None = None
) -> bytes:
"""Method to decryt encrypted text."""
if previouslyProcessedData is None:
length = len(self.oldDecrypt)
Expand Down Expand Up @@ -92,4 +95,4 @@ def decrypt(self, encText, previouslyProcessedData=None):
padding=ZeroPadding(BLOCK_SIZE),
block_size=BLOCK_SIZE,
)
return decryptor.decrypt(toDecrypt)
return decryptor.decrypt(toDecrypt) # pyright: ignore [reportUnknownMemberType]
2 changes: 0 additions & 2 deletions e3dc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
__all__ = [
"E3DC",
"AuthenticationError",
"NotAvailableError",
"PollError",
"SendError",
"CommunicationError",
"RSCPAuthenticationError",
"RSCPKeyError",
Expand Down
Loading

0 comments on commit 9993787

Please sign in to comment.