-1

I'm trying to hash a url using Sha-256 but i'm having a few problems. i have converted the bytes into a string and when i log that string its showing up incorrect instead of a 32 character long se of rand characters its showing this:

04-18 11:46:00.427: V/myApp(797): �C�rE�������.mm"7�{���"��Q]m

Any help would be greatly appreciated

heres my code:

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);
    }   
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40
  • Please see http://stackoverflow.com/questions/5470219/java-get-md5-string-from-message-digest – Rajesh Apr 18 '12 at 11:26

2 Answers2

0

Here is how I do it for 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();

Probably just a matter of changing the MessageDigest algorithm...

Greg
  • 196
  • 1
  • 3
0

your problem is converting the byte array to String - you could use this to do it:

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

ligi
  • 39,001
  • 44
  • 144
  • 244