{"record":{"id":"8c6c3caa79789ece","repo":"TheAlgorithms/Python","slug":"input-must-be-non-negative","errorCode":null,"errorMessage":"Input must be non-negative","messagePattern":"Input must be non-negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hashes/md5.py","lineNumber":81,"sourceCode":"        8-char little-endian hex string\n\n    >>> reformat_hex(1234)\n    b'd2040000'\n    >>> reformat_hex(666)\n    b'9a020000'\n    >>> reformat_hex(0)\n    b'00000000'\n    >>> reformat_hex(1234567890)\n    b'd2029649'\n    >>> reformat_hex(1234567890987654321)\n    b'b11c6cb1'\n    >>> reformat_hex(-1)\n    Traceback (most recent call last):\n    ...\n    ValueError: Input must be non-negative\n    \"\"\"\n    if i < 0:\n        raise ValueError(\"Input must be non-negative\")\n\n    hex_rep = format(i, \"08x\")[-8:]\n    little_endian_hex = b\"\"\n    for j in [3, 2, 1, 0]:\n        little_endian_hex += hex_rep[2 * j : 2 * j + 2].encode(\"utf-8\")\n    return little_endian_hex\n\n\ndef preprocess(message: bytes) -> bytes:\n    \"\"\"\n    Preprocesses the message string:\n    - Convert message to bit string\n    - Pad bit string to a multiple of 512 chars:\n        - Append a 1\n        - Append 0's until length = 448 (mod 512)\n        - Append length of original message (64 chars)\n\n    Example: Suppose the input is the following:","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/hashes/md5.py#L63-L99","documentation":"Thrown by reformat_hex() in the MD5 implementation when asked to format a negative integer. The function formats i as 8 hex digits (mod 2**32) and byte-swaps them for little-endian output; negative values have no such representation, so they are rejected with ValueError.","triggerScenarios":"Calling reformat_hex(-1), or passing an accumulator/word that went negative because of a subtraction (e.g. computing a - b on 32-bit words without wrapping).","commonSituations":"Reimplementing MD5 round math where subtractions produce negatives; feeding signed parse results (e.g. from a struct with signed interpretation) into a routine that expects unsigned 32-bit words.","solutions":["Mask values to unsigned 32-bit before formatting: reformat_hex(value & 0xFFFFFFFF).","Replace a - b with (a - b) % 2**32 in any 32-bit arithmetic feeding this function.","Parse binary words with signed=False / '<I' format so negatives never appear."],"exampleFix":"# before\nreformat_hex(a - b)  # can be negative\n\n# after\nreformat_hex((a - b) % 2**32)","handlingStrategy":"validation","validationCode":"value &= 0xFFFFFFFF  # force unsigned 32-bit before calling","typeGuard":"def is_u32(i: int) -> bool:\n    return isinstance(i, int) and 0 <= i < 2**32","tryCatchPattern":"try:\n    hx = reformat_hex(i)\nexcept ValueError:\n    hx = reformat_hex(i % 2**32)  # wrap into 32-bit range","preventionTips":["Wrap every 32-bit add/sub with % 2**32 in custom crypto math.","Parse binary words as unsigned (struct '<I').","Mask at the boundary: value & 0xFFFFFFFF."],"tags":["md5","hash","validation","unsigned","internal-api"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}