Add support for APK signing certificate SHA-256 digest and detecting APK release type and add them to App Info reports

The `TermuxConstants` class has been updated to `v0.21.0`. Check its Changelog sections for info on changes.
This commit is contained in:
agnostic-apollo
2021-05-13 15:40:09 +05:00
parent 4629276500
commit 1e30022ce7
4 changed files with 107 additions and 4 deletions

View File

@@ -3,11 +3,15 @@ package com.termux.shared.packages;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import com.termux.shared.data.DataUtils;
import com.termux.shared.logger.Logger;
import java.security.MessageDigest;
public class PackageUtils {
/**
@@ -32,8 +36,19 @@ public class PackageUtils {
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
*/
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context) {
return getPackageInfoForPackage(context, 0);
}
/**
* Get the {@link PackageInfo} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @param flags The flags to pass to {@link PackageManager#getPackageInfo(String, int)}.
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
*/
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context, final int flags) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return context.getPackageManager().getPackageInfo(context.getPackageName(), flags);
} catch (final Exception e) {
return null;
}
@@ -107,4 +122,21 @@ public class PackageUtils {
}
}
/**
* Get the {@code SHA-256 digest} of signing certificate for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the{@code SHA-256 digest}. This will be {@code null} if an exception is raised.
*/
public static String getSigningCertificateSHA256DigestForPackage(@NonNull final Context context) {
try {
PackageInfo packageInfo = getPackageInfoForPackage(context, PackageManager.GET_SIGNATURES);
if (packageInfo == null) return null;
return DataUtils.bytesToHex(MessageDigest.getInstance("SHA-256").digest(packageInfo.signatures[0].toByteArray()));
} catch (final Exception e) {
return null;
}
}
}