From f568fd459a397fd18f25d601bc5d1133a293b776 Mon Sep 17 00:00:00 2001 From: Ludvig Michaelsson Date: Fri, 24 Oct 2025 14:10:56 +0200 Subject: [PATCH] yubikit: refactor int2bytes Use the Python .to_bytes() function instead of custom algorithm. --- yubikit/core/__init__.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/yubikit/core/__init__.py b/yubikit/core/__init__.py index e139c6c9..fa146ccd 100644 --- a/yubikit/core/__init__.py +++ b/yubikit/core/__init__.py @@ -262,12 +262,8 @@ def require_version( def int2bytes(value: int, min_len: int = 0) -> bytes: - buf = [] - while value > 0xFF: - buf.append(value & 0xFF) - value >>= 8 - buf.append(value) - return bytes(reversed(buf)).rjust(min_len, b"\0") + min_len = max(min_len, (value.bit_length() + 7) // 8) + return value.to_bytes(min_len, "big") def bytes2int(data: bytes) -> int: