{"record":{"id":"89d4871f08e4b3ac","repo":"spring-projects/spring-security","slug":"cpu-cost-parameter-must-be-1-and-65536","errorCode":null,"errorMessage":"Cpu cost parameter must be > 1 and < 65536.","messagePattern":"Cpu cost parameter must be > 1 and < 65536\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"crypto/src/main/java/org/springframework/security/crypto/scrypt/SCryptPasswordEncoder.java","lineNumber":102,"sourceCode":"\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}\n\t\tthis.cpuCost = cpuCost;\n\t\tthis.memoryCost = memoryCost;\n\t\tthis.parallelization = parallelization;","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/crypto/src/main/java/org/springframework/security/crypto/scrypt/SCryptPasswordEncoder.java#L84-L120","documentation":"SCryptPasswordEncoder's 5-arg constructor validates the scrypt work-factor parameters before hashing. The cpuCost (N) must be > 1, and when memoryCost (r) == 1 it is additionally capped at 65536 because the scrypt block-size/runtime trade-off becomes invalid. The library throws IllegalArgumentException immediately rather than producing broken hashes.","triggerScenarios":"Calling `new SCryptPasswordEncoder(cpuCost, memoryCost, parallelization, keyLength, saltLength)` with cpuCost <= 1, or with memoryCost == 1 and cpuCost > 65536.","commonSituations":"Copy-pasting scrypt parameters from tuning articles where N is written as 0 or 1; computing N dynamically (e.g. 2^x) with a negative exponent; misreading the default constructor (N=16384) and passing the exponent itself as cpuCost.","solutions":["Pass cpuCost as the raw N value (e.g. 16384 for 2^14), not the exponent.","Ensure cpuCost >= 2; use the library default by calling the no-arg SCryptPasswordEncoder().","If cpuCost must exceed 65536, raise memoryCost above 1 so the cap does not apply, or reduce N."],"exampleFix":"// before\nnew SCryptPasswordEncoder(1, 1, 1, 32, 64);\n// after\nnew SCryptPasswordEncoder(16384, 8, 1, 32, 64);","handlingStrategy":"validation","validationCode":"if (cpuCost <= 1 || (memoryCost == 1 && cpuCost > 65536)) {\n    throw new IllegalArgumentException(\"cpuCost must be > 1 (and <= 65536 when memoryCost == 1): \" + cpuCost);\n}\nnew SCryptPasswordEncoder(cpuCost, memoryCost, parallelization, keyLength, saltLength);","typeGuard":null,"tryCatchPattern":"try {\n    encoder = new SCryptPasswordEncoder(cpuCost, memoryCost, p, keyLen, saltLen);\n} catch (IllegalArgumentException e) {\n    encoder = new SCryptPasswordEncoder(); // fall back to defaults\n}","preventionTips":["Pass N (e.g. 16384), not its exponent.","Prefer the no-arg SCryptPasswordEncoder().","Validate config-sourced parameters before constructing."],"tags":["java","spring-security","password-hashing","validation","constructor"],"backgroundTag":"invalid-constructor-argument","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"}