Cannot output correct hash in Java. What is wrong?

前端 未结 3 1082
忘掉有多难
忘掉有多难 2021-02-10 21:00

In my Android app I have a SHA256 hash which I must further hash with the RIPEMD160 message digest algorithm.

I can output the correct sha256 and ripemd160 hash of any s

3条回答
  •  野的像风
    2021-02-10 21:29

    Your hash is working fine. The problem is that the online calculators that you're using are treating your input:

    9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
    

    as a string instead of an array of bytes. In other words, it's treating each character as a byte instead of parsing character pairs as bytes in hexadecimal. If I give this as a string to online calculators, I indeed get exactly what you got:

    4efc1c36d3349189fb3486d2914f56e05d3e66f8
    

    However, you're treating the output as an array of bytes instead of a String and that's giving you different results. You should encode your raw SHA256 hash as a string, then pass the encoded string to the hash function. I see you have a getHexString method, so we'll just use that.

    public static String toRIPEMD160(String in) {
        try {
            byte[] addr = in.getBytes();
            byte[] out = new byte[20];
            RIPEMD160Digest digest = new RIPEMD160Digest();
    
            // These are the lines that changed
            byte[] rawSha256 = sha256(addr);
            String encodedSha256 = getHexString(rawSha256);
            byte[] strBytes = encodedSha256.getBytes("UTF-8");
            digest.update(strBytes, 0, strBytes.length);
    
            digest.doFinal(out, 0);
            return getHexString(out);
        } catch (UnsupportedEncodingException ex) {
            // Never happens, everything supports UTF-8
            return null;
        }
    }
    

    If you want to know it's working, take the value of encodedSha256 and put that into an online hash calculator. As long as the calculator uses UTF-8 encoding to turn the string into a byte array, it will match your output.

提交回复
热议问题