{"record":{"id":"587e1b23418cab16","repo":"spring-projects/spring-security","slug":"cpu-cost-parameter-must-be-1","errorCode":null,"errorMessage":"Cpu cost parameter must be > 1.","messagePattern":"Cpu cost parameter must be > 1\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"crypto/src/main/java/org/springframework/security/crypto/scrypt/SCryptPasswordEncoder.java","lineNumber":99,"sourceCode":"\tprivate final BytesKeyGenerator saltGenerator;\n\n\t/**\n\t * Constructs a SCrypt password encoder with the provided parameters.\n\t * @param cpuCost cpu cost of the algorithm (as defined in scrypt this is N). must be\n\t * power of 2 greater than 1. Default is currently 65,536 or 2^16)\n\t * @param memoryCost memory cost of the algorithm (as defined in scrypt this is r)\n\t * Default is currently 8.\n\t * @param parallelization the parallelization of the algorithm (as defined in scrypt\n\t * this is p) Default is currently 1. Note that the implementation does not currently\n\t * take advantage of parallelization.\n\t * @param keyLength key length for the algorithm (as defined in scrypt this is dkLen).\n\t * The default is currently 32.\n\t * @param saltLength salt length (as defined in scrypt this is the length of S). The\n\t * default is currently 16.\n\t */\n\tpublic SCryptPasswordEncoder(int cpuCost, int memoryCost, int parallelization, int keyLength, int saltLength) {\n\t\tif (cpuCost <= 1) {\n\t\t\tthrow new IllegalArgumentException(\"Cpu cost parameter must be > 1.\");\n\t\t}\n\t\tif (memoryCost == 1 && cpuCost > 65536) {\n\t\t\tthrow new IllegalArgumentException(\"Cpu cost parameter must be > 1 and < 65536.\");\n\t\t}\n\t\tif (memoryCost < 1) {\n\t\t\tthrow new IllegalArgumentException(\"Memory cost must be >= 1.\");\n\t\t}\n\t\tint maxParallel = Integer.MAX_VALUE / (128 * memoryCost * 8);\n\t\tif (parallelization < 1 || parallelization > maxParallel) {\n\t\t\tthrow new IllegalArgumentException(\"Parallelisation parameter p must be >= 1 and <= \" + maxParallel\n\t\t\t\t\t+ \" (based on block size r of \" + memoryCost + \")\");\n\t\t}\n\t\tif (keyLength < 1 || keyLength > Integer.MAX_VALUE) {\n\t\t\tthrow new IllegalArgumentException(\"Key length must be >= 1 and <= \" + Integer.MAX_VALUE);\n\t\t}\n\t\tif (saltLength < 1 || saltLength > Integer.MAX_VALUE) {\n\t\t\tthrow new IllegalArgumentException(\"Salt length must be >= 1 and <= \" + Integer.MAX_VALUE);\n\t\t}","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/crypto/src/main/java/org/springframework/security/crypto/scrypt/SCryptPasswordEncoder.java#L81-L117","documentation":"The SCryptPasswordEncoder(int...) constructor enforces scrypt's mathematical constraint that the CPU cost r must exceed 1 (the scrypt formula requires r > 1, and memoryCost == 1 additionally caps cpuCost at 65536). Passing cpuCost <= 1 would produce an invalid or degenerate scrypt configuration, so the constructor fails immediately with this IllegalArgumentException.","triggerScenarios":"Instantiating new SCryptPasswordEncoder(0|1, memoryCost, parallelization, keyLength, saltLength) or passing a cpuCost resolved from configuration that is 0, 1, or negative.","commonSituations":"Unset config property defaulting to 0; misunderstanding the parameter and passing parallelization/keyLength values in the wrong slot; attempting to 'disable' cost by setting it to 1.","solutions":["Use values at or above the defaults: new SCryptPasswordEncoder(16384, 8, 1, 32, 64).","Validate configuration before constructing: if (cpuCost > 1) { ... } else fall back to defaults.","Check argument order in the five-arg constructor — cpuCost is the first parameter, not keyLength or parallelization."],"exampleFix":"// before\nPasswordEncoder e = new SCryptPasswordEncoder(1, 8, 1, 32, 64); // invalid\n// after\nPasswordEncoder e = new SCryptPasswordEncoder(16384, 8, 1, 32, 64);","handlingStrategy":"validation","validationCode":"if (cpuCost <= 1) cpuCost = 16384;\nPasswordEncoder e = new SCryptPasswordEncoder(cpuCost, memoryCost, parallelization, keyLength, saltLength);","typeGuard":null,"tryCatchPattern":"try {\n    encoder = new SCryptPasswordEncoder(cpuCost, memoryCost, parallelization, keyLength, saltLength);\n} catch (IllegalArgumentException e) {\n    encoder = new SCryptPasswordEncoder(); // secure defaults\n}","preventionTips":["Keep scrypt parameters in a validated config object with bounds checks (cpuCost > 1, memoryCost >= 1).","Use the no-arg SCryptPasswordEncoder() unless you have a tested reason to tune parameters.","Double-check argument order in the five-arg constructor; cpuCost comes first."],"tags":["java","spring-security","scrypt","configuration"],"backgroundTag":"argument-out-of-range","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}