mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-08 03:24:04 +08:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user