Skip to content

Instantly share code, notes, and snippets.

@dilnei
Created August 6, 2014 17:20
Show Gist options
  • Select an option

  • Save dilnei/d50936059b7f53afb5a2 to your computer and use it in GitHub Desktop.

Select an option

Save dilnei/d50936059b7f53afb5a2 to your computer and use it in GitHub Desktop.
Responsável por gerar um hash MD5
package br.com.risingforce.geradores;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* <b>Classe responsável por gerar um hash MD5.</b>
*
* @author Dilnei Cunha.
*/
public class MD5Hashing {
/**
* <b>Gera Hash MD5 e retorna o valor como uma string.</b>
*
* @param str
* @return String
* @throws NoSuchAlgorithmException
*/
public static String generateHashMD5(String str) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
//convert the byte to hex format method 2
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < byteData.length; i++) {
String hex = Integer.toHexString(0xff & byteData[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public static void main(String args[]) throws NoSuchAlgorithmException {
System.out.println(generateHashMD5("123456"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment