Java Code Examples for org.springframework.security.crypto.bcrypt.BCrypt#gensalt()

The following examples show how to use org.springframework.security.crypto.bcrypt.BCrypt#gensalt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChoerodonBcryptPasswordEncoder.java    From oauth-server with Apache License 2.0 5 votes vote down vote up
@Override
public String encode(CharSequence rawPassword) {
    String salt;
    if (strength > 0) {
        if (random != null) {
            salt = BCrypt.gensalt(strength, random);
        } else {
            salt = BCrypt.gensalt(strength);
        }
    } else {
        salt = BCrypt.gensalt();
    }
    return BCrypt.hashpw(rawPassword.toString(), salt);
}
 
Example 2
Source File: UserManager.java    From Much-Assembly-Required with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Register an user and initialises its controlled unit
 * @param username username
 * @param password plain password
 * @throws RegistrationException is username/password length is invalid
 */
public void registerUser(String username, String password) throws RegistrationException {

    if (username.length() < 5 || username.length() > 20) {
        throw new RegistrationException("Username must be 5-20 characters");
    }
    if (password.length() < 8 || password.length() > 96) {
        throw new RegistrationException("Password must be 8-96 characters");
    }

    //Check if exists
    Document where = new Document();
    where.put("_id", username);

    if (userCollection.find(where).first() != null) {
        throw new RegistrationException("Username is already in use");
    }

    try {
        User user = GameServer.INSTANCE.getGameUniverse().getOrCreateUser(username, true);
        user.setUsername(username);

        String salt = BCrypt.gensalt();
        String hashedPassword = BCrypt.hashpw(password, salt);
        user.setPassword(hashedPassword);

        Document dbUser = user.mongoSerialise();

        userCollection.insertOne(dbUser);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage());
    }
}
 
Example 3
Source File: UserManager.java    From Much-Assembly-Required with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Change the password of an user and immediately save it
 * @param username Username
 * @param newPassword New plain password
 * @throws RegistrationException When password length is invalid
 */
public void changePassword(String username, String newPassword) throws RegistrationException {

    if (newPassword.length() < 8 || newPassword.length() > 96) {
        throw new RegistrationException("Password must be 8-96 characters");
    }

    User user = GameServer.INSTANCE.getGameUniverse().getUser(username);

    String salt = BCrypt.gensalt();
    String hashedPassword = BCrypt.hashpw(newPassword, salt);
    user.setPassword(hashedPassword);

    userCollection.replaceOne(new Document("_id", username), user.mongoSerialise()); //Save new password immediately
}