]> git.mxchange.org Git - juser-login-core.git/blob - src/org/mxchange/jusercore/model/user/UserUtils.java
It is now user and no need for SALT_LENGTH anymore
[juser-login-core.git] / src / org / mxchange / jusercore / model / user / UserUtils.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import org.apache.commons.codec.digest.Crypt;
20 import org.mxchange.jcore.BaseFrameworkSystem;
21 import org.mxchange.jusercore.container.login.LoginContainer;
22
23 /**
24  * An utilities class for users
25  * <p>
26  * @author Roland Haeder<roland@mxchange.org>
27  */
28 public class UserUtils extends BaseFrameworkSystem {
29
30         /**
31          * Hashes given user password and adds a salt to it
32          * <p>
33          * @param userPassword User password to be hashed
34          * <p>
35          * @return Hashed user password
36          */
37         public static String encryptPassword (final String userPassword) {
38                 // Is it null or empty?
39                 if (null == userPassword) {
40                         // Throw NPE
41                         throw new NullPointerException("userPassword is null");
42                 } else if (userPassword.isEmpty()) {
43                         // Empty passwords are hardcoded not allowed due to security risks
44                         throw new IllegalArgumentException("userPassword is empty");
45                 }
46
47                 // Generate large number
48                 String number = Long.toString(Math.round(Math.random() * 10_000_000_000L));
49
50                 // Generate salt
51                 String salt = Crypt.crypt(number);
52
53                 // First encrypt password
54                 String encryptedPassword = Crypt.crypt(userPassword, salt);
55
56                 // Return it
57                 return encryptedPassword;
58         }
59
60         /**
61          * Checks if password from container matches the updatedUser's password
62          * <p>
63          * @param container   Container holding user instance and unencrypted
64          *                    password
65          * @param updatedUser Updated user instance from database
66          * <p>
67          * @return Whether the password matches
68          */
69         public static boolean ifPasswordMatches (final LoginContainer container, final User updatedUser) {
70                 // Validate parameters
71                 if (null == container) {
72                         // Throw NPE
73                         throw new NullPointerException("container is null");
74                 } else if (null == updatedUser) {
75                         // And again NPE ...
76                         throw new NullPointerException("updatedUser is null");
77                 } else if (container.getUser() == null) {
78                         // NPE for user in container
79                         throw new NullPointerException("container.user is null");
80                 } else if (container.getUserPassword() == null) {
81                         // NPE for user password in container
82                         throw new NullPointerException("container.userPassword is null");
83                 } else if (container.getUserPassword().isEmpty()) {
84                         // Empty password in container
85                         throw new IllegalArgumentException("container.userPassword is empty");
86                 }
87
88                 // First encrypt password
89                 String encryptedPassword = Crypt.crypt(container.getUserPassword(), updatedUser.getUserEncryptedPassword());
90
91                 // Is it matching?
92                 return encryptedPassword.equals(updatedUser.getUserEncryptedPassword());
93         }
94
95         /**
96          * No instance from this class
97          */
98         private UserUtils () {
99         }
100 }