AshotN
AshotN

Reputation: 413

Java Make String to SHA1

I am trying to convert a String to a SHA1 hash!

This is my code

public static void SHA1(String x) throws NoSuchAlgorithmException
{

    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    SHA1 = sha1.digest((x).getBytes()); 

}

I have a private static byte[] SHA1;

Sadly the output comes out like this

[B@1a758cb

I am trying to make the code as small as possible! Thanks

Upvotes: 1

Views: 3320

Answers (1)

nos
nos

Reputation: 229098

You have to print the bytes in your array, and you'd likely want to display the hash as hex.

for(byte b : SHA1 ) {
  System.out.printf("%02x",b);
}
System.out.println();

Upvotes: 4

Related Questions