@Test public void testMatchesAutoDetection_BCRYPT() throws Exception { String rawPassword = "p@ssW0rd"; String encodedPassword = "$2a$10$cuR2ghEiftvcxKx/V8ZNieZBs4Xfiq6ZsRapxHZRnfWpMyBwQYUme"; boolean actual = sut.matches(rawPassword, encodedPassword); assertTrue(actual); }
@Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Matcher matcher = VERSION_ENCODED_PATTERN.matcher(encodedPassword); if (matcher.matches()) { String key = matcher.group(1); String body = matcher.group(2); PasswordEncoder encoder; if (encoders.containsKey(key)) { encoder = encoders.get(key); } else { log.warn("Unknown encoding type: {} ... using unknown encoder {}", key, unknownEncoder); encoder = encoders.get(unknownEncoder); } return encoder.matches(rawPassword, body); } else if (enableAutoDetection) { if (BCRYPT_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be BCrypt encoded password..."); if (BCRYPT.matches(rawPassword, encodedPassword)) { return true; } } if (STANDARD_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be Standard encoded password..."); if (STANDARD.matches(rawPassword, encodedPassword)) { return true; } } } return encoders.get(unknownEncoder).matches(rawPassword, encodedPassword); } catch (Exception e) { log.warn("Unexpected", e); } return false; }
VersionedPasswordEncoder implements PasswordEncoder { @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Matcher matcher = VERSION_ENCODED_PATTERN.matcher(encodedPassword); if (matcher.matches()) { String key = matcher.group(1); String body = matcher.group(2); PasswordEncoder encoder; if (encoders.containsKey(key)) { encoder = encoders.get(key); } else { log.warn("Unknown encoding type: {} ... using unknown encoder {}", key, unknownEncoder); encoder = encoders.get(unknownEncoder); } return encoder.matches(rawPassword, body); } else if (enableAutoDetection) { if (BCRYPT_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be BCrypt encoded password..."); if (BCRYPT.matches(rawPassword, encodedPassword)) { return true; } } if (STANDARD_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be Standard encoded password..."); if (STANDARD.matches(rawPassword, encodedPassword)) { return true; } } } return encoders.get(unknownEncoder).matches(rawPassword, encodedPassword); } catch (Exception e) { log.warn("Unexpected", e); } return false; } }
VersionedPasswordEncoder implements PasswordEncoder { @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Matcher matcher = VERSION_ENCODED_PATTERN.matcher(encodedPassword); if (matcher.matches()) { String key = matcher.group(1); String body = matcher.group(2); PasswordEncoder encoder; if (encoders.containsKey(key)) { encoder = encoders.get(key); } else { log.warn("Unknown encoding type: {} ... using unknown encoder {}", key, unknownEncoder); encoder = encoders.get(unknownEncoder); } return encoder.matches(rawPassword, body); } else if (enableAutoDetection) { if (BCRYPT_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be BCrypt encoded password..."); if (BCRYPT.matches(rawPassword, encodedPassword)) { return true; } } if (STANDARD_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be Standard encoded password..."); if (STANDARD.matches(rawPassword, encodedPassword)) { return true; } } } return encoders.get(unknownEncoder).matches(rawPassword, encodedPassword); } catch (Exception e) { log.warn("Unexpected", e); } return false; } VersionedPasswordEncoder();  VersionedPasswordEncoder(String unknownEncoder, String primaryEncoder,
			Map<String, PasswordEncoder> encoders); }
VersionedPasswordEncoder implements PasswordEncoder { @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Matcher matcher = VERSION_ENCODED_PATTERN.matcher(encodedPassword); if (matcher.matches()) { String key = matcher.group(1); String body = matcher.group(2); PasswordEncoder encoder; if (encoders.containsKey(key)) { encoder = encoders.get(key); } else { log.warn("Unknown encoding type: {} ... using unknown encoder {}", key, unknownEncoder); encoder = encoders.get(unknownEncoder); } return encoder.matches(rawPassword, body); } else if (enableAutoDetection) { if (BCRYPT_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be BCrypt encoded password..."); if (BCRYPT.matches(rawPassword, encodedPassword)) { return true; } } if (STANDARD_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be Standard encoded password..."); if (STANDARD.matches(rawPassword, encodedPassword)) { return true; } } } return encoders.get(unknownEncoder).matches(rawPassword, encodedPassword); } catch (Exception e) { log.warn("Unexpected", e); } return false; } VersionedPasswordEncoder();  VersionedPasswordEncoder(String unknownEncoder, String primaryEncoder,
			Map<String, PasswordEncoder> encoders); @Override String encode(CharSequence rawPassword); @Override boolean matches(CharSequence rawPassword, String encodedPassword); }
VersionedPasswordEncoder implements PasswordEncoder { @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Matcher matcher = VERSION_ENCODED_PATTERN.matcher(encodedPassword); if (matcher.matches()) { String key = matcher.group(1); String body = matcher.group(2); PasswordEncoder encoder; if (encoders.containsKey(key)) { encoder = encoders.get(key); } else { log.warn("Unknown encoding type: {} ... using unknown encoder {}", key, unknownEncoder); encoder = encoders.get(unknownEncoder); } return encoder.matches(rawPassword, body); } else if (enableAutoDetection) { if (BCRYPT_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be BCrypt encoded password..."); if (BCRYPT.matches(rawPassword, encodedPassword)) { return true; } } if (STANDARD_PATTERN.matcher(encodedPassword).matches()) { log.info("It seems to be Standard encoded password..."); if (STANDARD.matches(rawPassword, encodedPassword)) { return true; } } } return encoders.get(unknownEncoder).matches(rawPassword, encodedPassword); } catch (Exception e) { log.warn("Unexpected", e); } return false; } VersionedPasswordEncoder();  VersionedPasswordEncoder(String unknownEncoder, String primaryEncoder,
			Map<String, PasswordEncoder> encoders); @Override String encode(CharSequence rawPassword); @Override boolean matches(CharSequence rawPassword, String encodedPassword);  }