Java gets the signature summary of the APK installed on the phone _java

Source: Internet
Author: User
Tags md5

When you publish apk to an application store or to a Third-party SDK, you sometimes need to provide APK signature summary information, a digest algorithm MD5 or SHA-1 to obtain a signature summary, in addition to obtaining your own APK signature, or obtaining other installed apk signatures on your phone, You only need to pass in the other APK package name.

private static final char[] Hex_char = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E
 
', ' F '};
 
        /** gets the signature MD5 summary/public string[] Signaturedigest () {pkgInfo = Mcontext.getpackagemanager (). Getpackageinfo (
 
  Mcontext.getpackagename (), packagemanager.get_signatures);
 
  int length = PkgInfo.signatures.length;
 
  String[] digests = new String[length];
 
    for (int i = 0; i < length; ++i) {Signature sign = mpkginfo.signatures[i];
 
      try {messagedigest MD5 = messagedigest.getinstance ("MD5"); Byte[] Digest = Md5.digest (Sign.tobytearray ());
 
    Get digest with MD5 algorithm digests[i] = tohexstring (digest);
 
      catch (NoSuchAlgorithmException e) {e.printstacktrace ();
 
    Digests[i] = null;
 
} return digests; /** converts a byte array to the corresponding hexadecimal string */private string tohexstring (byte[] rawbytearray) {char[] chars = new Char[rawbytearray
 
  . length * 2]; for (int i = 0; i < Rawbytearray.length;
 
    ++i) {byte b = rawbytearray[i];
 
    CHARS[I*2] = hex_char[(b >>> 4 & 0x0f)];
 
  Chars[i*2+1] = hex_char[(b & 0x0f)];
 
Return to New String (chars); }

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.