0

I have a question about how java prints the HexString and how to print it in the same format that C# using the BitConverter does.

We (both) are generating this Hex as result of hashing a string with SHA-1 :

a94a8fe5ccb19ba61c4c873d391e987982fbbd3 (hash for "test" string)

The problem is when we test the hashing in C# , C# prints with uppercase and separated with "-" , like A9-4A-8F .. and so on.

How can I print the same format with java?

The code for hashing and printing is this:

 String password = "test";

        byte[] key = password.getBytes();

        MessageDigest md = MessageDigest.getInstance("SHA-1",new BouncyCastleProvider());

        byte[] hash = md.digest(key);

        String result = "";
        for (byte b : hash) {
            result += Integer.toHexString(b & 255);
        }
        return result;
Rafihna
  • 15
  • 7

1 Answers1

0
String result = "";
for (byte b : hash) {
    result += Integer.toHexString(b & 255).toUpperCase() + "-";
}
return result.subString(0, result.length()-1);
Jonathon Chase
  • 9,396
  • 21
  • 39