2011-08-29 27 views
-1
import java.security.MessageDigest; 
    class Enc{ 

      public String encryptPassword(String password) throws Exception{ 
        byte[] bArray=password.getBytes(); 
        MessageDigest md=MessageDigest.getInstance("SHA-1"); 
        md.reset(); 
        md.update(bArray); 
        byte[] encoded=md.digest(); 
        System.out.println(encoded.toString()); 

        return ""; 
      } 
      public static void main(String args[]){ 
        try{ 
        Enc e=new Enc(); 
        e.encryptPassword("secret"); 
        }catch(Exception e){e.printStackTrace();} 
      } 
    } 

/* 

jabira-whosechild-lm.local 12:40:35 % while (true); do java Enc; done 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
[[email protected] 
*/ 

回答

3

你只是打印出byte[].toString這不是內容的散列

System.out.println(encoded.toString()); 

要顯示散列作爲文本,則應該字節數組爲十六進制或Base64轉換 - 有對堆棧溢出片段的負載來實現這一目標(例如,使用Apache Commons Codec)。如果你不需要散列作爲文本,你可以將它保留爲字節數組。

還要注意的是,你不應該使用這個代碼:

byte[] bArray=password.getBytes() 

將使用系統默認的字符編碼,它可以從系統而異,並且可能不能夠編碼所有的Unicode的。使用固定的編碼,例如UTF-8,不管系統默認情況如何,這些編碼將始終爲相同的輸入提供相同的結果,並且可以對所有的Unicode進行編碼。

0

這裏是一段代碼片斷我到MD5整個文件。它對我有用,當我MD5ed一個文件,我想發送,看看他們的客戶端是否已經有相同的文件。如果需要完整的源可爲什麼還要頗費周折使用該文件的長度,而不是僅僅讀,直到`read`調用返回-1找到here on Github

private static String getMD5Digest(File file) { 
    BufferedInputStream reader = null; 
    String hexDigest = new String(); 
    try { 
     reader = new BufferedInputStream(new FileInputStream(file)); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    MessageDigest md = null; 
    try { 
     md = MessageDigest.getInstance("MD5"); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } 
    byte[] buffer = new byte[4096]; 
    long fileLength = file.length(); 
    long bytesLeft = fileLength; 
    int read = 0; 
    //Read our file into the md buffer 
    while(bytesLeft > 0){ 
     try { 
      read = reader.read(buffer,0, bytesLeft < buffer.length ? (int)bytesLeft : buffer.length); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     md.update(buffer,0,read); 
     bytesLeft -= read; 
    } 
    byte[] digest = md.digest(); 
    for (int i = 0; i < digest.length;i++) { 
     hexDigest += String.format("%02x" ,0xFF & digest[i]); 
    } 
    try { 
     reader.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return hexDigest; 
} 
+0

?另外,爲什麼要創建一個新的空字符串而不是使用「」?另外,如果發生IOException異常,就像沒有任何事情發生一樣......並且如果沒有經過檢查的異常,那麼您將讓讀者打開...(您應該使用finally塊)。 –

+0

我從來沒有說過它是好代碼。我說這是代碼工作:) 閱讀while bytesLeft> 0是更確定性的。它可能會捕獲/ dev/zero的邊緣情況,但我沒有嘗試。 在Ruby中String.new和「」是相等的,我認爲Java也是一樣。 這些調用被封裝在服務器/客戶端的try catch上,如果我正確記得清理套接字。 是的,這不是最好的代碼。 – EnabrenTane

+0

不,在Java中「」和「new String()」不一樣 - 後者總是創建一個新對象,前者不會。 (無論如何,最好使用StringBuilder)。無論套接字是什麼,由於缺少finally塊,* file *句柄可能會在上面的代碼中泄露 - 並且它只是在出錯時停止而不是指示給調用者一些問題是非常討厭的,IMO。 (我只是讓這個方法拋出IOException。) –