Matching Java SHA2 Output vs MySQL SHA2 Output

前端 未结 2 1306
一个人的身影
一个人的身影 2021-02-11 09:42

When I reproduce a SHA2 hash via the following code:

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


        
相关标签:
2条回答
  • 2021-02-11 09:55

    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
    
    0 讨论(0)
  • 2021-02-11 10:20

    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.

    0 讨论(0)
提交回复
热议问题