How to generate a checksum for an java object

49,088

Solution 1

I had similar problem (generating good hashcode for XML files) and I found out that the best solution is to use MD5 through MessageDigest or in case you need something faster: Fast MD5. Please notice that even if Object.hashCode would be the same every time it is anyway too short (only 32 bits) to ensure high uniqueness. I think 64 bits is a minimum to compute good hash code. Please be aware that MD5 generates 128 bits long hash code, which should is even more that needed in this situation.

Of course to use MessageDigest you need serialize (in your case marshall) the object first.

Solution 2

public static String getChecksum(Serializable object) throws IOException, NoSuchAlgorithmException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(baos.toByteArray());
        return DatatypeConverter.printHexBinary(thedigest);
    } finally {
        oos.close();
        baos.close();
    }
}

Solution 3

Example

private BigInteger checksum(Object obj) throws IOException, NoSuchAlgorithmException {

    if (obj == null) {
      return BigInteger.ZERO;   
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.close();

    MessageDigest m = MessageDigest.getInstance("SHA1");
    m.update(baos.toByteArray());

    return new BigInteger(1, m.digest());
}

Solution 4

I think you should look at serialization. Serialization mechanism needs to solve similar problem, so you can look how it's implemented.

But if you describe the problem you're trying to solve you'll probably get more precise solution.

Solution 5

If you control the source, you can implement hashCode() so it will be consistent from one execution to another.

Share:
49,088
Alex
Author by

Alex

Updated on July 09, 2022

Comments

  • Alex
    Alex almost 2 years

    I'm looking for a solution to generate a checksum for any type of Java object, which remains the same for every execution of an application that produces the same object.

    I tried it with Object.hashCode(), but the api says

    ....This integer need not remain consistent from one execution of an application to another execution of the same application.

  • Alex
    Alex about 14 years
    thank you all for your answers. I calculate a MD5 from the marshalled request.