spring-projects/spring-security · error · IllegalArgumentException
password cannot be more than 72 bytes
Error message
password cannot be more than 72 bytes
What it means
BCrypt.hashpw() enforces a maximum password length of 72 bytes for new passwords (when for_check is false). Bcrypt only uses the first 72 bytes of input, so longer passwords would be silently truncated; this library throws instead to avoid a false sense of security. Checking existing hashes is exempt.
Source
Thrown at crypto/src/main/java/org/springframework/security/crypto/bcrypt/BCrypt.java:616
* @param passwordb the password to hash, as a byte array
* @param salt the salt to hash with (perhaps generated using BCrypt.gensalt)
* @return the hashed password
*/
public static String hashpw(byte passwordb[], String salt) {
return hashpw(passwordb, salt, false);
}
private static String hashpw(byte passwordb[], String salt, boolean for_check) {
BCrypt B;
String real_salt;
byte saltb[], hashed[];
char minor = (char) 0;
int rounds, off;
StringBuilder rs = new StringBuilder();
// Enforce max length for new passwords only
if (!for_check && passwordb.length > 72) {
throw new IllegalArgumentException("password cannot be more than 72 bytes");
}
if (salt == null) {
throw new IllegalArgumentException("salt cannot be null");
}
int saltLength = salt.length();
if (saltLength < 28) {
throw new IllegalArgumentException("Invalid salt");
}
if (salt.charAt(0) != '$' || salt.charAt(1) != '2') {
throw new IllegalArgumentException("Invalid salt version");
}
if (salt.charAt(2) == '$') {
off = 3;
}
else {View on GitHub (pinned to 96852e8860)
Solutions
- Limit passwords to 72 bytes at input validation
- If long secrets are required, pre-hash: SHA-256 the input then Base64 and bcrypt that (with the caveats of null bytes)
- Encode consistently (UTF-8) so byte length is predictable and document the limit
Example fix
// before
String hash = BCrypt.hashpw(password, salt);
// after
if (password.getBytes(StandardCharsets.UTF_8).length > 72) {
throw new IllegalArgumentException("Password exceeds 72 bytes");
}
String hash = BCrypt.hashpw(password, salt); Defensive patterns
Strategy: validation
Validate before calling
if (password.getBytes(StandardCharsets.UTF_8).length > 72) {
throw new IllegalArgumentException("Password must be at most 72 bytes");
} Type guard
boolean bcryptSafe(String pw) {
return pw != null && pw.getBytes(StandardCharsets.UTF_8).length <= 72;
} Try / catch
try {
hash = BCrypt.hashpw(pw, salt);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("72 bytes")) { /* reject or pre-hash the password */ }
} Prevention
- Enforce a max password length (e.g. 64 chars) at registration/validation time
- Count bytes (UTF-8), not chars — multibyte characters inflate length
- Document the 72-byte bcrypt limit in password policy
When it happens
Trigger: BCrypt.hashpw(password, salt) with passwordb.length > 72 bytes (note bytes, not chars — multi-byte UTF-8 characters count more).
Common situations: Users pasting long passphrases or PEM keys as passwords; UTF-8 multibyte characters pushing a 60-char password over 72 bytes; not pre-hashing long inputs.
Understand the failure class
Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.
Related errors
AI-assisted analysis of spring-projects/spring-security@96852e8860 (2026-09-10).
Data as JSON: /api/errors/9abae9b7679d7d9f.
Report an issue: GitHub.