TheAlgorithms/Python · error · ValueError
Input must be of length 32
Error message
Input must be of length 32
What it means
Thrown by to_little_endian() in the MD5 implementation when the input is not exactly 32 bytes. The helper reverses four 8-byte groups of a 32-byte (256-bit) chunk to produce little-endian ordering, so any other length has undefined group boundaries and is rejected upfront.
Source
Thrown at hashes/md5.py:38
Converts the given string to little-endian in groups of 8 chars.
Arguments:
string_32 {[string]} -- [32-char string]
Raises:
ValueError -- [input is not 32 char]
Returns:
32-char little-endian string
>>> to_little_endian(b'1234567890abcdfghijklmnopqrstuvw')
b'pqrstuvwhijklmno90abcdfg12345678'
>>> to_little_endian(b'1234567890')
Traceback (most recent call last):
...
ValueError: Input must be of length 32
"""
if len(string_32) != 32:
raise ValueError("Input must be of length 32")
little_endian = b""
for i in [3, 2, 1, 0]:
little_endian += string_32[8 * i : 8 * i + 8]
return little_endian
def reformat_hex(i: int) -> bytes:
"""
Converts the given non-negative integer to hex string.
Example: Suppose the input is the following:
i = 1234
The input is 0x000004d2 in hex, so the little-endian hex string is
"d2040000".
Arguments:View on GitHub (pinned to f5988cc097)
Solutions
- Do not call to_little_endian directly; run the full md5_me(message) pipeline which slices correctly.
- If you must call it, pass exactly 32 bytes: slice your buffer into 32-byte chunks first.
- For arbitrary-width endian swapping use int.from_bytes(..., 'little') / struct instead.
Example fix
# before
le = to_little_endian(raw_bytes) # raw_bytes has arbitrary length
# after
for pos in range(0, len(raw_bytes), 32):
le = to_little_endian(raw_bytes[pos:pos + 32]) Defensive patterns
Strategy: validation
Validate before calling
assert len(chunk) == 32, f"to_little_endian needs 32 bytes, got {len(chunk)}" Type guard
def is_32_bytes(buf: bytes) -> bool:
return isinstance(buf, (bytes, bytearray)) and len(buf) == 32 Try / catch
try:
le = to_little_endian(chunk)
except ValueError:
raise ValueError(f"expected 32-byte chunk, got {len(chunk)} bytes") from None Prevention
- Prefer the public md5_me(message) API over internal helpers.
- Slice buffers into fixed 32-byte chunks before swapping.
- Use int.from_bytes(x, 'little') for arbitrary-width endian conversion.
When it happens
Trigger: Calling to_little_endian(b'1234567890') (10 bytes) or with any string whose length != 32. In practice this helper is called internally by get_block_words on 32-bit (32-char) slices, so a direct call with an arbitrary buffer triggers it.
Common situations: Using this internal helper as a generic endian-swap utility on arbitrary-length buffers; passing a hex string or a bit string of the wrong width instead of the 32-char block slice produced by preprocessing.
Related errors
- Input must be non-negative
- Input must have length that's a multiple of 512
- Shift must be non-negative
- number must be positive
- The value of input must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a12da8a35e25b28c.
Report an issue: GitHub.