简体   繁体   中英

SHA-256 - mismatch between NodeJS and Java code

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);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM