Skip to content

Commit 46f0dc6

Browse files
committed
Enforce BCrypt password length
1 parent 36ea1b1 commit 46f0dc6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

crypto/src/main/java/org/springframework/security/crypto/bcrypt/BCrypt.java

+3
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ private static String hashpw(byte passwordb[], String salt, boolean for_check) {
611611
int rounds, off;
612612
StringBuilder rs = new StringBuilder();
613613

614+
if (passwordb.length > 72) {
615+
throw new IllegalArgumentException("password cannot be more than 72 bytes");
616+
}
614617
if (salt == null) {
615618
throw new IllegalArgumentException("salt cannot be null");
616619
}

crypto/src/test/java/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoderTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,14 @@ public void checkWhenNoRoundsThenTrue() {
222222
assertThat(encoder.matches("wrong", "$2a$00$9N8N35BVs5TLqGL3pspAte5OWWA2a2aZIs.EGp7At7txYakFERMue")).isFalse();
223223
}
224224

225+
@Test
226+
public void enforcePasswordLength() {
227+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
228+
String password72chars = "123456789012345678901234567890123456789012345678901234567890123456789012";
229+
assertThat(encoder.matches(password72chars, encoder.encode(password72chars))).isTrue();
230+
String password73chars = password72chars.concat("a");
231+
assertThatIllegalArgumentException()
232+
.isThrownBy(() -> encoder.matches(password73chars, encoder.encode(password73chars)));
233+
}
234+
225235
}

0 commit comments

Comments
 (0)