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)