編輯:關於Android編程
android 獲取apk md5值
1, 獲取 Signature
2. 根據Signature計算md5和sha1信息,獲取簽名公鑰信息
TextView text = null;
StringBuffer sb = new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
try {
PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
Signature signatures = pi.signatures[0];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(signatures.toByteArray());
byte[] digest = md.digest();
String res = toHexString(digest);
Log.e(TAG, "apk md5 = "+res);
sb.append("apk md5 = "+res);
MessageDigest md2 = MessageDigest.getInstance("SHA1");
md2.update(signatures.toByteArray());
byte[] digest2 = md.digest();
String res2 = toHexString(digest2);
Log.e(TAG, "apk SHA1 = "+res2);
sb.append("\napk SHA1 = "+res2);
ByteArrayInputStream bais = new ByteArrayInputStream(signatures.toByteArray());
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(bais);
String sigAlgName = cert.getSigAlgName();
String subjectDN = cert.getSubjectDN().toString();
Log.e(TAG, "sigAlgName = "+sigAlgName);
Log.e(TAG, "subjectDN = "+subjectDN);
sb.append("\n sigAlgName = "+sigAlgName);
sb.append("\n subjectDN = "+subjectDN);
bais.close();
} catch (NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
text.setText(sb.toString());
}
private void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
/*
* Converts a byte array to hex string
*/
private String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len-1) {
buf.append(":");
}
}
return buf.toString();
}
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/39480503,本文出自:【張鴻洋的博客】上一篇已經
最近有人問我如何實現倒計時的按鈕功能,例如發送驗證碼,我記得有個CountDownTimer,因為好久沒用過了,自己就寫了一個,代碼如下 new CountDownTim
Android結合版最近幾個版本在包大小配額上超標了,先後采用了包括圖片壓縮,功能H5,無用代碼移除等手段減包,還是有著很大的減包壓力。組內希望我能從代碼的角度減少一些包
一、前言那麼今天,我們繼續來看一篇關於Android中的UI篇,如何自定義視圖View的進階篇,關於前奏篇之前已經寫過了,在這篇文章中我主要介紹了自定義View的一些基礎