BCryptHelper { public static int getCostFactor(final String bcryptedPassword) { final Matcher matcher = BCRYPT_PATTERN.matcher(bcryptedPassword); if (matcher.matches()) { if ("2a".equals(matcher.group(1))) { return Integer.valueOf(matcher.group(2)); } else { throw new IllegalArgumentException("invalid BCrypt version, supports 2a only"); } } else { throw new IllegalArgumentException("not a BCrypt hash"); } } private  BCryptHelper(); static int getCostFactor(final String bcryptedPassword);  }
@Test public void testGetCostFactorFailsForInvalidHash() { assertThatThrownBy(() -> BCryptHelper.getCostFactor("invalid-hash")).isInstanceOf(IllegalArgumentException.class); }
@Test public void testGetCostFactorFailsForUnsupportedVersion() { assertThatThrownBy(() -> BCryptHelper.getCostFactor("$2y$10$LgDCAvCL1IVbWrIty6RV4.NunlK67mAsj/0d6QXwW4VGD.9qnzU6q")) .isInstanceOf(IllegalArgumentException.class); }
@Test public void testGetCostFactorSucceedsForValidHash() { assertThat(BCryptHelper.getCostFactor("$2a$10$LgDCAvCL1IVbWrIty6RV4.NunlK67mAsj/0d6QXwW4VGD.9qnzU6q")) .isEqualTo(10); }