Search code examples
j2objc

Why does SHA-256 message digest give different result in j2objc?


I'm getting a different result when running my code in Java and j2obc. My java code looks like this:

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class TestScrypt {

    public static void main(String[] args) throws NoSuchAlgorithmException, DigestException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(new byte[64]);
        byte[] tmp = md.digest();
        md.digest(tmp, 0, tmp.length);
        System.out.println("Result:" + Base64.getEncoder().encodeToString(tmp));
    }
}

When I run this on my 64-bit Mac with Java 1.8.0_152 I get this output: Result:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=

But when I compile using j2objc (and link using the -ObjC -ljre_emul flags) I get this output: Result:9aX9QtFqIDAnmO9u0wmXm0MAPSMg2fDo6pgxqSdZ+0s=

Am I doing something wrong?


Solution

  • You found a bug in j2objc's support for md.digest(tmp, 0, tmp.length): it returns an updated hash as md.digest(tmp), but should instead return an non-updated digest. I filed issue #929, which you can subscribe to to be notified when it's fixed.