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

The following examples show how to use org.springframework.security.crypto.bcrypt.BCrypt#hashpw() . 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: Encryptor.java    From syncope with Apache License 2.0 6 votes vote down vote up
public String encode(final String value, final CipherAlgorithm cipherAlgorithm)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        IllegalBlockSizeException, BadPaddingException {

    String encoded = null;

    if (value != null) {
        if (cipherAlgorithm == null || cipherAlgorithm == CipherAlgorithm.AES) {
            Cipher cipher = Cipher.getInstance(CipherAlgorithm.AES.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);

            encoded = Base64.getEncoder().encodeToString(cipher.doFinal(value.getBytes(StandardCharsets.UTF_8)));
        } else if (cipherAlgorithm == CipherAlgorithm.BCRYPT) {
            encoded = BCrypt.hashpw(value, BCrypt.gensalt());
        } else {
            encoded = getDigester(cipherAlgorithm).digest(value);
        }
    }

    return encoded;
}
 
Example 2
Source File: CustomPasswordEncoder.java    From webFluxTemplate with MIT License 5 votes vote down vote up
@Override
public String encode(CharSequence rawPassword) {
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);

    String hashed = BCrypt.hashpw(rawPassword.toString(), BCrypt.gensalt(14, random));
    return hashed;
}
 
Example 3
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 4
Source File: UserDao.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static String encrypt(String password) {
    String ret = null;

    try {
        ret = BCrypt.hashpw(password, BCrypt.gensalt());
    } catch (Throwable excp) {
        LOG.warn("UserDao.encrypt(): failed", excp);
    }

    return ret;
}
 
Example 5
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 6
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
}
 
Example 7
Source File: ApplicationUserServiceImpl.java    From ReCiter with Apache License 2.0 5 votes vote down vote up
@Override
public boolean createUser(ApplicationUser appUser) {
	if(appUser.getId() != null
			&& !appUser.getId().isEmpty()
			&& appUser.getUsername() != null
			&& !appUser.getUsername().isEmpty()
			&& appUser.getPassword() != null
			&& !appUser.getPassword().isEmpty()) {
		String password = BCrypt.hashpw(appUser.getPassword(), secretsalt);
		appUser.setPassword(password);
		applicationUserRepository.save(appUser);
		return true;
	}
	return false;
}
 
Example 8
Source File: StrongPasswordEncoder.java    From vics with MIT License 4 votes vote down vote up
@Override
public String encode(CharSequence rawPassword) {
    return BCrypt.hashpw(rawPassword.toString(), BCrypt.gensalt());
}
 
Example 9
Source File: UserService.java    From vics with MIT License 4 votes vote down vote up
private String hashPw(String password) {
    return BCrypt.hashpw(password, BCrypt.gensalt());
}
 
Example 10
Source File: CryptoUtil.java    From personal_book_library_web_project with MIT License 2 votes vote down vote up
public static String cryptPassword(String password) {
	
	return BCrypt.hashpw(password, BCrypt.gensalt());
}
 
Example 11
Source File: CustomPasswordEncoder.java    From spring-microservice-boilerplate with MIT License 2 votes vote down vote up
/**
 * Encode the password.
 *
 * @param rawPassword raw password
 * @return encoded password
 */
@Override public String encode(CharSequence rawPassword) {
  String rawPwd = (String) rawPassword;
  return BCrypt.hashpw(rawPwd, BCrypt.gensalt());
}