2

I'm trying to convert a hash function originally written in Java to Javascript in our codebase. However, I am getting different results. Below is the code in Java

public static String hashText(String str) {
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        messageDigest = null;
    }
    byte[] bytes = str.getBytes();
    for (int i = 0; i < 64; i++) {
        messageDigest.update(bytes);
        bytes = messageDigest.digest();
    }
    hashedText = new String(Base64.encode(bytes, 2));
    return hashedText.replace(StringUtils.LF, "");
}

And here is what I wrote in Javascript

function hashText(text){
  const crypto = require('crypto')
  const hash = crypto.createHash('sha512');
  const digest = hash.update(text).digest();
  return digest.toString("base64")
}
console.log(hashText(text))

I've been trying to figure out what I am doing wrong here but no success yet. I need help!

4
  • 3
    That for loop in the java, it looks like it's rehashing the hash 64 times. Looks like you're only hashing once in javascript. Commented Jun 9, 2021 at 22:35
  • Your Base64.encode(byte[],int) in Java does not match either the java.util one or Apache commons-codec, so if it does anything weird (I don't say nonstandard because there isn't a definite standard for base64), the nodejs version of base64 may not match it without help. Commented Jun 10, 2021 at 5:54
  • What is Java's "default character encoding" on your system? Some ISO-8859 variant? Some Windows code page? UTF-8? The results of 'str.getBytes()' depend on that default. Second point: it would help to provide some example inputs and expected/differing outputs. Commented Jun 10, 2021 at 9:47
  • @RalfKleberhoff the default is UTF-8. I've been able to solve it, and have posted the answer in case anyone else needs it. Thanks Commented Jun 10, 2021 at 12:07

2 Answers 2

2

That for loop in the java, it looks like it's rehashing the hash 64 times. Looks like you're only hashing once in JavaScript. Here's an example of how you might do that loop in node.js.

function hashText(text, iter = 1) {
  const crypto = require('crypto');
  let digest = text;
  for (let i = 0; i < iter; i++) {
    const hash = crypto.createHash('sha512');
    digest = hash.update(digest).digest();
  }
  return digest.toString('base64');
}
const text = 'asdf 1234 zxcv 5678';
console.log(hashText(text, 64));

*Edit: I don't use java, didn't know how MessageDigest.digest(), worked. Specifically:

... The digest is reset after this call is made.

In node, this means running createHash every iteration of the loop. Thanks @dave_thompson_085 for pointing this out!

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

3 Comments

You're right nodejs hashes can't be recycled like Java's, but doing the copy after update gives the wrong result. Make the loop body digest = hash.copy().update(digest).digest(); -- or digest = crypto.createHash('sha512').update(digest).digest(); with no pre-create.
@David784, I just tried your answer, it is not correct. I'm still getting different results
Thanks @dave_thompson_085, edited my answer based on your correction.
0

It turns out the way nodejs crypto hash copy works is kinda different. As stated in the docs, it creates a new Hash object that contains a deep copy of the internal state of the current Hash object. However, what I need is to create a new hash with the encoded string.

Below is a sample of a working snippet

function hashText(text, length) {
  const crypto = require('crypto');
  let digest = text;
  for (let i = 0; i < length; i++) {
    const hash = crypto.createHash('sha512');
    hash.update(digest);
    digest = hash.digest();
  }

  return digest.toString('base64');
}
console.log(hashText(text, 64));

Thanks to everyone that helped one way or the other

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.