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