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?
bytearray in Base64. So in node you'd have to decode this base64 string into probably anArrayBuffer