Search code examples
androidhashchecksumandroid-package-managers

What is the encoding format of the byte arrays returned by ApkChecksum's getValue()?


Starting from API level 31, android's package manager has added requestChecksums(), allowing various apk hashes to be retrieved. With this, we can obtain a List<ApkChecksum>; however, the developer reference for ApkChecksum very sparsely documents the ApkChecksum class. getValue() returns "Checksum value" as a byte array (byte[]). And it cannot be null.

But it doesn't say anything about the encoding/format of the bytes thus returned. As this SO post indicates, "Your byte array must have some encoding." and it points to https://docs.oracle.com/en/java/javase/15/intl/supported-encodings.html#GUID-F8900850-51B5-4B8B-88C0-6186DF1C16B5 for a list of dozens of basic encoding sets contained in just java.base module, not to mention many others that developers may have chosen to use. And those are just character encodings, whereas hashes returned by ApkChecksum may presumably used other encodings/formats for numbers or big numbers.

Does anyone know what is the encoding for the byte array returned by ApkChecksum's getValue()?


Solution

  • I referred to this page which uses String.format ("%02X", b) to print the Hex value into a string, and concatenated the output to form the hash. My code looks something like this:

                    val Checksum = checksum.value
                    var concatWhole = ""
                    for (a in Checksum) {
                        val st = String.format("%02X", a)
                        concatWhole += st
    

    Android is little-endian according to the documentation