gchq/CyberChef · error · OperationError
Error: You can only calculate the edit distance between 2 st
Error message
Error: You can only calculate the edit distance between 2 strings. Please ensure exactly two inputs are provided, separated by the specified delimiter.
What it means
Thrown by Hamming Distance when the input split by the delimiter does not yield exactly two samples. Hamming distance is defined pairwise, so any count other than two is rejected before length comparison.
Source
Thrown at src/core/operations/HammingDistance.mjs:60
"type": "option",
"value": ["Raw string", "Hex"]
}
];
}
/**
* @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],View on GitHub (pinned to 4290ea7539)
Solutions
- Set the Delimiter argument to exactly the separator between your two strings.
- Ensure the input contains precisely two strings separated once by that delimiter.
- Strip any trailing delimiters or whitespace that would create extra empty segments.
- If comparing many pairs, run the operation per-pair rather than batch.
Example fix
// before: input uses a single newline but delimiter defaults to "\n\n"
const input = "foo\nbar";
run(input, ["\n\n", "Byte", "Raw string"]);
// after: match the delimiter to the data
run("foo\nbar", ["\n", "Byte", "Raw string"]); Defensive patterns
Strategy: validation
Validate before calling
function splitHammingInput(input, delim) {
const samples = input.split(delim);
if (samples.length !== 2) {
throw new Error(`Expected exactly 2 samples, got ${samples.length}. Delimiter was ${JSON.stringify(delim)}`);
}
return samples;
} Type guard
function hasExactlyTwoSamples(input, delim) {
return input.split(delim).length === 2;
} Try / catch
try {
result = hamming.run(input, [delim, unit, type]);
} catch (e) {
if (e instanceof OperationError && /exactly two inputs/i.test(e.message)) {
// delimiter mismatch - try common separators
for (const d of ['\n', '\n\n', ',']) {
if (input.split(d).length === 2) { delim = d; break; }
}
result = hamming.run(input, [delim, unit, type]);
} else throw e;
} Prevention
- Set the Delimiter argument to exactly the separator in your input.
- Strip trailing delimiters that would create empty third segments.
- Run the operation once per pair rather than batching many.
When it happens
Trigger: Input contains 0, 1, 3+ delimiter occurrences; delimiter argument does not match the actual separator in the input (so nothing splits, or splits too many times); default delimiter is a literal '\n\n' but input uses a single newline.
Common situations: Delimiter mismatch (the arg is a binaryShortString like '\n\n' but data uses ',' or '\n'); pasting only one string; trailing delimiter producing an empty third segment; using Bit/Hex mode on text that lacks the expected separator.
Related errors
- Error: Both inputs must be of the same length.
- Invalid block cipher mode: ${mode}
- Invalid ciphertext length: ${originalLength} bytes. Must be
- No padding requested but input is not a ${blockSize}-byte mu
- Unknown padding type: ${padding}
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/e941f1d130ab1785.
Report an issue: GitHub.