问题
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