2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jusercore.model.user;
19 import java.security.SecureRandom;
20 import java.text.MessageFormat;
21 import java.util.Random;
22 import org.apache.commons.codec.digest.Crypt;
23 import org.mxchange.jcore.BaseFrameworkSystem;
24 import org.mxchange.jusercore.container.login.LoginContainer;
25 import org.mxchange.jusercore.container.login.UserLoginContainer;
28 * An utilities class for users
30 * @author Roland Haeder<roland@mxchange.org>
32 public class UserUtils extends BaseFrameworkSystem {
37 private static final String PASSWORD_ALPHABET = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY0123456789-/?!_+#@"; //NOI18N
40 * Minimum password length
42 private static final Integer PASSWORD_MINIMUM_LENGTH = 5;
45 * Random number generator
47 private static final Random RANDOM_NUMBER_GENERATOR;
54 RANDOM_NUMBER_GENERATOR = new SecureRandom();
58 * Creates a pseudo-random password with given length
60 * @param length Length of the password
62 * @return Pseudo-random password
64 public static String createRandomPassword (final Integer length) {
65 // Parameter should be valid
68 throw new NullPointerException("length is null"); //NOI18N
69 } else if (length < PASSWORD_MINIMUM_LENGTH) {
71 throw new IllegalArgumentException(MessageFormat.format("Password length {0} is to short, minimum: {1}", length, PASSWORD_MINIMUM_LENGTH)); //NOI18N
75 StringBuilder password = new StringBuilder(length);
78 for (int i = 0; i < length; i++) {
79 // Generate random number
80 int pos = RANDOM_NUMBER_GENERATOR.nextInt(PASSWORD_ALPHABET.length());
82 // Get char at this position and add it to the final password
83 password.append(String.valueOf(PASSWORD_ALPHABET.charAt(pos)));
86 // Should have the wanted length
87 assert (password.length() == length) : MessageFormat.format("Password length {0} doesn't match requested: {1}", password.length(), length); //NOI18N
90 return password.toString();
94 * Hashes given user password and adds a salt to it
96 * @param userPassword User password to be hashed
98 * @return Hashed user password
100 public static String encryptPassword (final String userPassword) {
101 // Is it null or empty?
102 if (null == userPassword) {
104 throw new NullPointerException("userPassword is null"); //NOI18N
105 } else if (userPassword.isEmpty()) {
106 // Empty passwords are hardcoded not allowed due to security risks
107 throw new IllegalArgumentException("userPassword is empty"); //NOI18N
110 // Generate large number
111 String number = Long.toString(RANDOM_NUMBER_GENERATOR.nextLong() * 10_000_000_000L);
114 String salt = Crypt.crypt(number);
116 // First encrypt password
117 String encryptedPassword = Crypt.crypt(userPassword, salt);
120 return encryptedPassword;
124 * Checks if password from container matches the updatedUser's password
126 * @param container Container holding user instance and unencrypted password
127 * @param updatedUser Updated user instance from database
129 * @return Whether the password matches
131 public static boolean ifPasswordMatches (final LoginContainer container, final User updatedUser) {
132 // Validate parameters
133 if (null == container) {
135 throw new NullPointerException("container is null"); //NOI18N
136 } else if (null == updatedUser) {
138 throw new NullPointerException("updatedUser is null"); //NOI18N
139 } else if (container.getUser() == null) {
140 // NPE for user in container
141 throw new NullPointerException("container.user is null"); //NOI18N
142 } else if (container.getUserPassword() == null) {
143 // NPE for user password in container
144 throw new NullPointerException("container.userPassword is null"); //NOI18N
145 } else if (container.getUserPassword().isEmpty()) {
146 // Empty password in container
147 throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
150 // First encrypt password
151 String encryptedPassword = Crypt.crypt(container.getUserPassword(), updatedUser.getUserEncryptedPassword());
154 return encryptedPassword.equals(updatedUser.getUserEncryptedPassword());
158 * Checks if password from container matches with from user instance.
160 * @param container Container holding user instance and unencrypted password
162 * @return Whether it maches
164 public static boolean ifPasswordMatches (final UserLoginContainer container) {
165 // Validate parameters
166 if (null == container) {
168 throw new NullPointerException("container is null"); //NOI18N
169 } else if (container.getUser() == null) {
170 // NPE for user in container
171 throw new NullPointerException("container.user is null"); //NOI18N
172 } else if (container.getUserPassword() == null) {
173 // NPE for user password in container
174 throw new NullPointerException("container.userPassword is null"); //NOI18N
175 } else if (container.getUserPassword().isEmpty()) {
176 // Empty password in container
177 throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N
180 // First encrypt password
181 String encryptedPassword = Crypt.crypt(container.getUserPassword(), container.getUser().getUserEncryptedPassword());
184 return encryptedPassword.equals(container.getUser().getUserEncryptedPassword());
188 * No instance from this class
190 private UserUtils () {