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!
forloop in the java, it looks like it's rehashing the hash 64 times. Looks like you're only hashing once in javascript.Base64.encode(byte[],int)in Java does not match either thejava.utilone 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.