/** * Generate a salt for use with the BCrypt.hashpw() method, * selecting a reasonable default for the number of hashing * rounds to apply. * * @return an encoded salt value */ public static String gensalt() { return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS); }
/** * Generate a salt for use with the BCrypt.hashpw() method. * * @param log_rounds the log2 of the number of rounds of * hashing to apply - the work factor therefore increases as * 2**log_rounds. * @return an encoded salt value */ public static String gensalt(int log_rounds) { return gensalt(log_rounds, new SecureRandom()); }
/** * Returns the {@link BCrypt} hash tool with given rounds number for salt generation. */ public static HashEngine bcrypt(final int rounds) { return bcrypt(BCrypt.gensalt(rounds)); }
/** * Encodes the {@link SimTok} to JSON string. */ public String encode(final SimTok simTok) { final String json = JsonSerializer.create().deep(true).serialize(simTok); final String p1 = Base64.encodeToString("JoddSimTok" + SALT_ROUNDS); final String p2 = Base64.encodeToString(json); final String salt = BCrypt.gensalt(SALT_ROUNDS); final String p3 = BCrypt.hashpw(p1 + "." + p2 + "." + SECRET, salt); return p1 + "." + p2 + "." + p3; }
/** * Test for correct hashing of non-US-ASCII passwords */ @Test void testInternationalChars() { String pw1 = "\u2605\u2605\u2605\u2605\u2605\u2605\u2605\u2605"; String pw2 = "????????"; String h1 = BCrypt.hashpw(pw1, BCrypt.gensalt()); assertFalse(BCrypt.checkpw(pw2, h1)); String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt()); assertFalse(BCrypt.checkpw(pw1, h2)); }
/** * Test method for 'BCrypt.gensalt()' */ @Test void testGensalt() { for (int i = 0; i < test_vectors.length; i += 4) { String plain = test_vectors[i][0]; String salt = BCrypt.gensalt(); String hashed1 = BCrypt.hashpw(plain, salt); String hashed2 = BCrypt.hashpw(plain, hashed1); assertEquals(hashed1, hashed2); } }
@Test void testBCrypt() { String hash = BCrypt.hashpw("password", BCrypt.gensalt(7)); assertTrue(BCrypt.checkpw("password", hash)); }
/** * Test method for 'BCrypt.gensalt(int)' */ @Test void testGensaltInt() { for (int i = 4; i <= 12; i++) { for (int j = 0; j < test_vectors.length; j += 4) { String plain = test_vectors[j][0]; String salt = BCrypt.gensalt(i); String hashed1 = BCrypt.hashpw(plain, salt); String hashed2 = BCrypt.hashpw(plain, hashed1); assertEquals(hashed1, hashed2); } } }
@Test void testBCryptRandom() { for (int rounds = 0; rounds < 1000; rounds++) { String text = RandomString.get().randomAlphaNumeric(10); String hash = BCrypt.hashpw(text, BCrypt.gensalt(4)); assertTrue(BCrypt.checkpw(text, hash)); } }
/** * Generate a salt for use with the BCrypt.hashpw() method, * selecting a reasonable default for the number of hashing * rounds to apply. * * @return an encoded salt value */ public static String gensalt() { return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS); }
/** * Generate a salt for use with the BCrypt.hashpw() method. * * @param log_rounds the log2 of the number of rounds of * hashing to apply - the work factor therefore increases as * 2**log_rounds. * @return an encoded salt value */ public static String gensalt(int log_rounds) { return gensalt(log_rounds, new SecureRandom()); }
/** * Returns the {@link BCrypt} hash tool with given rounds number for salt generation. */ public static HashEngine bcrypt(final int rounds) { return bcrypt(BCrypt.gensalt(rounds)); }
/** * Encodes the {@link SimTok} to JSON string. */ public String encode(final SimTok simTok) { final String json = JsonSerializer.create().deep(true).serialize(simTok); final String p1 = Base64.encodeToString("JoddSimTok" + SALT_ROUNDS); final String p2 = Base64.encodeToString(json); final String salt = BCrypt.gensalt(SALT_ROUNDS); final String p3 = BCrypt.hashpw(p1 + "." + p2 + "." + SECRET, salt); return p1 + "." + p2 + "." + p3; }