8
8
import android .content .pm .PackageInfo ;
9
9
import android .content .pm .PackageManager ;
10
10
import android .content .pm .Signature ;
11
+ import android .content .pm .SigningInfo ;
11
12
import android .graphics .drawable .Drawable ;
12
13
import android .net .Uri ;
13
14
import android .os .Build ;
@@ -498,8 +499,8 @@ public static int getAppVersionCode(final String packageName) {
498
499
*
499
500
* @return the application's signature
500
501
*/
501
- public static Signature [] getAppSignature () {
502
- return getAppSignature (Utils .getApp ().getPackageName ());
502
+ public static Signature [] getAppSignatures () {
503
+ return getAppSignatures (Utils .getApp ().getPackageName ());
503
504
}
504
505
505
506
/**
@@ -508,17 +509,26 @@ public static Signature[] getAppSignature() {
508
509
* @param packageName The name of the package.
509
510
* @return the application's signature
510
511
*/
511
- public static Signature [] getAppSignature (final String packageName ) {
512
+ public static Signature [] getAppSignatures (final String packageName ) {
512
513
if (UtilsBridge .isSpace (packageName )) return null ;
513
514
try {
514
515
PackageManager pm = Utils .getApp ().getPackageManager ();
515
- PackageInfo pi ;
516
516
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
517
- pi = pm .getPackageInfo (packageName , PackageManager .GET_SIGNING_CERTIFICATES );
517
+ PackageInfo pi = pm .getPackageInfo (packageName , PackageManager .GET_SIGNING_CERTIFICATES );
518
+ if (pi == null ) return null ;
519
+
520
+ SigningInfo signingInfo = pi .signingInfo ;
521
+ if (signingInfo .hasMultipleSigners ()) {
522
+ return signingInfo .getApkContentsSigners ();
523
+ } else {
524
+ return signingInfo .getSigningCertificateHistory ();
525
+ }
518
526
} else {
519
- pi = pm .getPackageInfo (packageName , PackageManager .GET_SIGNATURES );
527
+ PackageInfo pi = pm .getPackageInfo (packageName , PackageManager .GET_SIGNATURES );
528
+ if (pi == null ) return null ;
529
+
530
+ return pi .signatures ;
520
531
}
521
- return pi == null ? null : pi .signatures ;
522
532
} catch (PackageManager .NameNotFoundException e ) {
523
533
e .printStackTrace ();
524
534
return null ;
@@ -531,25 +541,34 @@ public static Signature[] getAppSignature(final String packageName) {
531
541
* @param file The file.
532
542
* @return the application's signature
533
543
*/
534
- public static Signature [] getAppSignature (final File file ) {
544
+ public static Signature [] getAppSignatures (final File file ) {
535
545
if (file == null ) return null ;
536
546
PackageManager pm = Utils .getApp ().getPackageManager ();
537
- PackageInfo pi ;
538
547
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
539
- pi = pm .getPackageArchiveInfo (file .getAbsolutePath (), PackageManager .GET_SIGNING_CERTIFICATES );
548
+ PackageInfo pi = pm .getPackageArchiveInfo (file .getAbsolutePath (), PackageManager .GET_SIGNING_CERTIFICATES );
549
+ if (pi == null ) return null ;
550
+
551
+ SigningInfo signingInfo = pi .signingInfo ;
552
+ if (signingInfo .hasMultipleSigners ()) {
553
+ return signingInfo .getApkContentsSigners ();
554
+ } else {
555
+ return signingInfo .getSigningCertificateHistory ();
556
+ }
540
557
} else {
541
- pi = pm .getPackageArchiveInfo (file .getAbsolutePath (), PackageManager .GET_SIGNATURES );
558
+ PackageInfo pi = pm .getPackageArchiveInfo (file .getAbsolutePath (), PackageManager .GET_SIGNATURES );
559
+ if (pi == null ) return null ;
560
+
561
+ return pi .signatures ;
542
562
}
543
- return pi == null ? null : pi .signatures ;
544
563
}
545
564
546
565
/**
547
566
* Return the application's signature for SHA1 value.
548
567
*
549
568
* @return the application's signature for SHA1 value
550
569
*/
551
- public static String getAppSignatureSHA1 () {
552
- return getAppSignatureSHA1 (Utils .getApp ().getPackageName ());
570
+ public static List < String > getAppSignaturesSHA1 () {
571
+ return getAppSignaturesSHA1 (Utils .getApp ().getPackageName ());
553
572
}
554
573
555
574
/**
@@ -558,17 +577,17 @@ public static String getAppSignatureSHA1() {
558
577
* @param packageName The name of the package.
559
578
* @return the application's signature for SHA1 value
560
579
*/
561
- public static String getAppSignatureSHA1 (final String packageName ) {
562
- return getAppSignatureHash (packageName , "SHA1" );
580
+ public static List < String > getAppSignaturesSHA1 (final String packageName ) {
581
+ return getAppSignaturesHash (packageName , "SHA1" );
563
582
}
564
583
565
584
/**
566
585
* Return the application's signature for SHA256 value.
567
586
*
568
587
* @return the application's signature for SHA256 value
569
588
*/
570
- public static String getAppSignatureSHA256 () {
571
- return getAppSignatureSHA256 (Utils .getApp ().getPackageName ());
589
+ public static List < String > getAppSignaturesSHA256 () {
590
+ return getAppSignaturesSHA256 (Utils .getApp ().getPackageName ());
572
591
}
573
592
574
593
/**
@@ -577,17 +596,17 @@ public static String getAppSignatureSHA256() {
577
596
* @param packageName The name of the package.
578
597
* @return the application's signature for SHA256 value
579
598
*/
580
- public static String getAppSignatureSHA256 (final String packageName ) {
581
- return getAppSignatureHash (packageName , "SHA256" );
599
+ public static List < String > getAppSignaturesSHA256 (final String packageName ) {
600
+ return getAppSignaturesHash (packageName , "SHA256" );
582
601
}
583
602
584
603
/**
585
604
* Return the application's signature for MD5 value.
586
605
*
587
606
* @return the application's signature for MD5 value
588
607
*/
589
- public static String getAppSignatureMD5 () {
590
- return getAppSignatureMD5 (Utils .getApp ().getPackageName ());
608
+ public static List < String > getAppSignaturesMD5 () {
609
+ return getAppSignaturesMD5 (Utils .getApp ().getPackageName ());
591
610
}
592
611
593
612
/**
@@ -596,11 +615,10 @@ public static String getAppSignatureMD5() {
596
615
* @param packageName The name of the package.
597
616
* @return the application's signature for MD5 value
598
617
*/
599
- public static String getAppSignatureMD5 (final String packageName ) {
600
- return getAppSignatureHash (packageName , "MD5" );
618
+ public static List < String > getAppSignaturesMD5 (final String packageName ) {
619
+ return getAppSignaturesHash (packageName , "MD5" );
601
620
}
602
621
603
-
604
622
/**
605
623
* Return the application's user-ID.
606
624
*
@@ -625,12 +643,17 @@ public static int getAppUid(String pkgName) {
625
643
}
626
644
}
627
645
628
- private static String getAppSignatureHash (final String packageName , final String algorithm ) {
629
- if (UtilsBridge .isSpace (packageName )) return "" ;
630
- Signature [] signature = getAppSignature (packageName );
631
- if (signature == null || signature .length <= 0 ) return "" ;
632
- return UtilsBridge .bytes2HexString (UtilsBridge .hashTemplate (signature [0 ].toByteArray (), algorithm ))
633
- .replaceAll ("(?<=[0-9A-F]{2})[0-9A-F]{2}" , ":$0" );
646
+ private static List <String > getAppSignaturesHash (final String packageName , final String algorithm ) {
647
+ ArrayList <String > result = new ArrayList <>();
648
+ if (UtilsBridge .isSpace (packageName )) return result ;
649
+ Signature [] signatures = getAppSignatures (packageName );
650
+ if (signatures == null || signatures .length <= 0 ) return result ;
651
+ for (Signature signature : signatures ) {
652
+ String hash = UtilsBridge .bytes2HexString (UtilsBridge .hashTemplate (signature .toByteArray (), algorithm ))
653
+ .replaceAll ("(?<=[0-9A-F]{2})[0-9A-F]{2}" , ":$0" );
654
+ result .add (hash );
655
+ }
656
+ return result ;
634
657
}
635
658
636
659
/**
0 commit comments