Files
opencaser/lib/lomka.py
2026-01-17 23:34:35 +09:00

42 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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