0

I have a JAVA piece of code as following :

public static String base64AndMD5(byte[] bytes) throws NoSuchAlgorithmException {
    final MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update(bytes);
    byte[] md5Result = md.digest();
    String base64Result = Base64.encodeBase64String(md5Result);
    return base64Result.length() > 24 ? base64Result.substring(0, 23) : base64Result;
}

I have zero experience with java and most of time work on Node JS. This function expects a byte array and then finds md5 of it and then base64 encodes. I was trying to implement it in NodeJs in the following way:

byteArray = [123, 10, 32, 32, 34, 117, 115, 101, 114, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 117, 115, 101, 114, 95, 105, 100, 34, 58, 32, 34, 49, 52, 55, 48, 57, 53, 57, 54, 48, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 112, 105, 110, 99, 111, 100, 101, 34, 58, 32, 34, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 108, 97, 116, 34, 58, 32, 34, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 108, 111, 110, 103, 34, 58, 34, 34, 10, 32, 32, 32, 125, 44, 10, 32, 32, 34, 100, 101, 118, 105, 99, 101, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 103, 97, 95, 105, 100, 34, 32, 58, 32, 34, 49, 50, 51, 34, 10, 32, 32, 125, 44, 10, 32, 32, 34, 99, 104, 97, 110, 110, 101, 108, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 110, 97, 109, 101, 34, 58, 32, 34, 97, 110, 100, 114, 111, 105, 100, 97, 112, 112, 34, 10, 32, 32, 125, 44, 10, 32, 32, 34, 99, 117, 115, 116, 111, 109, 34, 58, 32, 123, 10, 32, 32, 32, 34, 99, 117, 114, 114, 101, 110, 116, 95, 112, 97, 103, 101, 34, 32, 58, 32, 34, 104, 111, 109, 101, 112, 97, 103, 101, 34, 44, 10, 32, 32, 32, 34, 105, 100, 34, 58, 34, 50, 48, 49, 56, 57, 52, 55, 57, 57, 34, 10, 32, 32, 125, 10, 125];

let md5OfByteArray = crypto.createHash('md5').update(byteArray).digest("hex");

But it returns an error that byteArray should be a string or buffer. Now when I push execute java function with above byte array it returns, hKJQcQlv5e1RB5WyDZX3AA==. Can someone explain what these java functions are doing and how can we do the same in nodejs?

1
  • Java encodes the byte array in Base64. So in node you'd have to decode this base64 string into probably an ArrayBuffer Commented Mar 24, 2020 at 10:42

1 Answer 1

2

You need to use a buffer based on your bytes array. Then you can hash and encode to base64

function base64AndMD5(bytesArray) {

  const buf = Buffer.from(bytesArray); // bytesArray = [123, 10, 32, 32, 34, 117, 115, 101, 114, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 117, 115, 101, 114, 95, 105, 100, 34, 58, 32, 34, 49, 52, 55, 48, 57, 53, 57, 54, 48, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 112, 105, 110, 99, 111, 100, 101, 34, 58, 32, 34, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 108, 97, 116, 34, 58, 32, 34, 34, 44, 10, 32, 32, 32, 32, 32, 32, 34, 108, 111, 110, 103, 34, 58, 34, 34, 10, 32, 32, 32, 125, 44, 10, 32, 32, 34, 100, 101, 118, 105, 99, 101, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 103, 97, 95, 105, 100, 34, 32, 58, 32, 34, 49, 50, 51, 34, 10, 32, 32, 125, 44, 10, 32, 32, 34, 99, 104, 97, 110, 110, 101, 108, 34, 58, 32, 123, 10, 32, 32, 32, 32, 32, 32, 34, 110, 97, 109, 101, 34, 58, 32, 34, 97, 110, 100, 114, 111, 105, 100, 97, 112, 112, 34, 10, 32, 32, 125, 44, 10, 32, 32, 34, 99, 117, 115, 116, 111, 109, 34, 58, 32, 123, 10, 32, 32, 32, 34, 99, 117, 114, 114, 101, 110, 116, 95, 112, 97, 103, 101, 34, 32, 58, 32, 34, 104, 111, 109, 101, 112, 97, 103, 101, 34, 44, 10, 32, 32, 32, 34, 105, 100, 34, 58, 34, 50, 48, 49, 56, 57, 52, 55, 57, 57, 34, 10, 32, 32, 125, 10, 125]

  const hashed = crypto
                       .createHash('md5')
                       .update(buf)
                       .digest("base64"); // hKJQcQlv5e1RB5WyDZX3AA==

  return hashed.length > 24 ? hashed.slice(0, 23) : hashed;
  // this is weird, I would expect hashed.length >= 24 rather than > but anyways it respects your java code.
}
Sign up to request clarification or add additional context in comments.

3 Comments

Any idea how to do it in plain JS?
you mean in the browser without Node libs?
Yes. I'm okay using libs via CDN.

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.