如何为java对象生成校验和 [英] How to generate a checksum for an java object

查看:215
本文介绍了如何为java对象生成校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案来为任何类型的Java对象生成校验和,对于生成相同对象的应用程序的每次执行,它都保持相同。



<我用 <$尝试了它c $ c> Object.hashCode() ,但是api说


... 。从应用程序的一次执行到同一应用程序的另一次执行,这个整数不需要保持一致。



解决方案

我有类似的问题(为XML文件生成好的哈希码)我发现了最好的解决方案是通过 MessageDigest 使用MD5或者如果你需要更快的东西: Fast MD5 。请注意,即使 Object.hashCode 在每次太短(仅32位)时都是相同的,以确保高唯一性。我认为64位是计算良好哈希码的最小值。请注意,MD5生成128位长的哈希码,在这种情况下应该更多。



当然要使用 MessageDigest 你需要首先序列化(在你的情况下是马歇尔)对象。


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.

解决方案

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.

这篇关于如何为java对象生成校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆