Custom stations: Revisit checksum generation

Some AVRs do not allow an ID larger than 15 characters. Thanks to Marc for pointing that out.
Use a MD5 hash (128), XOR fold it (64) and cut it to size (48). This way we _should_ still have enough uniqueness and can generate a checksum which fits the limits.
This commit is contained in:
milaq 2019-08-25 13:42:31 +02:00
parent 19e0ff8649
commit c6f4fe1691

View file

@ -75,7 +75,12 @@ def get_stations_by_category(category):
return stations
def get_checksum(feed):
def get_checksum(feed, charlimit=12):
hash_feed = feed.encode()
hash_object = hashlib.sha256(hash_feed)
return hash_object.hexdigest()
hash_object = hashlib.md5(hash_feed)
digest = hash_object.digest()
xor_fold = bytearray(digest[:8])
for i, b in enumerate(digest[8:]):
xor_fold[i] ^= b
digest_xor_fold = ''.join(format(x, '02x') for x in bytes(xor_fold))
return digest_xor_fold[:charlimit]