{"record":{"id":"26959db37a74d63e","repo":"spring-projects/spring-ai","slug":"nosuchalgorithmexception-wrapped-illegalargumente","errorCode":null,"errorMessage":"NoSuchAlgorithmException (wrapped IllegalArgumentException)","messagePattern":"NoSuchAlgorithmException \\(wrapped IllegalArgumentException\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-commons/src/main/java/org/springframework/ai/document/id/JdkSha256HexIdGenerator.java","lineNumber":51,"sourceCode":" * @author Christian Tzolov\n */\npublic class JdkSha256HexIdGenerator implements IdGenerator {\n\n\tprivate static final String SHA_256 = \"SHA-256\";\n\n\tprivate final String byteHexFormat = \"%02x\";\n\n\tprivate final Charset charset;\n\n\tprivate final MessageDigest messageDigest;\n\n\tpublic JdkSha256HexIdGenerator(final String algorithm, final Charset charset) {\n\t\tthis.charset = charset;\n\t\ttry {\n\t\t\tthis.messageDigest = MessageDigest.getInstance(algorithm);\n\t\t}\n\t\tcatch (NoSuchAlgorithmException e) {\n\t\t\tthrow new IllegalArgumentException(e);\n\t\t}\n\t}\n\n\tpublic JdkSha256HexIdGenerator() {\n\t\tthis(SHA_256, StandardCharsets.UTF_8);\n\t}\n\n\t@Override\n\tpublic String generateId(Object... contents) {\n\t\treturn this.hash(this.serializeToBytes(contents));\n\t}\n\n\t// https://github.com/spring-projects/spring-ai/issues/113#issue-2000373318\n\tprivate String hash(byte[] contentWithMetadata) {\n\t\tbyte[] hashBytes = getMessageDigest().digest(contentWithMetadata);\n\t\tStringBuilder sb = new StringBuilder();\n\t\tfor (byte b : hashBytes) {\n\t\t\tsb.append(String.format(this.byteHexFormat, b));","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-commons/src/main/java/org/springframework/ai/document/id/JdkSha256HexIdGenerator.java#L33-L69","documentation":"JdkSha256HexIdGenerator resolves a MessageDigest via MessageDigest.getInstance(algorithm) in its constructor; unsupported algorithm names throw NoSuchAlgorithmException, wrapped in IllegalArgumentException. The default constructor uses SHA-256, so this only occurs when supplying a custom algorithm.","triggerScenarios":"new JdkSha256HexIdGenerator(\"SHA3-999\", charset) or any algorithm string not provided by the installed JCE providers (typo like 'sha256' is usually fine — JDK is case-insensitive — but e.g. 'MD6' or a FIPS-restricted JDK lacking the algorithm fails).","commonSituations":"Configuring a custom hash algorithm via properties on a hardened/FIPS JVM where the requested provider isn't installed, or typos in algorithm names.","solutions":["Use the no-arg constructor which defaults to SHA-256","Use a standard JDK algorithm name: 'SHA-256', 'SHA-512', 'MD5'","Check available algorithms: Security.getAlgorithms(\"MessageDigest\")","Catch IllegalArgumentException at construction and fall back to SHA-256"],"exampleFix":"// before\nIdGenerator gen = new JdkSha256HexIdGenerator(config.getHashAlgo(), UTF_8);\n// after\nString algo = Security.getAlgorithms(\"MessageDigest\").contains(config.getHashAlgo()) ? config.getHashAlgo() : \"SHA-256\";\nIdGenerator gen = new JdkSha256HexIdGenerator(algo, UTF_8);","handlingStrategy":"validation","validationCode":"if (!Security.getAlgorithms(\"MessageDigest\").contains(algorithm)) {\n    throw new IllegalArgumentException(\"Unsupported digest algorithm: \" + algorithm);\n}\nnew JdkSha256HexIdGenerator(algorithm, charset);","typeGuard":null,"tryCatchPattern":"try {\n    gen = new JdkSha256HexIdGenerator(algo, charset);\n} catch (IllegalArgumentException e) {\n    gen = new JdkSha256HexIdGenerator(); // fallback to SHA-256\n}","preventionTips":["Default to the no-arg SHA-256 constructor","Validate algorithm names against Security.getAlgorithms(\"MessageDigest\")","Be aware of FIPS-hardened JVMs with restricted providers"],"tags":["hashing","algorithm","configuration"],"backgroundTag":"invalid-argument-value","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}