02 Jan 2012
Java Tips: Gerando hash MD5
Esse é uma dica bem simples de como gerar hash md5 de uma String no Java:
import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashMd5 { public static void main(String[] args) { HashMd5 hashMd5 = new HashMd5(); System.out.println(hashMd5.md5("Calcular hash deste texto")); } public String md5(String str) { MessageDigest m = null; String md5hash = null; try { m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } if(m != null) { m.update(str.getBytes(),0,str.length()); BigInteger i = new BigInteger(1, m.digest()); md5hash = String.format("%1$032x", i); } return md5hash; } }
Resultado:
Hash Gerado: d24300b797dfab0df069cb6aaab442b4
Espero que possa ser útil em algum momento.
Please follow and like us:
Obrigado amigo foi muito útil!
Que bom que ajudou Romulo, obrigado pelo feedback! Abs!