-
Notifications
You must be signed in to change notification settings - Fork 0
/
h10301.py
64 lines (49 loc) · 1.87 KB
/
h10301.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class H10301:
"""
Creates an HID card type H10301 that uses the Wiegand protocol (https://en.wikipedia.org/wiki/Wiegand_interface).
"""
DATA_LENGTH: int = 6
FACILITY_MIN_VALUE: int = 0
FACILITY_MAX_VALUE: int = 255
FACILITY_BITS: int = 8
CARD_MIN_VALUE: int = 0
CARD_MAX_VALUE: int = 65535
CARD_BITS: int = 16
BIT_COUNT_FOR_PARITY = 12
def __init__(self, facility: int, card: int):
assert(self.__validate_facility(facility))
assert(self.__validate_card(card))
self.facility = facility
self.card = card
def data(self) -> int:
return (self.facility << self.CARD_BITS) + self.card
def bin(self) -> str:
return bin(self.data())[2:]
def bits(self) -> list[int]:
return [int(bit) for bit in self.bin()]
def hex(self) -> str:
return hex(self.data())[2:].zfill(self.DATA_LENGTH)
def hex_flipper(self) -> str:
chunks = [self.hex().upper()[i:i+2] for i in range(0, self.DATA_LENGTH, 2)]
return ' '.join(chunks)
def parity_even(self):
return self.__parity(self.bits()[:self.BIT_COUNT_FOR_PARITY + 1], False)
def parity_odd(self):
return self.__parity(self.bits()[self.BIT_COUNT_FOR_PARITY:], True)
def flipper(self):
return """Filetype: Flipper RFID key
Version: 1
Key type: H10301
Data: %s
""" % (self.hex_flipper())
def __validate_facility(self, data: int) -> bool:
if data < self.FACILITY_MIN_VALUE or data > self.FACILITY_MAX_VALUE:
return False
return True
def __validate_card(self, data: int) -> bool:
if data < self.CARD_MIN_VALUE or data > self.CARD_MAX_VALUE:
return False
return True
@staticmethod
def __parity(data: list[int], odd: bool):
return ((sum(data) % 2) + odd) % 2