How to generate a checksum for an java object

后端 未结 10 802
生来不讨喜
生来不讨喜 2020-12-13 01:04

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.

10条回答
  •  Happy的楠姐
    2020-12-13 01:48

    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.

提交回复
热议问题