Skip to content

Support get android app signatures. #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-native-version-check/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 26
def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "26.0.3"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_MIN_SDK_VERSION = 16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.os.Build;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import java.security.MessageDigest;

import javax.annotation.Nullable;

Expand All @@ -25,6 +32,36 @@ public RNVersionCheckModule(ReactApplicationContext reactContext)
this.packageName = this.reactContext.getPackageName();
}

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}

public static String signatureToSHAHex(Signature sig) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA");
digest.update(sig.toByteArray());
return bytesToHex(digest.digest());
}
catch (Exception e) {
return null;
}
}

public static List<String> signaturesToSHAHexList(Signature[] signatures) {
List<String> signatureList = new ArrayList<String>();
for (Signature sig : signatures) {
signatureList.add(signatureToSHAHex(sig));
}
return signatureList;
}

@Override
public @Nullable Map<String, Object> getConstants()
{
Expand All @@ -36,12 +73,38 @@ public RNVersionCheckModule(ReactApplicationContext reactContext)
PackageManager packageManager = this.reactContext.getPackageManager();
try
{
PackageInfo info = packageManager.getPackageInfo(packageName, 0);
boolean useNewAPI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
PackageInfo info = packageManager.getPackageInfo(packageName, useNewAPI ? PackageManager.GET_SIGNING_CERTIFICATES : PackageManager.GET_SIGNATURES);

List<String> signatureList;

if (useNewAPI)
{
// New signature
SigningInfo sig = info.signingInfo;
if (sig.hasMultipleSigners())
{
// Send all with apkContentsSigners
signatureList = signaturesToSHAHexList(sig.getApkContentsSigners());
}
else
{
// Send one with signingCertificateHistory
signatureList = signaturesToSHAHexList(sig.getSigningCertificateHistory());
}
}
else
{
signatureList = signaturesToSHAHexList(info.signatures);
}

constants.put("signatures", signatureList);
constants.put("currentVersion", info.versionName);
constants.put("currentBuildNumber", info.versionCode);
}
catch (PackageManager.NameNotFoundException e)
{
constants.put("signatures", null);
constants.put("currentVersion", null);
constants.put("currentBuildNumber", null);
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-version-check/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default (VersionInfoObject: IVersionInfo): ReactNativeVersionCheck => {
getPackageName: VersionInfoObject.getPackageName,
getCurrentBuildNumber: VersionInfoObject.getCurrentBuildNumber,
getCurrentVersion: VersionInfoObject.getCurrentVersion,
getAppSignatures: VersionInfoObject.getAppSignatures,

getStoreUrl,
getAppStoreUrl,
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-version-check/src/RNVersionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default {
getPackageName: (): string => RNVersionCheck.packageName,
getCurrentBuildNumber: (): number => RNVersionCheck.currentBuildNumber,
getCurrentVersion: (): string => RNVersionCheck.currentVersion,
getAppSignatures: (): string[] => RNVersionCheck.signatures,
};
1 change: 1 addition & 0 deletions packages/react-native-version-check/src/versionInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface IVersionInfo {
getPackageName: () => string;
getCurrentBuildNumber: () => number;
getCurrentVersion: () => string;
getAppSignatures: () => string[];
}

let VersionInfo: ?IVersionInfo = null;
Expand Down