{"record":{"id":"a96efaed5af8d1e4","repo":"TheAlgorithms/Python","slug":"input-must-have-length-that-s-a-multiple-of-512","errorCode":null,"errorMessage":"Input must have length that's a multiple of 512","messagePattern":"Input must have length that's a multiple of 512","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hashes/md5.py","lineNumber":180,"sourceCode":"        a list of 16 32-bit words\n\n    >>> test_string = (\"\".join(format(n << 24, \"032b\") for n in range(16))\n    ...                  .encode(\"utf-8\"))\n    >>> list(get_block_words(test_string))\n    [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]\n    >>> list(get_block_words(test_string * 4)) == [list(range(16))] * 4\n    True\n    >>> list(get_block_words(b\"1\" * 512)) == [[4294967295] * 16]\n    True\n    >>> list(get_block_words(b\"\"))\n    []\n    >>> list(get_block_words(b\"1111\"))\n    Traceback (most recent call last):\n    ...\n    ValueError: Input must have length that's a multiple of 512\n    \"\"\"\n    if len(bit_string) % 512 != 0:\n        raise ValueError(\"Input must have length that's a multiple of 512\")\n\n    for pos in range(0, len(bit_string), 512):\n        block = bit_string[pos : pos + 512]\n        block_words = []\n        for i in range(0, 512, 32):\n            block_words.append(int(to_little_endian(block[i : i + 32]), 2))\n        yield block_words\n\n\ndef not_32(i: int) -> int:\n    \"\"\"\n    Perform bitwise NOT on given int.\n\n    Arguments:\n        i {[int]} -- [given int]\n\n    Raises:\n        ValueError -- [input is negative]","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/hashes/md5.py#L162-L198","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Always derive the bit string via preprocess(message) before calling get_block_words.","If building the bit string yourself, append MD5 padding until len % 512 == 0.","Sanity-check: len(bit_string) % 512 == 0 before the call in debug builds."],"exampleFix":"# before\nwords = list(get_block_bits)  # raw bits, unpadded\n\n# after\nfrom hashes.md5 import preprocess, get_block_words\nwords = list(get_block_words(preprocess(message)))","handlingStrategy":"validation","validationCode":"assert len(bit_string) % 512 == 0, (\n    f\"bit string length {len(bit_string)} is not a multiple of 512\"\n)","typeGuard":"def is_padded_bit_string(bits: bytes | str) -> bool:\n    return len(bits) % 512 == 0","tryCatchPattern":"try:\n    words = list(get_block_words(bits))\nexcept ValueError:\n    words = list(get_block_words(preprocess(message)))  # re-pad correctly","preventionTips":["Always call preprocess(message) before get_block_words.","Remember 512 bits = 64 bytes; check both units when debugging.","Never hand-truncate test vectors."],"tags":["md5","hash","validation","padding","internal-api"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}