I am admittedly flailing in the dark somewhat regarding solving this. Partly because it's not entirely clear to me what which things are supposed to be equal. The method MessageDigest.isEqual, which I am assuming I will need to use, takes two byte arrays. So I have been working with the assumption that I have to get an MD5 MessageDigest object, use it's digest() method, and then compare that with converting the 2nd argument (a string) to a byte array.
When I add some printlns to test values, I get
baos.digest = [B@4b1210ee
md5.getBytes = [B@4d7e1886
The first 4 characters are the same and it's the same number of characters total, so... it's a start? I usually look at the solution if I feel like I'm missing something minor that I will understand, but in this case I have a suspicion I won't understand the solution at all, so I haven't looked at it.
package com.codegym.task.task32.task3211;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
/*
Data integrity
*/
public class Solution
{
public static void main(String... args) throws Exception
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(new String("test string"));
oos.flush();
System.out.println(compareMD5(bos, "5a47d12a2e3f9fecf2d9ba1fd98152eb")); // true
}
public static boolean compareMD5(ByteArrayOutputStream byteArrayOutputStream, String md5) throws Exception
{
MessageDigest baos = MessageDigest.getInstance("MD5");
baos.update(byteArrayOutputStream.toByteArray());
return MessageDigest.isEqual(baos.digest(), md5.getBytes(StandardCharsets.UTF_8));
}
}