TheAlgorithms/Python · error · ValueError

Input must have length that's a multiple of 512

Error message

Input must have length that's a multiple of 512

What it means

Thrown by get_block_words() in the MD5 implementation when the input bit string's length is not a multiple of 512. MD5 consumes 512-bit blocks of 16 32-bit words; a partial block cannot be split into words, so the generator validates total length before yielding anything.

Source

Thrown at hashes/md5.py:180

        a list of 16 32-bit words

    >>> test_string = ("".join(format(n << 24, "032b") for n in range(16))
    ...                  .encode("utf-8"))
    >>> list(get_block_words(test_string))
    [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
    >>> list(get_block_words(test_string * 4)) == [list(range(16))] * 4
    True
    >>> list(get_block_words(b"1" * 512)) == [[4294967295] * 16]
    True
    >>> list(get_block_words(b""))
    []
    >>> list(get_block_words(b"1111"))
    Traceback (most recent call last):
    ...
    ValueError: Input must have length that's a multiple of 512
    """
    if len(bit_string) % 512 != 0:
        raise ValueError("Input must have length that's a multiple of 512")

    for pos in range(0, len(bit_string), 512):
        block = bit_string[pos : pos + 512]
        block_words = []
        for i in range(0, 512, 32):
            block_words.append(int(to_little_endian(block[i : i + 32]), 2))
        yield block_words


def not_32(i: int) -> int:
    """
    Perform bitwise NOT on given int.

    Arguments:
        i {[int]} -- [given int]

    Raises:
        ValueError -- [input is negative]

View on GitHub (pinned to f5988cc097)

Solutions

  1. Always derive the bit string via preprocess(message) before calling get_block_words.
  2. If building the bit string yourself, append MD5 padding until len % 512 == 0.
  3. Sanity-check: len(bit_string) % 512 == 0 before the call in debug builds.

Example fix

# before
words = list(get_block_bits)  # raw bits, unpadded

# after
from hashes.md5 import preprocess, get_block_words
words = list(get_block_words(preprocess(message)))
Defensive patterns

Strategy: validation

Validate before calling

assert len(bit_string) % 512 == 0, (
    f"bit string length {len(bit_string)} is not a multiple of 512"
)

Type guard

def is_padded_bit_string(bits: bytes | str) -> bool:
    return len(bits) % 512 == 0

Try / catch

try:
    words = list(get_block_words(bits))
except ValueError:
    words = list(get_block_words(preprocess(message)))  # re-pad correctly

Prevention

When it happens

Trigger: Calling get_block_words(b"1111") or any bit string whose length % 512 != 0. Happens when raw message bits are passed without the MD5 padding step, or when a test vector is truncated.

Common situations: Skipping preprocess(message) (which appends the 1-bit, zeros, and 64-bit length to pad to a 512 multiple) and feeding raw bits directly; concatenating bit strings of the wrong width; bit vs byte length confusion (512 bits = 64 bytes).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/a96efaed5af8d1e4. Report an issue: GitHub.