2012-04-18 58 views
-1

我想哈希使用沙256的網址,但我有幾個問題。我已經將字節轉換爲一個字符串,當我記錄該字符串它顯示不正確,而不是一個32字符長的蘭特字符se它顯示此:Android哈希不工作

04-18 11:46:00.427:V/myApp(797 ):CrE.mm 「7 {」 Q]米

任何幫助,將不勝感激

繼承人我的代碼:

public void hash() throws NoSuchAlgorithmException, UnsupportedEncodingException{ 

     MessageDigest md = MessageDigest.getInstance("SHA-256"); 
     md.update(fixturesFeedURL.getBytes("UTF-8")); 
     byte[] digest = md.digest(); 
     String strhash = new String(digest); 
     Log.v("myApp", strhash); 
    } 
+0

請參閱http://stackoverflow.com/questions/5470219/java-get-md5-字符串從消息摘要 – Rajesh 2012-04-18 11:26:03

回答

0

下面是我如何做MD5:

MessageDigest md = MessageDigest.getInstance("MD5"); 
byte[] b = md.digest(input.getBytes()); 
StringBuffer output = new StringBuffer(); 
for (int i = 0; i < b.length; i++) { 
    String tmpStr = "0" + Integer.toHexString((0xff & b[i])); 
    output.append(tmpStr.substring(tmpStr.length() - 2)); 
} 
return output.toString(); 

也許只是改變了消息摘要算法的問題...

+0

我怎麼能登錄這個滑雪可以看看它是否工作 – iamlukeyb 2012-04-18 12:10:10

0

你的問題是字節數​​組轉換爲字符串 - 您可以使用它來做到這一點:

HTTP://commons.apache .org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#encodeHex(byte [])