How to use PackageInfo.GET_SIGNING_CERTIFICATES in API 28?












1















The documentation of PackageManager.GET_SIGNATURES says "This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead".



Unfortunately it was not secure and was easily hacked.



How can you use the new "GET_SIGNING_CERTIFICATES" introduced with Android P?










share|improve this question

























  • "Unfortunately it was not secure and was easily hacked." Please elaborate.

    – Michael
    Aug 27 '18 at 15:22











  • Answers should be posted as answers, not as an edit to a question.

    – Michael
    Aug 27 '18 at 15:42
















1















The documentation of PackageManager.GET_SIGNATURES says "This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead".



Unfortunately it was not secure and was easily hacked.



How can you use the new "GET_SIGNING_CERTIFICATES" introduced with Android P?










share|improve this question

























  • "Unfortunately it was not secure and was easily hacked." Please elaborate.

    – Michael
    Aug 27 '18 at 15:22











  • Answers should be posted as answers, not as an edit to a question.

    – Michael
    Aug 27 '18 at 15:42














1












1








1








The documentation of PackageManager.GET_SIGNATURES says "This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead".



Unfortunately it was not secure and was easily hacked.



How can you use the new "GET_SIGNING_CERTIFICATES" introduced with Android P?










share|improve this question
















The documentation of PackageManager.GET_SIGNATURES says "This constant was deprecated in API level 28. Use GET_SIGNING_CERTIFICATES instead".



Unfortunately it was not secure and was easily hacked.



How can you use the new "GET_SIGNING_CERTIFICATES" introduced with Android P?







android signature






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 27 '18 at 16:00







Ettore

















asked Aug 27 '18 at 14:46









EttoreEttore

968




968













  • "Unfortunately it was not secure and was easily hacked." Please elaborate.

    – Michael
    Aug 27 '18 at 15:22











  • Answers should be posted as answers, not as an edit to a question.

    – Michael
    Aug 27 '18 at 15:42



















  • "Unfortunately it was not secure and was easily hacked." Please elaborate.

    – Michael
    Aug 27 '18 at 15:22











  • Answers should be posted as answers, not as an edit to a question.

    – Michael
    Aug 27 '18 at 15:42

















"Unfortunately it was not secure and was easily hacked." Please elaborate.

– Michael
Aug 27 '18 at 15:22





"Unfortunately it was not secure and was easily hacked." Please elaborate.

– Michael
Aug 27 '18 at 15:22













Answers should be posted as answers, not as an edit to a question.

– Michael
Aug 27 '18 at 15:42





Answers should be posted as answers, not as an edit to a question.

– Michael
Aug 27 '18 at 15:42












2 Answers
2






active

oldest

votes


















3














My solution is:



In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:



try {
if(Build.VERSION.SDK_INT >= 28) {
@SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
final Signature signatures = packageInfo.signingInfo.getApkContentsSigners();
final MessageDigest md = MessageDigest.getInstance("SHA");
for (Signature signature : signatures) {
md.update(signature.toByteArray());
final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
Log.d("Signature Base64", signatureBase64);
}
}
} catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
e.printStackTrace();
}


If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.






share|improve this answer































    3














    In API28 you should check for multipleSigners too.



    This function will do the job(It's in kotlin):



    fun getApplicationSignature(packageName: String = context.packageName): List<String> {
    val signatureList: List<String>
    try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    // New signature
    val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
    signatureList = if (sig.hasMultipleSigners()) {
    // Send all with apkContentsSigners
    sig.apkContentsSigners.map {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(it.toByteArray())
    bytesToHex(digest.digest())
    }
    } else {
    // Send one with signingCertificateHistory
    sig.signingCertificateHistory.map {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(it.toByteArray())
    bytesToHex(digest.digest())
    }
    }
    } else {
    val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
    signatureList = sig.map {
    val digest = MessageDigest.getInstance("SHA")
    digest.update(it.toByteArray())
    bytesToHex(digest.digest())
    }
    }

    return signatureList
    } catch (e: Exception) {
    // Handle error
    }
    return emptyList()
    }


    And byteToHex is:



    fun bytesToHex(bytes: ByteArray): String {
    val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
    val hexChars = CharArray(bytes.size * 2)
    var v: Int
    for (j in bytes.indices) {
    v = bytes[j].toInt() and 0xFF
    hexChars[j * 2] = hexArray[v.ushr(4)]
    hexChars[j * 2 + 1] = hexArray[v and 0x0F]
    }
    return String(hexChars)
    }


    This'll handle App signature in android 9 (or lower)






    share|improve this answer
























    • @Ettore Feel free to vote and accept if the answer was helpful to you.

      – Malv
      Jan 20 at 18:16











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52041805%2fhow-to-use-packageinfo-get-signing-certificates-in-api-28%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    My solution is:



    In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:



    try {
    if(Build.VERSION.SDK_INT >= 28) {
    @SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
    final Signature signatures = packageInfo.signingInfo.getApkContentsSigners();
    final MessageDigest md = MessageDigest.getInstance("SHA");
    for (Signature signature : signatures) {
    md.update(signature.toByteArray());
    final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
    Log.d("Signature Base64", signatureBase64);
    }
    }
    } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
    e.printStackTrace();
    }


    If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.






    share|improve this answer




























      3














      My solution is:



      In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:



      try {
      if(Build.VERSION.SDK_INT >= 28) {
      @SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
      final Signature signatures = packageInfo.signingInfo.getApkContentsSigners();
      final MessageDigest md = MessageDigest.getInstance("SHA");
      for (Signature signature : signatures) {
      md.update(signature.toByteArray());
      final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
      Log.d("Signature Base64", signatureBase64);
      }
      }
      } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
      e.printStackTrace();
      }


      If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.






      share|improve this answer


























        3












        3








        3







        My solution is:



        In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:



        try {
        if(Build.VERSION.SDK_INT >= 28) {
        @SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
        final Signature signatures = packageInfo.signingInfo.getApkContentsSigners();
        final MessageDigest md = MessageDigest.getInstance("SHA");
        for (Signature signature : signatures) {
        md.update(signature.toByteArray());
        final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
        Log.d("Signature Base64", signatureBase64);
        }
        }
        } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        }


        If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.






        share|improve this answer













        My solution is:



        In the gradle build set "compileSdkVersion 28" and "targetSdkVersion 28", now you can use this sample code:



        try {
        if(Build.VERSION.SDK_INT >= 28) {
        @SuppressLint("WrongConstant") final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);
        final Signature signatures = packageInfo.signingInfo.getApkContentsSigners();
        final MessageDigest md = MessageDigest.getInstance("SHA");
        for (Signature signature : signatures) {
        md.update(signature.toByteArray());
        final String signatureBase64 = new String(Base64.encode(md.digest(), Base64.DEFAULT));
        Log.d("Signature Base64", signatureBase64);
        }
        }
        } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        }


        If strangely Android Studio does not recognize the constant GET_SIGNING_CERTIFICATES you can use the @SuppressLint ("WrongConstant") annotation.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 27 '18 at 16:00









        EttoreEttore

        968




        968

























            3














            In API28 you should check for multipleSigners too.



            This function will do the job(It's in kotlin):



            fun getApplicationSignature(packageName: String = context.packageName): List<String> {
            val signatureList: List<String>
            try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // New signature
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
            signatureList = if (sig.hasMultipleSigners()) {
            // Send all with apkContentsSigners
            sig.apkContentsSigners.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            } else {
            // Send one with signingCertificateHistory
            sig.signingCertificateHistory.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }
            } else {
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
            signatureList = sig.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }

            return signatureList
            } catch (e: Exception) {
            // Handle error
            }
            return emptyList()
            }


            And byteToHex is:



            fun bytesToHex(bytes: ByteArray): String {
            val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
            val hexChars = CharArray(bytes.size * 2)
            var v: Int
            for (j in bytes.indices) {
            v = bytes[j].toInt() and 0xFF
            hexChars[j * 2] = hexArray[v.ushr(4)]
            hexChars[j * 2 + 1] = hexArray[v and 0x0F]
            }
            return String(hexChars)
            }


            This'll handle App signature in android 9 (or lower)






            share|improve this answer
























            • @Ettore Feel free to vote and accept if the answer was helpful to you.

              – Malv
              Jan 20 at 18:16
















            3














            In API28 you should check for multipleSigners too.



            This function will do the job(It's in kotlin):



            fun getApplicationSignature(packageName: String = context.packageName): List<String> {
            val signatureList: List<String>
            try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // New signature
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
            signatureList = if (sig.hasMultipleSigners()) {
            // Send all with apkContentsSigners
            sig.apkContentsSigners.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            } else {
            // Send one with signingCertificateHistory
            sig.signingCertificateHistory.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }
            } else {
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
            signatureList = sig.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }

            return signatureList
            } catch (e: Exception) {
            // Handle error
            }
            return emptyList()
            }


            And byteToHex is:



            fun bytesToHex(bytes: ByteArray): String {
            val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
            val hexChars = CharArray(bytes.size * 2)
            var v: Int
            for (j in bytes.indices) {
            v = bytes[j].toInt() and 0xFF
            hexChars[j * 2] = hexArray[v.ushr(4)]
            hexChars[j * 2 + 1] = hexArray[v and 0x0F]
            }
            return String(hexChars)
            }


            This'll handle App signature in android 9 (or lower)






            share|improve this answer
























            • @Ettore Feel free to vote and accept if the answer was helpful to you.

              – Malv
              Jan 20 at 18:16














            3












            3








            3







            In API28 you should check for multipleSigners too.



            This function will do the job(It's in kotlin):



            fun getApplicationSignature(packageName: String = context.packageName): List<String> {
            val signatureList: List<String>
            try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // New signature
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
            signatureList = if (sig.hasMultipleSigners()) {
            // Send all with apkContentsSigners
            sig.apkContentsSigners.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            } else {
            // Send one with signingCertificateHistory
            sig.signingCertificateHistory.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }
            } else {
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
            signatureList = sig.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }

            return signatureList
            } catch (e: Exception) {
            // Handle error
            }
            return emptyList()
            }


            And byteToHex is:



            fun bytesToHex(bytes: ByteArray): String {
            val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
            val hexChars = CharArray(bytes.size * 2)
            var v: Int
            for (j in bytes.indices) {
            v = bytes[j].toInt() and 0xFF
            hexChars[j * 2] = hexArray[v.ushr(4)]
            hexChars[j * 2 + 1] = hexArray[v and 0x0F]
            }
            return String(hexChars)
            }


            This'll handle App signature in android 9 (or lower)






            share|improve this answer













            In API28 you should check for multipleSigners too.



            This function will do the job(It's in kotlin):



            fun getApplicationSignature(packageName: String = context.packageName): List<String> {
            val signatureList: List<String>
            try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // New signature
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo
            signatureList = if (sig.hasMultipleSigners()) {
            // Send all with apkContentsSigners
            sig.apkContentsSigners.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            } else {
            // Send one with signingCertificateHistory
            sig.signingCertificateHistory.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }
            } else {
            val sig = context.packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES).signatures
            signatureList = sig.map {
            val digest = MessageDigest.getInstance("SHA")
            digest.update(it.toByteArray())
            bytesToHex(digest.digest())
            }
            }

            return signatureList
            } catch (e: Exception) {
            // Handle error
            }
            return emptyList()
            }


            And byteToHex is:



            fun bytesToHex(bytes: ByteArray): String {
            val hexArray = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
            val hexChars = CharArray(bytes.size * 2)
            var v: Int
            for (j in bytes.indices) {
            v = bytes[j].toInt() and 0xFF
            hexChars[j * 2] = hexArray[v.ushr(4)]
            hexChars[j * 2 + 1] = hexArray[v and 0x0F]
            }
            return String(hexChars)
            }


            This'll handle App signature in android 9 (or lower)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 7:31









            MalvMalv

            884716




            884716













            • @Ettore Feel free to vote and accept if the answer was helpful to you.

              – Malv
              Jan 20 at 18:16



















            • @Ettore Feel free to vote and accept if the answer was helpful to you.

              – Malv
              Jan 20 at 18:16

















            @Ettore Feel free to vote and accept if the answer was helpful to you.

            – Malv
            Jan 20 at 18:16





            @Ettore Feel free to vote and accept if the answer was helpful to you.

            – Malv
            Jan 20 at 18:16


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52041805%2fhow-to-use-packageinfo-get-signing-certificates-in-api-28%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory