2016-07-22 41 views

回答

0

自己的例子,創建一個實用工具類散列,使用這樣的靜態方法...

public class MD5Util { 
    private static final String ALGORITHM = "MD5"; 
    public static String getHash(String text){ 
     try { 
      MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
      md.update(text.getBytes()); 
      byte[] byteData = md.digest(); 
      StringBuffer hexString = new StringBuffer(); 
      for(byte bd : byteData){ 
       String hex = Integer.toHexString(bd & 0xff); 
       if(hex.length() == 1) 
        hexString.append(0); 
       hexString.append(hex); 
      } 
      return hexString.toString(); 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(MD5Util.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return null; 
    } 
} 

然後,調用靜態方法和結果集到標籤字段,像這樣...

hashValueLabel.setText(MD5Util.getHash(textInputLabel.getText()));