Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Created June 11, 2010 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezpablo85/434466 to your computer and use it in GitHub Desktop.
Save fernandezpablo85/434466 to your computer and use it in GitHub Desktop.
public class Test
{
private static final String MD5 = "MD5";
private static final String UTF_8 = "UTF-8";
public static void main(String[] args) throws Exception
{
String email = "example@gmail.com";
byte[] digestBytes = hash(email);
System.out.println(digestBytes.length);
System.out.println(Arrays.toString(digestBytes));
String hexa = bytesToHexa(digestBytes);
byte[] hexaBytes = hexa.getBytes("UTF-8");
System.out.println(hexa.length());
System.out.println(Arrays.toString(hexaBytes));
}
private static String bytesToHexa(byte[] digest)
{
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < digest.length; i++)
{
hexString.append(Integer.toHexString(0xFF & digest[i]));
}
return hexString.toString();
}
private static byte[] hash(String email) throws Exception
{
byte[] bytes = email.getBytes(UTF_8);
MessageDigest md = MessageDigest.getInstance(MD5);
return md.digest(bytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment