Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate unique number for an email in JAVA?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 419
    Comment on it

    Sometimes we need to generate a unique number for an emailId, for example when a user register then we need to generate a number by using that user can verify his EmailId. For this we can use SHA1 encoding method.

    Example - In the below method I'm generating unique number for the provided emailId by using SHA1.

        import java.security.MessageDigest;
        import java.security.NoSuchAlgorithmException;
    
        /**
         * This method will generate unique code based on emailID
         * @param emailID
         * @return
         */
        public static String generateEncodedKey(String emailID)
        {
             String generatedKey="";
         try {
                MessageDigest md = MessageDigest.getInstance("SHA1");
                md.update(emailID.getBytes());
                byte[] bytes = md.digest();
                StringBuilder sb = new StringBuilder();
                for(int i=0; i< bytes.length ;i++){
                    sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
                }
                generatedKey = sb.toString().substring(0, 8);
            }
            catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }
    
            return generatedKey;
        }


    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: