@@ -223,13 +223,34 @@ public void checkWhenNoRoundsThenTrue() {
223223 }
224224
225225 @ Test
226- public void enforcePasswordLength () {
226+ public void encodeWhenPasswordOverMaxLengthThenThrowIllegalArgumentException () {
227227 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
228+
228229 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 )));
230+ encoder .encode (password72chars );
231+
232+ String password73chars = password72chars + "3" ;
233+ assertThatIllegalArgumentException ().isThrownBy (() -> encoder .encode (password73chars ));
234+ }
235+
236+ @ Test
237+ public void matchesWhenPasswordOverMaxLengthThenAllowToMatch () {
238+ BCryptPasswordEncoder encoder = new BCryptPasswordEncoder ();
239+
240+ String password71chars = "12345678901234567890123456789012345678901234567890123456789012345678901" ;
241+ String encodedPassword71chars = "$2a$10$jx3x2FaF.iX5QZ9i3O424Os2Ou5P5JrnedmWYHuDyX8JKA4Unp4xq" ;
242+ assertThat (encoder .matches (password71chars , encodedPassword71chars )).isTrue ();
243+
244+ String password72chars = password71chars + "2" ;
245+ String encodedPassword72chars = "$2a$10$oXYO6/UvbsH5rQEraBkl6uheccBqdB3n.RaWbrimog9hS2GX4lo/O" ;
246+ assertThat (encoder .matches (password72chars , encodedPassword72chars )).isTrue ();
247+
248+ // Max length is 72 bytes, however, we need to ensure backwards compatibility
249+ // for previously encoded passwords that are greater than 72 bytes and allow the
250+ // match to be performed.
251+ String password73chars = password72chars + "3" ;
252+ String encodedPassword73chars = "$2a$10$1l9.kvQTsqNLiCYFqmKtQOHkp.BrgIrwsnTzWo9jdbQRbuBYQ/AVK" ;
253+ assertThat (encoder .matches (password73chars , encodedPassword73chars )).isTrue ();
233254 }
234255
235256}
0 commit comments