{"record":{"id":"361815efd2432bc7","repo":"gchq/CyberChef","slug":"invalid-hash","errorCode":null,"errorMessage":"Invalid hash","messagePattern":"Invalid hash","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/AnalyseHash.mjs","lineNumber":44,"sourceCode":"        this.outputType = \"string\";\n        this.args = [];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        input = input.replace(/\\s/g, \"\");\n\n        let output = \"\",\n            possibleHashFunctions = [];\n        const byteLength = input.length / 2,\n            bitLength = byteLength * 8;\n\n        if (!/^[a-f0-9]+$/i.test(input)) {\n            throw new OperationError(\"Invalid hash\");\n        }\n\n        output += \"Hash length: \" + input.length + \"\\n\" +\n            \"Byte length: \" + byteLength + \"\\n\" +\n            \"Bit length:  \" + bitLength + \"\\n\\n\" +\n            \"Based on the length, this hash could have been generated by one of the following hashing functions:\\n\";\n\n        switch (bitLength) {\n            case 4:\n                possibleHashFunctions = [\n                    \"Fletcher-4\",\n                    \"Luhn algorithm\",\n                    \"Verhoeff algorithm\",\n                ];\n                break;\n            case 8:\n                possibleHashFunctions = [\n                    \"Fletcher-8\",","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/AnalyseHash.mjs#L26-L62","documentation":"Thrown by AnalyseHash.run when the input, after stripping all whitespace, does not match /^[a-f0-9]+$/i. The operation analyses a hash by its byte/bit length to suggest candidate algorithms, so it requires the input to be a contiguous hex string. Any non-hex character (a stray separator, a 0x prefix, a base64 hash, or trailing newline outside whitespace) causes rejection.","triggerScenarios":"Input containing characters outside [0-9a-fA-F] after whitespace removal: a '0x' prefix, colons/dashes between byte groups, a base64-encoded hash, mixed-case with non-hex letters like 'g-z', or an empty string.","commonSituations":"Pasting a hash that includes separators (aa:bb:cc...), feeding a base64 or raw binary hash instead of hex, copying a hash with a leading label like 'sha256: ...', or an upstream operation emitting a non-hex digest.","solutions":["Provide the hash as a pure hexadecimal string (only 0-9 and a-f/A-F).","Remove any '0x' prefix, colons, dashes, or algorithm-name labels before the operation.","If the hash is base64 or raw bytes, first convert it to hex with a 'From Base64' or 'To Hex' operation.","Strip whitespace upstream; AnalyseHash already removes whitespace but will not remove other separators."],"exampleFix":"// before\nchef.analyseHash(\"sha256: e3b0c44298fc1c14...\");\n\n// after\nchef.analyseHash(\"e3b0c44298fc1c14...\");","handlingStrategy":"validation","validationCode":"function assertHexString(hash) {\n  const clean = String(hash).replace(/\\s/g, \"\");\n  if (!/^[a-f0-9]+$/i.test(clean) || clean.length === 0) {\n    throw new Error(\"Input must be a non-empty hex string\");\n  }\n  return clean;\n}\nconst hex = assertHexString(input);","typeGuard":"function isHexString(s) {\n  const clean = String(s).replace(/\\s/g, \"\");\n  return clean.length > 0 && /^[a-f0-9]+$/i.test(clean);\n}","tryCatchPattern":null,"preventionTips":["Convert base64/raw hashes to hex before AnalyseHash.","Strip algorithm-name labels and '0x' prefixes upstream.","Avoid byte separators (colons/dashes) in the input."],"tags":["hash","input-validation","format"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}