Properly handle parity bits in codeToHex

This commit is contained in:
Adam Goldsmith 2019-11-08 14:46:23 -05:00
parent 667260831c
commit 8367c8bbc1

View File

@ -1,21 +1,35 @@
#!/usr/bin/env python3
import bitstring
import requests
import csv
from io import StringIO
from common import *
def hexToCode(hex):
b = bin(int(hex, 16))[2:]
facility = int(b[0:8], 2)
code = int(b[9:24], 2)
# Reference for H10301 card format:
# https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
def hexToCode(h):
b = bitstring.Bits(hex=h)
facility = b[7:15].uint
code = b[15:31].uint
return ((facility, code))
def codeToHex(facility, code):
return "{:08X}".format(int(bin(facility)[2:] + "0" + bin(code)[2:] + "1", 2))
b = bitstring.pack('0b000000, uint:1, uint:8, uint:16, uint:1', 0,
facility, code, 0)
# calculate parity bits
b[6] = b[7:19].count(1) % 2 # even parity
b[31] = not (b[19:31].count(1) % 2) # odd parity
return b.hex.upper()
# hexToCode("01E29DA1") <-> codeToHex(241, 20176)
def forEachDoor(fxn):
for door in doors.values():
print(door.name)