A: 

Yes, you need to turn your byte array into a hex string. :-) Look into Apache Commons Codec, especially the Hex class.

Chris Jester-Young
+7  A: 

The value you're expecting is a Hex-encoded value. You're creating a String based on the raw bytes, which won't work.

You should use the standard Java Crypto API whenever possible instead of BouncyCastle specific APIs.

Try the following (the Hex library comes from commons-codec):

Security.addProvider(new BouncyCastleProvider());

String data = "hello world";

MessageDigest mda = MessageDigest.getInstance("SHA-512", "BC");
byte [] digesta = mda.digest(data.getBytes());

MessageDigest mdb = MessageDigest.getInstance("SHA-512", "BC");
byte [] digestb = mdb.digest(data.getBytes());

System.out.println(MessageDigest.isEqual(digesta, digestb));

System.out.println(Hex.encodeHex(digesta));
Kevin
+1 I like your more-comprehensive answer. Question: when/why would one use MessageDigest.isEqual over Arrays.equals?
Chris Jester-Young
They're functionally equivalent. MessageDigest#isEqual() provides a little more semantic meaning but that is debatable.
Kevin
They are not entirely equivalent. The Arrays.equals methods consider two null references to be equal, while the MessageDigest.isEqual method would throw a NullPointerException.
jarnbjo
+4  A: 

Just an addition to Kevin's answer: Since Java 5, you can use String.format("%0128x", new BigInteger(1, digesta)) instead of commons-codec to format the byte array as a 128 digit hex encoded number with leading zeros.

jarnbjo