This commit is contained in:
2026-01-17 23:34:35 +09:00
parent f3af97c9e5
commit 5b2915d1df
5 changed files with 64 additions and 7 deletions

42
lib/lomka.py Normal file
View 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