{"record":{"id":"47ae257313f34f20","repo":"gchq/CyberChef","slug":"error-both-inputs-must-be-of-the-same-length","errorCode":null,"errorMessage":"Error: Both inputs must be of the same length.","messagePattern":"Error: Both inputs must be of the same length\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/HammingDistance.mjs","lineNumber":64,"sourceCode":"    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const delim = args[0],\n            byByte = args[1] === \"Byte\",\n            inputType = args[2],\n            samples = input.split(delim);\n\n        if (samples.length !== 2) {\n            throw new OperationError(\"Error: You can only calculate the edit distance between 2 strings. Please ensure exactly two inputs are provided, separated by the specified delimiter.\");\n        }\n\n        if (samples[0].length !== samples[1].length) {\n            throw new OperationError(\"Error: Both inputs must be of the same length.\");\n        }\n\n        if (inputType === \"Hex\") {\n            samples[0] = fromHex(samples[0]);\n            samples[1] = fromHex(samples[1]);\n        } else {\n            samples[0] = new Uint8Array(Utils.strToArrayBuffer(samples[0]));\n            samples[1] = new Uint8Array(Utils.strToArrayBuffer(samples[1]));\n        }\n\n        let dist = 0;\n\n        for (let i = 0; i < samples[0].length; i++) {\n            const lhs = samples[0][i],\n                rhs = samples[1][i];\n\n            if (byByte && lhs !== rhs) {\n                dist++;","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/HammingDistance.mjs#L46-L82","documentation":"Thrown by Hamming Distance when the two samples exist but differ in length. Hamming distance is only defined for equal-length sequences, so the operation refuses to proceed. Note the length check runs on the raw strings BEFORE hex/raw decoding, so it compares character counts, not decoded byte counts.","triggerScenarios":"Two input strings of different character lengths after splitting; one string has trailing whitespace/padding; Hex mode where one hex string has an odd or missing byte (lengths differ before decode).","commonSituations":"Padded one string but not the other; copy-paste truncation; hex inputs where one was uppercased/normalized losing a leading zero; UTF-8 vs ASCII byte-length confusion in Raw mode (the check uses JS string length, so multibyte chars count as one).","solutions":["Pad the shorter string to match the longer (or trim the longer) so both have equal character length.","In Hex mode, ensure both hex strings have the same number of hex characters (even length).","Re-check for stray whitespace/newlines at the end of either sample.","If you actually want unequal-length edit distance, use Levenshtein distance instead."],"exampleFix":"// before: lengths differ\nrun(\"hello\\nworld!\", [\"\\n\", \"Byte\", \"Raw string\"]);\n// after: equalise lengths\nrun(\"hello\\nworld\", [\"\\n\", \"Byte\", \"Raw string\"]);","handlingStrategy":"validation","validationCode":"function assertEqualLengthHamming(input, delim) {\n  const [a, b] = splitHammingInput(input, delim);\n  if (a.length !== b.length) {\n    throw new Error(`Inputs differ in length: ${a.length} vs ${b.length}`);\n  }\n  return [a, b];\n}","typeGuard":"function areEqualHammingLengths(input, delim) {\n  const s = input.split(delim);\n  return s.length === 2 && s[0].length === s[1].length;\n}","tryCatchPattern":"try {\n  result = hamming.run(input, args);\n} catch (e) {\n  if (e instanceof OperationError && /same length/i.test(e.message)) {\n    // pad the shorter sample to match\n    const [a, b] = input.split(delim);\n    const max = Math.max(a.length, b.length);\n    result = hamming.run(a.padEnd(max).concat(delim, b.padEnd(max)), args);\n  } else throw e;\n}","preventionTips":["Pad or trim so both samples have equal character length before invoking.","In Hex mode keep both hex strings at the same (even) length.","Use Levenshtein distance for genuinely unequal inputs."],"tags":["hamming-distance","length-mismatch","argument-validation","string-metric"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}