gchq/CyberChef · error · OperationError

Error: Both inputs must be of the same length.

Error message

Error: Both inputs must be of the same length.

What it means

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.

Source

Thrown at src/core/operations/HammingDistance.mjs:64

    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const delim = args[0],
            byByte = args[1] === "Byte",
            inputType = args[2],
            samples = input.split(delim);

        if (samples.length !== 2) {
            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.");
        }

        if (samples[0].length !== samples[1].length) {
            throw new OperationError("Error: Both inputs must be of the same length.");
        }

        if (inputType === "Hex") {
            samples[0] = fromHex(samples[0]);
            samples[1] = fromHex(samples[1]);
        } else {
            samples[0] = new Uint8Array(Utils.strToArrayBuffer(samples[0]));
            samples[1] = new Uint8Array(Utils.strToArrayBuffer(samples[1]));
        }

        let dist = 0;

        for (let i = 0; i < samples[0].length; i++) {
            const lhs = samples[0][i],
                rhs = samples[1][i];

            if (byByte && lhs !== rhs) {
                dist++;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Pad the shorter string to match the longer (or trim the longer) so both have equal character length.
  2. In Hex mode, ensure both hex strings have the same number of hex characters (even length).
  3. Re-check for stray whitespace/newlines at the end of either sample.
  4. If you actually want unequal-length edit distance, use Levenshtein distance instead.

Example fix

// before: lengths differ
run("hello\nworld!", ["\n", "Byte", "Raw string"]);
// after: equalise lengths
run("hello\nworld", ["\n", "Byte", "Raw string"]);
Defensive patterns

Strategy: validation

Validate before calling

function assertEqualLengthHamming(input, delim) {
  const [a, b] = splitHammingInput(input, delim);
  if (a.length !== b.length) {
    throw new Error(`Inputs differ in length: ${a.length} vs ${b.length}`);
  }
  return [a, b];
}

Type guard

function areEqualHammingLengths(input, delim) {
  const s = input.split(delim);
  return s.length === 2 && s[0].length === s[1].length;
}

Try / catch

try {
  result = hamming.run(input, args);
} catch (e) {
  if (e instanceof OperationError && /same length/i.test(e.message)) {
    // pad the shorter sample to match
    const [a, b] = input.split(delim);
    const max = Math.max(a.length, b.length);
    result = hamming.run(a.padEnd(max).concat(delim, b.padEnd(max)), args);
  } else throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/47ae257313f34f20. Report an issue: GitHub.