{"record":{"id":"a12da8a35e25b28c","repo":"TheAlgorithms/Python","slug":"input-must-be-of-length-32","errorCode":null,"errorMessage":"Input must be of length 32","messagePattern":"Input must be of length 32","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hashes/md5.py","lineNumber":38,"sourceCode":"    Converts the given string to little-endian in groups of 8 chars.\n\n    Arguments:\n        string_32 {[string]} -- [32-char string]\n\n    Raises:\n        ValueError -- [input is not 32 char]\n\n    Returns:\n        32-char little-endian string\n    >>> to_little_endian(b'1234567890abcdfghijklmnopqrstuvw')\n    b'pqrstuvwhijklmno90abcdfg12345678'\n    >>> to_little_endian(b'1234567890')\n    Traceback (most recent call last):\n    ...\n    ValueError: Input must be of length 32\n    \"\"\"\n    if len(string_32) != 32:\n        raise ValueError(\"Input must be of length 32\")\n\n    little_endian = b\"\"\n    for i in [3, 2, 1, 0]:\n        little_endian += string_32[8 * i : 8 * i + 8]\n    return little_endian\n\n\ndef reformat_hex(i: int) -> bytes:\n    \"\"\"\n    Converts the given non-negative integer to hex string.\n\n    Example: Suppose the input is the following:\n        i = 1234\n\n        The input is 0x000004d2 in hex, so the little-endian hex string is\n        \"d2040000\".\n\n    Arguments:","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/hashes/md5.py#L20-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nle = to_little_endian(raw_bytes)  # raw_bytes has arbitrary length\n\n# after\nfor pos in range(0, len(raw_bytes), 32):\n    le = to_little_endian(raw_bytes[pos:pos + 32])","handlingStrategy":"validation","validationCode":"assert len(chunk) == 32, f\"to_little_endian needs 32 bytes, got {len(chunk)}\"","typeGuard":"def is_32_bytes(buf: bytes) -> bool:\n    return isinstance(buf, (bytes, bytearray)) and len(buf) == 32","tryCatchPattern":"try:\n    le = to_little_endian(chunk)\nexcept ValueError:\n    raise ValueError(f\"expected 32-byte chunk, got {len(chunk)} bytes\") from None","preventionTips":["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."],"tags":["md5","hash","validation","endianess","internal-api"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}