25 lines
640 B
Python
25 lines
640 B
Python
|
from network.enums import types as TYPE
|
||
|
# TODO: Single function if Object's state is not needed
|
||
|
|
||
|
class CheckSum:
|
||
|
def __init__(self):
|
||
|
self.clear()
|
||
|
|
||
|
def clear(self):
|
||
|
self.sum = 0
|
||
|
self.r = 55665
|
||
|
self.c1 = 52845
|
||
|
self.c2 = 22719
|
||
|
|
||
|
def add(self, byteArray):
|
||
|
[self._add(b) for b in byteArray]
|
||
|
|
||
|
def _add(self, byte):
|
||
|
cipher = byte ^ (self.r >> 8)
|
||
|
cipher &= TYPE.MAX_UCHAR # [BYTE MAGIC]
|
||
|
self.r = (cipher + self.r) * self.c1 + self.c2
|
||
|
self.r &= TYPE.MAX_USHORT # [BYTE MAGIC]
|
||
|
self.sum += cipher
|
||
|
|
||
|
def get(self):
|
||
|
return self.sum
|