Skip to content

Commit

Permalink
Merge pull request #48 - improve code quality
Browse files Browse the repository at this point in the history
improve code quality
  • Loading branch information
vchrisb authored Jan 21, 2022
2 parents 3f5cf24 + 709ea4a commit 3b2d75d
Show file tree
Hide file tree
Showing 13 changed files with 3,593 additions and 3,036 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Run validation

on: [push, pull_request]

jobs:
validate:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black flake8 isort
- name: Run flake8
run: flake8
- name: Run isort
run: isort ./ --check
- name: Run black
run: black ./ --check
- name: Run test install
run: pip install .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# python-e3dc

**NOTE: This branch is now for python3 and it has a different API than the old python2.7**
**NOTE: With Release 0.6.0 at least Python 3.7 is required**

Python API for querying E3/DC systems, either through the manufacturer's portal or directly via RSCP connection

Expand Down
78 changes: 48 additions & 30 deletions e3dc/_RSCPEncryptDecrypt.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,87 @@
from py3rijndael import RijndaelCbc, ZeroPadding

import math

from py3rijndael import RijndaelCbc, ZeroPadding

KEY_SIZE = 32
BLOCK_SIZE = 32


class ParameterError(Exception):
pass


def zeroPad_multiple(string, value):
l = len(string)
if l % value == 0:
return string
newL = int(value * math.ceil(float(l)/value))
return string.ljust(newL, b'\x00')
newL = int(value * math.ceil(float(l) / value))
return string.ljust(newL, b"\x00")


def truncate_multiple(string, value):
l = len(string)
if l % value == 0:
return string
newL = int(value * math.floor(float(l)/value))
newL = int(value * math.floor(float(l) / value))
return string[:newL]


class RSCPEncryptDecrypt:
def __init__(self, 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''

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):
encryptor = RijndaelCbc(self.key, self.encryptIV, padding=ZeroPadding(BLOCK_SIZE), block_size = BLOCK_SIZE)
encText = encryptor.encrypt( plainText )
encryptor = RijndaelCbc(
self.key,
self.encryptIV,
padding=ZeroPadding(BLOCK_SIZE),
block_size=BLOCK_SIZE,
)
encText = encryptor.encrypt(plainText)
self.encryptIV = encText[-BLOCK_SIZE:]
return encText
def decrypt(self, encText, previouslyProcessedData = None):

def decrypt(self, encText, previouslyProcessedData=None):
if previouslyProcessedData is None:
l = len(self.oldDecrypt)
if l % BLOCK_SIZE == 0:
previouslyProcessedData = l
else:
previouslyProcessedData = int(BLOCK_SIZE * math.floor(l/BLOCK_SIZE))
#print previouslyProcessedData
previouslyProcessedData = int(BLOCK_SIZE * math.floor(l / BLOCK_SIZE))

# print previouslyProcessedData
# previouslyProcessedData was passed by the parent: it means that a frame was decoded and there was some data left. This does not include the padding zeros
if previouslyProcessedData % BLOCK_SIZE != 0:
previouslyProcessedData = int(BLOCK_SIZE * math.ceil(previouslyProcessedData/BLOCK_SIZE))

previouslyProcessedData = int(
BLOCK_SIZE * math.ceil(previouslyProcessedData / BLOCK_SIZE)
)

remainingData = self.oldDecrypt[previouslyProcessedData:]
if self.oldDecrypt != b'':
self.decryptIV = self.oldDecrypt[previouslyProcessedData - BLOCK_SIZE:previouslyProcessedData]

self.oldDecrypt = encText # save current block

if self.oldDecrypt != b"":
self.decryptIV = self.oldDecrypt[
previouslyProcessedData - BLOCK_SIZE : previouslyProcessedData
]

self.oldDecrypt = encText # save current block

toDecrypt = truncate_multiple(remainingData + encText, BLOCK_SIZE)
decryptor = RijndaelCbc(self.key, self.decryptIV, padding=ZeroPadding(BLOCK_SIZE), block_size = BLOCK_SIZE)
decryptor = RijndaelCbc(
self.key,
self.decryptIV,
padding=ZeroPadding(BLOCK_SIZE),
block_size=BLOCK_SIZE,
)
return decryptor.decrypt(toDecrypt)
if __name__ == '__main__':


if __name__ == "__main__":
ed = RSCPEncryptDecrypt(b"love")
enc = ed.encrypt(b"hello")
print(enc)
Expand Down
18 changes: 13 additions & 5 deletions e3dc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
"""

from ._e3dc import E3DC, AuthenticationError, PollError
from ._e3dc_rscp_web import SocketNotReady, RequestTimeoutError
from ._e3dc_rscp_local import RSCPAuthenticationError, CommunicationError
from ._e3dc_rscp_local import CommunicationError, RSCPAuthenticationError
from ._e3dc_rscp_web import RequestTimeoutError, SocketNotReady
from ._rscpLib import FrameError



__version__ = "0.5.2"
__all__ = [
E3DC,
AuthenticationError,
PollError,
CommunicationError,
RSCPAuthenticationError,
RequestTimeoutError,
SocketNotReady,
FrameError,
]
__version__ = "0.6.0"
Loading

0 comments on commit 3b2d75d

Please sign in to comment.