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]
*/
-1
A
回答
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;
}
相關問題
- 1. 精華版
- 2. 在Node.js的計算SHA1返回不同的結果比在PHP
- 3. 從couchdb返回sha1()hash
- 4. OpenId總是返回HMAC-SHA1
- 5. Objective-C的精華指數:
- 6. Java的MessageDigest SHA1算法返回的結果不是SHA1的函數php
- 7. Keystore SHA1不同於cert.rsa SHA1 APK
- 8. 不同的SHA1但相同的SHA256
- 9. 紅寶石SHA1標誌返回不同的結果比OpenSSL簽名
- 10. 如何將github回購中的文件轉換爲精華
- 11. SHA1計算返回錯誤值
- 12. HMAC-SHA1不返回所需散列的示例?
- 13. Go Hmac SHA1生成與Java中的Hmac SHA1不同的哈希
- 14. SQL公式返回不一致精度
- 15. iOS'dateFromString'返回不同?
- 16. 與標準Java HMAC-SHA1不同的Android HMAC-SHA1
- 17. Python HMAC-SHA1與Java HMAC-SHA1不同的結果
- 18. GetHashCode返回不同的值
- 19. toString返回不同的值
- 20. LAMBDA返回不同的值
- 21. cURL返回不同的
- 22. TextClock返回不同的值
- 23. FileUpload.getFilename()返回的值不同
- 24. 返回不同的表格
- 25. Zenity - 不同的返回值
- 26. UserDefault返回不同的值
- 27. 返回不同的日期
- 28. getRGB返回不同的值
- 29. 不同的返回類型
- 30. Html.Hidden返回不同的值
?另外,爲什麼要創建一個新的空字符串而不是使用「」?另外,如果發生IOException異常,就像沒有任何事情發生一樣......並且如果沒有經過檢查的異常,那麼您將讓讀者打開...(您應該使用finally塊)。 –
我從來沒有說過它是好代碼。我說這是代碼工作:) 閱讀while bytesLeft> 0是更確定性的。它可能會捕獲/ dev/zero的邊緣情況,但我沒有嘗試。 在Ruby中String.new和「」是相等的,我認爲Java也是一樣。 這些調用被封裝在服務器/客戶端的try catch上,如果我正確記得清理套接字。 是的,這不是最好的代碼。 – EnabrenTane
不,在Java中「」和「new String()」不一樣 - 後者總是創建一個新對象,前者不會。 (無論如何,最好使用StringBuilder)。無論套接字是什麼,由於缺少finally塊,* file *句柄可能會在上面的代碼中泄露 - 並且它只是在出錯時停止而不是指示給調用者一些問題是非常討厭的,IMO。 (我只是讓這個方法拋出IOException。) –