3

To get a Java SHA256 hash I use the following method:

public String getSha256(String text, String encoding){
    String sha = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        md.update(text.getBytes(encoding));
        byte[] digest = md.digest();
        sha = new String(digest);
        sha = sha.replace("\n", "");
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sha;
}

Like so:

String testValue = getSha256("test", "UTF-8");

Then to get the HEX value out of it I do the following:

public String getHexFromString(String text){
    String hex = "";
    try {
        byte[] myBytes = text.getBytes("UTF-8");
        hex = DatatypeConverter.printHexBinary(myBytes);
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hex;
}

System.out.print(getHexFromString(testValue));

The result of this is:

C5B8E280A0C390EFBFBDCB864C7D65C5A12FC3AAC2A0C3855AC39015C2A3C2BF4F1B2B0BE2809A2CC3915D6C15C2B0C3B008

In javascript I do the following (using this library):

var hash = sjcl.hash.sha256.hash("test");
console.log(sjcl.codec.hex.fromBits(hash));

And the result is:

9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

How can I get same hex in Java and Javascript?

What am I doing wrong, is it the Java or Javascript code?

2
  • 3
    sha = new String(digest); - don't do this. The digest is arbitrary binary data. You should convert digest to hex, rather than converting it into a string and then calling your getHexFromString code. That's almost certainly part of the problem. Commented Mar 12, 2017 at 22:46
  • @JonSkeet thanks, this solved the issue :) ... also thanks for answering my other questions longer ago, somehow you are the right guy at the right place, ill remember that :P Commented Mar 13, 2017 at 0:30

1 Answer 1

2

@JonSkeet was right, changed the method, it now looks like this:

public String getSha256Hex(String text, String encoding){
    String shaHex = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        md.update(text.getBytes(encoding));
        byte[] digest = md.digest();

        shaHex = DatatypeConverter.printHexBinary(digest);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return shaHex;
}

And the result is:

9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08

The only difference is that the Java hex is upper case, but thats a minor thing to solve.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.