2020-2-8 seo達人
閑著沒事兒寫了個小 demo ,獲取手機上已安裝應用信息,系統應用和 非系統應用
MD5 SHA1 SHA256 簽名信息 點擊簽名信息可復制到剪切板,
GitHub:https://github.com/sunan-n/GetAppInfo
如下圖:
<span style="white-space:pre;"> </span>主要就是這個方法,傳參數進來獲取相應的簽名類型 信息<br />
public static String getSignaturesInfo(Context context, String packageName, String tpye) {<br />
// //獲取包管理器<br />
PackageManager pm = context.getPackageManager();<br />
//返回包括在包中的簽名信息<br />
int flags = PackageManager.GET_SIGNATURES;<br />
PackageInfo packageInfo = null;<br />
try {<br />
//獲得包的所有內容信息類<br />
packageInfo = pm.getPackageInfo(packageName, flags);<br />
} catch (PackageManager.NameNotFoundException e) {<br />
e.printStackTrace();<br />
}<br />
//簽名信息<br />
Signature[] signatures = packageInfo.signatures;<br />
byte[] cert = signatures[0].toByteArray();<br />
//將簽名轉換為字節數組流<br />
InputStream input = new ByteArrayInputStream(cert);<br />
//證書工廠類,這個類實現了出廠合格證算法的功能<br />
CertificateFactory cf = null;<br />
try {<br />
cf = CertificateFactory.getInstance("X509");<br />
} catch (CertificateException e) {<br />
e.printStackTrace();<br />
}<br />
//X509證書,X.509是一種非常通用的證書格式<br />
X509Certificate c = null;<br />
try {<br />
c = (X509Certificate) cf.generateCertificate(input);<br />
} catch (CertificateException e) {<br />
e.printStackTrace();<br />
}<br />
String hexString = null;<br />
try {<br />
//加密算法的類,這里的參數可以使MD4,MD5等加密算法<br />
// MessageDigest md = MessageDigest.getInstance("SHA1");<br />
MessageDigest md = MessageDigest.getInstance(tpye);<br />
//獲得公鑰<br />
byte[] publicKey = md.digest(c.getEncoded());<br />
//字節到十六進制的格式轉換<br />
hexString = byte2HexFormatted(publicKey);<br />
} catch (NoSuchAlgorithmException e1) {<br />
e1.printStackTrace();<br />
} catch (CertificateEncodingException e) {<br />
e.printStackTrace();<br />
}<br />
return hexString;<br />
}<br />
<br />