In the open platform, such as Application API key and secret often need to use the APK file signature, then how to read it?
The following paste the relevant reading source:
Altogether two documents Mainactivity and MD5,
Package Com.lcg.gensinature;import Android.app.activity;import Android.app.alertdialog;import Android.content.context;import Android.content.dialoginterface;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.content.pm.signature;import Android.os.bundle;import Android.text.clipboardmanager;import Android.text.editable;import Android.text.textutils;import Android.util.log;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends activity{private Button MCOPYBTN; Private Button mgenbtn; Private EditText Mpackageet; Private TextView Md5signtv; public static byte[] Getsign (Context paramcontext, String paramstring) {PackageInfo localpackageinfo; int i; Byte[] Arrayofbyte; try {LocalpAckageinfo = Paramcontext.getpackagemanager (). Getpackageinfo (paramstring, 64); i = 0; if (i >= localPackageInfo.signatures.length) {return null; } System.out.println (i); Arrayofbyte = Localpackageinfo.signatures[i].tobytearray (); System.out.println (Arrayofbyte); return arrayofbyte; } catch (Packagemanager.namenotfoundexception localnamenotfoundexception) {return null; }} protected void OnCreate (Bundle parambundle) {super.oncreate (parambundle); Setcontentview (R.layout.activity_main); This.mpackageet = ((EditText) Findviewbyid (R.id.pkgnameet)); THIS.MGENBTN = (Button) Findviewbyid (R.ID.GETBTN)); This.mGenBtn.setOnClickListener (New View.onclicklistener () {public void OnClick (View paramview) {Str ing str1 = MainActivity.this.mPackageEt.getText (). toString (); if (Textutils.isempty (str1)); Byte[] Arrayofbyte; Arrayofbyte = Mainactivity.getsign (MainActivity.this.getApplicationContext (), str1); String str2 = ""; try {str2 = Md5.hexdigest (arrayofbyte); LOG.D ("Gensinature", "Gen md5=" + str2); System.out.println (STR2); MainActivity.this.md5SignTv.setText (STR2); Clipboardmanager CMB = (Clipboardmanager) MainActivity.this.getSystemService (MainActivity.this.CLIPBOARD_SERVICE); Cmb.settext (STR2); Toast.maketext (Mainactivity.this, "copied to clipboard", 1). Show (); Return } catch (Exception e) {e.printstacktrace (); } } }); This.md5signtv = ((TextView) Findviewbyid (R.ID.RESULTTV)); ShowDialog (); private void ShowDialog () {Alertdialog.builder localbuilder = new Alertdialog.builder (this); Localbuilder.setcancelable (FALSE); Localbuilder.settitle ("Warning?"). Setmessage ("will read app information"); Localbuilder.setpositivebutton ("reject", null); LocalBuilder.setnegativebutton ("Consent", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface paramd ialoginterface, int paramint) {MainActivity.this.finish (); } }); Localbuilder.show (); }}
Package Com.lcg.gensinature;import Java.security.messagedigest;public class md5{private static final char[] Hexdigits = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; public static string Hexdigest (String paramstring) {try {string str = Hexdigest (Paramstring.getbytes ()); return str; } catch (Exception localexception) {} return null; } public static String hexdigest (byte[] paramarrayofbyte) {try {messagedigest localmessagedigest = messaged Igest.getinstance ("MD5"); System.out.println (Paramarrayofbyte); Localmessagedigest.update (Paramarrayofbyte); byte[] Arrayofbyte = Localmessagedigest.digest (); System.out.println (Arrayofbyte); char[] Arrayofchar = new CHAR[32]; int i = 0; int j = 0; for (i=0;i<16;i++) {int k = arrayofbyte[i]; int L = j + 1; ARRAYOFCHAR[J] = hexdigits[(0xF & K >>> 4)]; j = L + 1; ARRAYOFCHAR[L] = Hexdigits[(K & 0xF)]; } if (I >=) return new String (Arrayofchar); } catch (Exception e) {e.printstacktrace (); } return null; }}
For more information, please visit http://www.leegtang.com
How to read the Android apk file signature