import dataclasses from typing import Literal import bitstring # Reference for H10301 card format: # https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf class InvalidHexCode(Exception): pass class Not26Bit(InvalidHexCode): def __init__(self) -> None: super().__init__("Card number > 26 bits") class InvalidParity(InvalidHexCode): def __init__(self, even_odd: Literal["even", "odd"]) -> None: super().__init__(f"Bad {even_odd} parity") @dataclasses.dataclass(frozen=True) class Credential: bits: bitstring.Bits @classmethod def from_code(cls, facility=int, card_number=int) -> "Credential": bits = bitstring.pack( "0b000000, 0b0, uint:8=facility, uint:16=card_number, 0b0", facility=facility, card_number=card_number, ) bits[6] = bits[7:19].count(1) % 2 # even parity bits[31] = bits[19:31].count(0) % 2 # odd parity return cls(bits) @classmethod def from_hex(cls, hex_code: str) -> "Credential": bits = bitstring.Bits(hex=hex_code) if bits[:6].any(1): raise Not26Bit if bits[6] != bits[7:19].count(1) % 2: raise InvalidParity("even") if bits[31] != (bits[19:31].count(0) % 2): raise InvalidParity("odd") return cls(bits) @property def facility_code(self) -> int: return self.bits[7:15].uint @property def card_number(self) -> int: return self.bits[15:31].uint @property def hex(self) -> str: return self.bits.hex.upper()