2

I have this code on Nodejs I need to write similar to Java, but the results differ. I think that the problem is in hex encoding. But I do not understand how it works.

Nodejs code:

crypto.createHash('sha256').update(seed, 'hex').digest()

Java code:

digest = MessageDigest.getInstance("SHA-256");
byte[] encodedhash = digest.digest(seedString);
1
  • How does a typical seed lookalike (is it a string or binary data)? Commented May 19, 2018 at 17:30

2 Answers 2

6

These two codes will give you the same output

NodeJS

var data = "seed";
var crypto = require('crypto');
crypto.createHash('sha256').update(data).digest("hex");

Java

import java.security.MessageDigest;

public class SHAHashingExample 
{
    public static void main(String[] args)throws Exception
    {
        String password = "seed";

        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        System.out.println("Hex format : " + sb.toString());

        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<byteData.length;i++) {
            String hex=Integer.toHexString(0xff & byteData[i]);
            if(hex.length()==1) hexString.append('0');
            hexString.append(hex);
        }
        System.out.println("Hex format : " + hexString.toString());
    }
}

For more details:

NodeJS link
Java link

As pointed out by others it is a matter of how you present data. If in the update function you don't specify nothing - like in the solution I gave above - you are telling to interpret the seed as encoded with the default UTF-8 encoding. Now what's the translation of UTF-8 string seed in hex terms? the answer is 73656564, as you can easily check for example from this online tool

Now let's do a verification. Let's write:

NodeJS

var data = "73656564";
crypto.createHash('sha256').update(data, 'hex').digest('hex');

You will get the same result as well. You are telling to the update function that the data you are providing are an hex representation and must be interpreted as such

Hope this can help clarifying the role of hex

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

2 Comments

In the above example, everything works correctly, but my problem is in the second argument of the update function, namely encoding = hex. If you set the results will be different. And in this my problem I do not understand what does encoding = hex.
@Павел I improved my answer based on your comment, hope now everything is clearer
0

From nodejs documentation:

Updates the hash content with the given data, the encoding of which is given in inputEncoding and can be 'utf8', 'ascii' or 'latin1'. If encoding is not provided, and the data is a string, an encoding of 'utf8' is enforced. If data is a Buffer, TypedArray, or DataView, then inputEncoding is ignored.

In simple words it means format of data you are providing.

PS.

Code looks a bit wrong in update you would provide data not a seed.

1 Comment

The code on nodejs is a sample, it works correctly, the problem is with the code on java. I read the documentation and there is not a word about hex encoding.

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.