package com.gr.project.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashUtil { public static void main(String[] args) { System.out.println(md5HexString("qwert")); } public static String md5HexString(String password) { MessageDigest md = null; String md5 = null; try { md = MessageDigest.getInstance("MD5"); md.update(password.getBytes()); byte byteData[] = md.digest(); //convert the byte to hex format method 2 StringBuffer hexString = new StringBuffer(); 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); } md5 = hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return md5; } public static String generateHash(String seed) { String toHash = seed; MessageDigest md = null; byte[] hash = null; try { md = MessageDigest.getInstance("SHA-512"); hash = md.digest(toHash.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return convertToHex(hash); } /** * Converts the given byte[] to a hex string. * * @param raw * the byte[] to convert * @return the string the given byte[] represents */ private static String convertToHex(byte[] raw) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < raw.length; i++) { sb.append(Integer.toString((raw[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } }