Signature.hashCode是否指向正确哈希?sign.hashCode()返回签名还是内存对象哈希?
Android Signature.hashCode() Explained
Let's clear this up directly: when you call sign.hashCode() in your code, it returns a hash value derived from the actual signature data, not the object's memory address.
Here's the breakdown:
- By default,
Object.hashCode()returns a value tied to an object's memory identity, but Android'sSignatureclass explicitly overrides this method (along withequals()) to calculate the hash based on the raw signature bytes it encapsulates. - This design makes perfect sense for a class representing app signatures: two distinct
Signatureinstances that hold identical signature data should be treated as equal, and thus return the same hashCode—even if they're separate objects in memory.
If you want to verify this behavior yourself, you could compute a hash from the raw signature bytes (using sign.toByteArray()) and see that it aligns logically with the hashCode() result (note: hashCode() returns a 32-bit integer, while full signature hashes like SHA-256 are longer, but the core link to the signature content remains).
Your code for reference:
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(); }
内容的提问来源于stack exchange,提问作者Hossein Shahdoost
相关产品推荐
相关产品推荐

