def calculate_crc(key): """Calculate the CRC16 and compare it.""" print_debug(3, "Starting to compute CRC-16-genibus") keylen = len(key) keystring = [chr(b) for b in key[:keylen - 2]] print_debug(3, "ASCII key: " + str(keystring)) crc = predefined.Crc("crc-16-genibus") crc.update(keystring) crc_keydata = int(crc.hexdigest(), 16) # Do it like the game does, because it's funny constant = [0x5D, 0x10, 0x7A, 0x33, 0x00, 0x77, 0x13, 0x92, 0xDE] constant.reverse() constant = [(~b) & 0xFF for b in constant] constant = [chr(b) for b in constant] crc = predefined.Crc("crc-16-genibus") crc.update(constant) crc_constantdata = int(crc.hexdigest(), 16) # It's always 0x00D3 # Mix CRC crc_key = crc_constantdata ^ crc_keydata ^ 0x62D3 print_debug( 3, "Key CRC: " + hex(crc_keydata) + ", constant CRC: " + hex(crc_constantdata) + ", final CRC: " + hex(crc_key)) return crc_key
def MODBUSCrc(payload): ''' Have MODBUS CRC of the payload. Reference from http://crcmod.sourceforge.net/crcmod.predefined.html ''' modbuscrc = predefined.Crc("modbus") modbuscrc.update(payload) return modbuscrc.crcValue
def try_parse(packet=""): packet_CRC = packet[-4:] check_CRC = detect.Crc('crc-16') check_CRC.update((packet[:-4]).encode('utf-8')) if packet_CRC == check_CRC.hexdigest(): p_components = packet[:-4].split(MeshConfiguration.separator) route = classes.Route.from_string(p_components[0], p_components[1], p_components[2], p_components[3]) command = classes.Command.from_string(p_components[4], p_components[5], p_components[6]) return Packet(route=route, command=command) else: raise exceptions.CorruptPacketException( ['CRC code did not match.', packet])
def computeHashProb(name_set, hash_function): probability = 1.0 hash_set = set() collisions = 0.0 for name in name_set: hash_fun = crc.Crc(hash_function) hash_fun.update(name) hash_res = hash_fun.hexdigest() if hash_res not in hash_set: hash_set.add(hash_res) else: collisions += 1.0 #print "collisions holds %.2f" % collisions if (name_set.__len__() is not 0): probability = collisions / name_set.__len__() return probability
def computeHash(self): hash_fun = crc.Crc(self.hash_func) hash_fun.update(self.nameEntry.get_name(delimiter="")) return hash_fun.hexdigest()
def crc16(self): crc = detect.Crc('crc-16') crc.update(str(self).encode('utf-8')) return crc.hexdigest()
#!/usr/bin/env python # -*- coding: utf-8 -*- from crcmod import predefined def MODBUSCrc(payload): ''' Have MODBUS CRC of the payload. Reference from http://crcmod.sourceforge.net/crcmod.predefined.html ''' modbuscrc = predefined.Crc("modbus") modbuscrc.update(payload) return modbuscrc.crcValue if __name__ == "__main__": modbuscrc = predefined.Crc("modbus") modbuscrc.update(b"123456789") print(modbuscrc) print(hex(MODBUSCrc(b"123456789")).upper()) print(hex(MODBUSCrc(b"987654321")).upper())