]> git.mxchange.org Git - juser-login-core.git/commitdiff
added method generatedConfirmationKey()
authorRoland Häder <roland@mxchange.org>
Fri, 13 May 2016 12:42:27 +0000 (14:42 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 13 May 2016 12:42:27 +0000 (14:42 +0200)
src/org/mxchange/jusercore/model/user/UserUtils.java

index bd2c898ae0558f9343b910a6404c690dd4219aeb..8aa0b11f455c8a739c125ac84cc6ba7bdcbc90e1 100644 (file)
@@ -21,6 +21,8 @@ import java.security.SecureRandom;
 import java.text.MessageFormat;
 import java.util.Random;
 import org.apache.commons.codec.digest.Crypt;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jusercore.container.login.LoginContainer;
 
 /**
@@ -137,10 +139,62 @@ public class UserUtils implements Serializable {
                return userName;
        }
 
+       /**
+        * Generate a key suitable for confirmation. This is basicly a large and
+        * strong hash with a lop entropy.
+        * <p>
+        * @param user User instance to use as additional entropy source
+        * <p>
+        * @return Generated key
+        */
+       public static String generatedConfirmationKey (final User user) {
+               // Generates random string by creating a random, encrypted password
+               StringBuilder key = new StringBuilder(encryptPassword(generateRandomUserName()));
+
+               // Is user set?
+               if (user instanceof User) {
+                       // Add it's name, too
+                       key.append(":").append(user); //NOI18N
+
+                       // Is user name set?
+                       if (user.getUserName() instanceof String) {
+                               // Add it
+                               key.append(":").append(user.getUserName()); //NOI18N
+                       }
+
+                       // Is password set?
+                       if (user.getUserEncryptedPassword() instanceof String) {
+                               // Add it, too
+                               key.append(":").append(user.getUserEncryptedPassword()); //NOI18N
+                       }
+
+                       // Get contact instance
+                       Contact contact = user.getUserContact();
+
+                       // Is contact set?
+                       if (contact instanceof Contact) {
+                               // Add it, too
+                               key.append(":").append(contact); //NOI18N
+
+                               // Is email address set?
+                               if (contact.getContactEmailAddress() instanceof String) {
+                                       // Add it, too
+                                       key.append(":").append(contact.getContactEmailAddress()); //NOI18N
+                               }
+                       }
+               }
+
+               // Hash key
+               String hash = DigestUtils.sha256Hex(key.toString());
+
+               // Return it
+               return hash;
+       }
+
        /**
         * Checks if password from container matches the updatedUser's password
         * <p>
-        * @param container   Container holding user instance and unencrypted password
+        * @param container Container holding user instance and unencrypted password
         * @param updatedUser Updated user instance from database
         * <p>
         * @return Whether the password matches