is Signature.hashCode referring to the right hashCode?

China☆狼群 提交于 2020-01-05 09:07:16

问题


Does the following code (sign.hashCode()) gives me the hashCode of my signature or the hash of the object in the memory?

try {
    PackageInfo packageInfo = getPackageManager().getPackageInfo(
            "com.klxx.as", PackageManager.GET_SIGNATURES);
    Signature[] signs = packageInfo.signatures;
    Signature sign = signs[0];
    Log.i("test", "hashCode : "+sign.hashCode());
} catch (Exception e) {
    e.printStackTrace();
}

The documentation (here) only says the following which is like any other object.

a hash code value for this object.

But I have seen the above snippet in multiple websites claiming that it shows the sign of the apk. Also some other sources have used the bytes of signature to create the hash by themself.


回答1:


Signature.hashCode() is overwritten and in this case is calculated on the content of the signature byte array.

You can see the source code to solve your doubts http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/pm/Signature.java#Signature.hashCode%28%29




回答2:


It returns a hashCode of the signature object in memory. HashCode is a function on every Java object that returns an id used by HashMap, HashSet, etc to quickly differentiate objects. There is no definition of what that hash code should be, except that it should return different values for everything that should hash independently.



来源:https://stackoverflow.com/questions/49651834/is-signature-hashcode-referring-to-the-right-hashcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!