chore: Get rid of the util.b function

It doesn't really gave a use in the world of bytestrings.
This commit is contained in:
Gergely Polonkai 2023-11-03 07:44:28 +01:00
parent 5a3af65927
commit 86a9fa300c

View File

@ -54,13 +54,6 @@ def split_chunks(seq: Sequence[T], n: int) -> Generator[Sequence[T], None, None]
seq = seq[n:] seq = seq[n:]
# Stolen from PyCypto (Public Domain)
def b(s: str) -> bytes:
"""Shorthand for s.encode("latin-1")"""
return s.encode("latin-1") # utf-8 would cause some side-effects we don't want
def long_to_bytes(n: int, blocksize: int = 0) -> bytes: def long_to_bytes(n: int, blocksize: int = 0) -> bytes:
"""Convert a long integer to a byte string. """Convert a long integer to a byte string.
@ -69,7 +62,7 @@ def long_to_bytes(n: int, blocksize: int = 0) -> bytes:
""" """
# after much testing, this algorithm was deemed to be the fastest # after much testing, this algorithm was deemed to be the fastest
s = b("") s = b""
pack = struct.pack pack = struct.pack
while n > 0: while n > 0:
@ -78,11 +71,11 @@ def long_to_bytes(n: int, blocksize: int = 0) -> bytes:
# strip off leading zeros # strip off leading zeros
for i, c in enumerate(s): for i, c in enumerate(s):
if c != b("\000")[0]: if c != 0:
break break
else: else:
# only happens when n == 0 # only happens when n == 0
s = b("\000") s = b"\x00"
i = 0 i = 0
s = s[i:] s = s[i:]
@ -90,7 +83,7 @@ def long_to_bytes(n: int, blocksize: int = 0) -> bytes:
# add back some pad bytes. this could be done more efficiently w.r.t. the # add back some pad bytes. this could be done more efficiently w.r.t. the
# de-padding being done above, but sigh... # de-padding being done above, but sigh...
if blocksize > 0 and len(s) % blocksize: if blocksize > 0 and len(s) % blocksize:
s = (blocksize - len(s) % blocksize) * b("\000") + s s = (blocksize - len(s) % blocksize) * b"\x00" + s
return s return s
@ -107,7 +100,7 @@ def bytes_to_long(s: bytes) -> int:
if length % 4: if length % 4:
extra = 4 - length % 4 extra = 4 - length % 4
s = b("\000") * extra + s s = b"\x00" * extra + s
length = length + extra length = length + extra
for i in range(0, length, 4): for i in range(0, length, 4):