Sync
This commit is contained in:
42
lib/lomka.py
Normal file
42
lib/lomka.py
Normal file
@@ -0,0 +1,42 @@
|
||||
class BCCCalculator:
|
||||
"""
|
||||
Класс для работы с BCC
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def xor_bcc(data):
|
||||
"""XOR BCC"""
|
||||
if isinstance(data, str):
|
||||
data = data.encode('utf-8')
|
||||
elif isinstance(data, list):
|
||||
data = bytes(data)
|
||||
|
||||
bcc = 0
|
||||
for byte in data:
|
||||
bcc ^= byte
|
||||
return bcc
|
||||
|
||||
@staticmethod
|
||||
def sum_bcc(data):
|
||||
"""Суммирование BCC"""
|
||||
if isinstance(data, str):
|
||||
data = data.encode('utf-8')
|
||||
elif isinstance(data, list):
|
||||
data = bytes(data)
|
||||
|
||||
total = sum(data)
|
||||
return total & 0xFF
|
||||
|
||||
@staticmethod
|
||||
def verify_bcc(data_with_bcc):
|
||||
"""
|
||||
Проверка BCC в данных
|
||||
"""
|
||||
if len(data_with_bcc) < 2:
|
||||
return False
|
||||
|
||||
data = data_with_bcc[:-1]
|
||||
expected_bcc = data_with_bcc[-1]
|
||||
calculated_bcc = BCCCalculator.xor_bcc(data)
|
||||
|
||||
return expected_bcc == calculated_bcc
|
||||
Reference in New Issue
Block a user