6

I can't figure out what I'm doing wrong here. I have the following code:

byte[] digest = new byte[0];
MessageDigest md = null;
try{
    md = MessageDigest.getInstance( "SHA-512" );
}
catch( NoSuchAlgorithmException e ) {
    return digest;
}
digest = md.digest( myString.getBytes() );

Looking at the hex values of digest byte[] in the NetBeans debugger, it shows something different than the output of:

echo "myString" | openssl dgst -sha512

I'm guessing it's a character encoding issue, but doesn't the JVM and openssl use the default character set for the machine?

Any help is appreciated.

David Z
  • 128,184
  • 27
  • 255
  • 279
Tim
  • 377
  • 1
  • 4
  • 6
  • Character encodings shouldn't be an issue for `"myString"`, since that's pure ASCII. But you might want to examine the return value of `myString.getBytes()` just to make sure. – David Z Jun 26 '10 at 18:17

1 Answers1

8

echo appends a newline at the end -

[steven@emu:~]% echo "myString" | hexdump -C
00000000  6d 79 53 74 72 69 6e 67  0a                       |myString.|

Try echo -n?

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100