From ab1d58d49af5889de27f42c20d14281cb518eb13 Mon Sep 17 00:00:00 2001 From: Rob Berwick Date: Sun, 17 Nov 2024 17:06:02 +0000 Subject: [PATCH] fix: Fix infoblock setting bugs * restrict info block data string to 31 bytes * correctly handle empty string --- src/blinkstick/utilities.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/blinkstick/utilities.py b/src/blinkstick/utilities.py index 095c3c0..a828fc1 100644 --- a/src/blinkstick/utilities.py +++ b/src/blinkstick/utilities.py @@ -8,11 +8,10 @@ def string_to_info_block_data(data: str) -> bytes: @rtype: byte[32] @return: It fills the rest of bytes with zeros. """ - byte_array = bytearray([1]) - for c in data: - byte_array.append(ord(c)) + info_block_data = data[:31] + byte_array = bytearray([1] + [0] * 31) - for i in range(32 - len(data)): - byte_array.append(0) + for i, c in enumerate(info_block_data): + byte_array[i + 1] = ord(c) return bytes(byte_array)