1

When I reproduce a SHA2 hash via the following code:

MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.digest("A".getBytes("UTF-8"));

it gives me a byte array, which is: 85,-102,-22,-48,-126,100,-43,121,93,57,9,113,-116,-35,5,-85,-44,-107,114,-24,79,-27,85,-112,-18,-13,26,-120,-96,-113,-33,-3

But when I reproduce same hash via MySQL it gives me a string which is: 5cfe2cddbb9940fb4d8505e25ea77e763a0077693dbb01b1a6aa94f2

How can I convert tha Java's result so that I can compare it with MySQL's result?

markAnthopins
  • 237
  • 2
  • 12
  • Use a string formatter to convert the byte array to hex string –  Nov 11 '12 at 13:27
  • What do you mean with: `it gives me a byte array, which is:85-102-22-48-126100-...` how can a `byte[]` have values bigger than 127? – jlordo Nov 11 '12 at 13:36
  • output format may cause misunderstanding. Here is the values seperated with a comma: 85,-102,-22,-48,-126,100,-43,121,93,57,9,113,-116,-35,5,-85,-44,-107,114,-24,79,-27,85,-112,-18,-13,26,-120,-96,-113,-33,-3 – markAnthopins Nov 11 '12 at 13:41
  • converting to hex string doesn't give the same value also. – markAnthopins Nov 11 '12 at 13:46
  • 1
    ah, ok. So `85` (decimal) is `55` hex, your MySQL hash starts with `5c`, are you sure both are using the same algorithm? – jlordo Nov 11 '12 at 13:46
  • Here is the result: I thought these are the same: MySQL: SHA2('A',224) Java: ("SHA-256") Obviously they were not the same. So it must be: MySQL: SHA2('A',256) Java: ("SHA-256") Also this was useful: http://stackoverflow.com/questions/4680661/java-sha256-outputs-different-hash-to-php-sha256 – markAnthopins Nov 11 '12 at 14:01

2 Answers2

5

First check out your DB result it looks like your initial hash is actually a SHA-224 not SHA-256:

mysql> SELECT SHA2("A", 224);
+----------------------------------------------------------+
| SHA2("A", 224)                                           |
+----------------------------------------------------------+
| 5cfe2cddbb9940fb4d8505e25ea77e763a0077693dbb01b1a6aa94f2 |
+----------------------------------------------------------+
1 row in set (0.00 sec)

Instead of:

mysql> SELECT SHA2("A", 256);
+------------------------------------------------------------------+
| SHA2("A", 256)                                                   |
+------------------------------------------------------------------+
| 559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd |
+------------------------------------------------------------------+
1 row in set (0.06 sec)

From there you're on the right track you just need to convert the byte[] output to a hex string.

import java.security.MessageDigest;

public class TestSHA256 {

    public static void main(String[] args) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest("A".getBytes("UTF-8"));

            StringBuilder hexString = new StringBuilder();
            for (int i: hash) {
                hexString.append(Integer.toHexString(0XFF & i));
            }
            System.out.println(hexString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

559aead08264d5795d399718cdd5abd49572e84fe55590eef31a88a08fdffd
jsuhre
  • 181
  • 4
4

Integer.toHexString(0XFF & i) must be replaced with String.format("%02x", 0XFF & i), otherwise it only produces a 1 character output, while 2 characters are expected.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
Maciej M.
  • 41
  • 1