Java Code Examples for android.content.pm.Signature#toByteArray()

The following examples show how to use android.content.pm.Signature#toByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppToolUtils.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制)
 * @return
 */
private static String getSignatureString(Signature sig, String type) {
    byte[] hexBytes = sig.toByteArray();
    String fingerprint = "error!";
    try {
        MessageDigest digest = MessageDigest.getInstance(type);
        if (digest != null) {
            byte[] digestBytes = digest.digest(hexBytes);
            StringBuilder sb = new StringBuilder();
            for (byte digestByte : digestBytes) {
                sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
            }
            fingerprint = sb.toString();
        }
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return fingerprint;
}
 
Example 2
Source File: HwPushManager.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制)
 *
 * @param sig
 * @param type
 *
 * @return
 */
public static String getSignatureString(Signature sig, String type)
{
    byte[] hexBytes = sig.toByteArray();
    String fingerprint = "error!";
    try
    {
        MessageDigest digest = MessageDigest.getInstance(type);
        if (digest != null)
        {
            byte[] digestBytes = digest.digest(hexBytes);
            StringBuilder sb = new StringBuilder();
            for (byte digestByte : digestBytes)
            {
                sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
            }
            fingerprint = sb.toString();
        }
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }

    return fingerprint;
}
 
Example 3
Source File: Util.java    From android-perftracking with MIT License 6 votes vote down vote up
/**
 * Check if the application is debuggable.
 *
 * <p>This method check if the application is signed by a debug key.
 * https://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build
 *
 * @param context application context
 * @return true if app is debuggable, false otherwise
 */
@RestrictTo(LIBRARY)
@VisibleForTesting(otherwise = PACKAGE_PRIVATE)
static boolean isAppDebuggable(@NonNull final Context context) {
  try {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo =
        packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
    Signature[] signatures = packageInfo.signatures;

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

    for (Signature signature : signatures) {
      ByteArrayInputStream stream = new ByteArrayInputStream(signature.toByteArray());
      X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
      String principal = cert.getSubjectX500Principal().toString().toUpperCase();
      return principal.contains("C=US")
          && principal.contains("O=ANDROID")
          && principal.contains("CN=ANDROID DEBUG");
    }
  } catch (Exception e) {
    // Things went south, anyway the app is not debuggable.
  }
  return false;
}
 
Example 4
Source File: AppUtils.java    From SmartChart with Apache License 2.0 6 votes vote down vote up
/**
 * 检测当前应用是否是Debug版本
 *
 * @param ctx
 * @return
 */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
        for (Signature signature : signatures) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signature.toByteArray());
            X509Certificate cert = (X509Certificate) cf
                    .generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable)
                break;
        }

    } catch (NameNotFoundException | CertificateException e) {
    }
    return debuggable;
}
 
Example 5
Source File: AppSigning.java    From Aria with Apache License 2.0 6 votes vote down vote up
/**
 * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制)
 */
public static String getSignatureString(Signature sig, String type) {
  byte[] hexBytes = sig.toByteArray();
  String fingerprint = "error!";
  try {
    MessageDigest digest = MessageDigest.getInstance(type);
    if (digest != null) {
      byte[] digestBytes = digest.digest(hexBytes);
      StringBuilder sb = new StringBuilder();
      for (byte digestByte : digestBytes) {
        sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
      }
      fingerprint = sb.toString();
    }
  } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
  }

  return fingerprint;
}
 
Example 6
Source File: AppUtils.java    From Aria with Apache License 2.0 6 votes vote down vote up
/**
 * 获取相应的类型的字符串(把签名的byte[]信息转换成16进制)
 */
public static String getSignatureString(Signature sig, String type) {
  byte[] hexBytes = sig.toByteArray();
  String fingerprint = "error!";
  try {
    MessageDigest digest = MessageDigest.getInstance(type);
    if (digest != null) {
      byte[] digestBytes = digest.digest(hexBytes);
      StringBuilder sb = new StringBuilder();
      for (byte digestByte : digestBytes) {
        sb.append((Integer.toHexString((digestByte & 0xFF) | 0x100)).substring(1, 3));
      }
      fingerprint = sb.toString();
    }
  } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
  }

  return fingerprint;
}
 
Example 7
Source File: Util.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
public static boolean validateAppSignature(Context context) {
	try {
		// get the signature form the package manager
		PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
		Signature[] appSignatures = packageInfo.signatures;
		// this sample only checks the first certificate
		for (Signature signature : appSignatures) {
			byte[] signatureBytes = signature.toByteArray();
			// calc sha1 in hex
			String currentSignature = calcSHA1(signatureBytes);
			// compare signatures
			Log.i("ValidateSigningCert", "Signature is " + currentSignature + " (" + CERTIFICATE_SHA1 + ")");
			return CERTIFICATE_SHA1.equalsIgnoreCase(currentSignature);
		}
	} catch (Exception e) { // if error assume failed to validate
		return false;
	}

	return false;
}
 
Example 8
Source File: SignaturesUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取证书对象
 * @param signature {@link Signature}
 * @return {@link X509Certificate}
 */
public static X509Certificate getX509Certificate(final Signature signature) {
    if (signature != null) {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream bais = new ByteArrayInputStream(signature.toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(bais);
            return cert;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "getX509Certificate");
        }
    }
    return null;
}
 
Example 9
Source File: SysUtils.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public static byte[] getSignature(Context context) {
    PackageInfo packageInfo = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
        L.e(e);
    }
    if (packageInfo == null || packageInfo.signatures == null)
        return null;
    Signature signature = packageInfo.signatures[0];
    return signature.toByteArray();
}
 
Example 10
Source File: CompleteHelper.java    From MobileInfo with Apache License 2.0 5 votes vote down vote up
private static void getSign(Context context, JSONObject jsonObject) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        Signature[] signs = packageInfo.signatures;
        Signature sign = signs[0];
        byte[] signBytes = sign.toByteArray();
        jsonObject.put("signMD5", getMD5(signBytes));
        jsonObject.put("signSHA1", getSHA1(signBytes));
    } catch (Exception e) {
        //e.printStackTrace();
    }
}
 
Example 11
Source File: GrpcClientModule.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
private static String signatureDigest(Signature sig) {
  byte[] signature = sig.toByteArray();
  try {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    byte[] digest = md.digest(signature);
    return BaseEncoding.base16().upperCase().encode(digest);
  } catch (NoSuchAlgorithmException e) {
    return null;
  }
}
 
Example 12
Source File: Utility.java    From letv with Apache License 2.0 5 votes vote down vote up
public static String getSign(Context context, String pkgName) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(pkgName, 64);
        for (Signature toByteArray : packageInfo.signatures) {
            byte[] str = toByteArray.toByteArray();
            if (str != null) {
                return MD5.hexdigest(str);
            }
        }
        return null;
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 13
Source File: SysUtils.java    From 920-text-editor-v2 with Apache License 2.0 5 votes vote down vote up
public static byte[] getSignature(Context context) {
    PackageInfo packageInfo = null;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
        L.e(e);
    }
    if (packageInfo == null || packageInfo.signatures == null)
        return null;
    Signature signature = packageInfo.signatures[0];
    return signature.toByteArray();
}
 
Example 14
Source File: AndroidUtils.java    From Android-Next with Apache License 2.0 5 votes vote down vote up
public static String getSignatureInfo(Context context) {
    final Signature signature = getPackageSignature(context);
    if (signature == null) {
        return "";
    }
    final StringBuilder builder = new StringBuilder();
    try {
        final byte[] signatureBytes = signature.toByteArray();
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        final InputStream is = new ByteArrayInputStream(signatureBytes);
        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(is);
        final String chars = signature.toCharsString();
        final String hex = CryptoUtils.HEX.encodeHex(signatureBytes, false);
        final String md5 = CryptoUtils.HASH.md5(signatureBytes);
        final String sha1 = CryptoUtils.HASH.sha1(signatureBytes);
        builder.append("SignName:").append(cert.getSigAlgName()).append("\n");
        builder.append("Chars:").append(chars).append("\n");
        builder.append("Hex:").append(hex).append("\n");
        builder.append("MD5:").append(md5).append("\n");
        builder.append("SHA1:").append(sha1).append("\n");
        builder.append("SignNumber:").append(cert.getSerialNumber()).append("\n");
        builder.append("SubjectDN:").append(cert.getSubjectDN().getName()).append("\n");
        builder.append("IssuerDN:").append(cert.getIssuerDN().getName()).append("\n");
    } catch (Exception e) {
        LogUtils.e(TAG, "parseSignature() ex=" + e);
    }

    final String text = builder.toString();

    if (DEBUG) {
        LogUtils.v(TAG, text);
    }

    return text;
}
 
Example 15
Source File: LatinIMESettings.java    From hackerskeyboard with Apache License 2.0 4 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    int autoTextSize = AutoText.getSize(getListView());
    if (autoTextSize < 1) {
        ((PreferenceGroup) findPreference(PREDICTION_SETTINGS_KEY))
                .removePreference(mQuickFixes);
    }
    
    Log.i(TAG, "compactModeEnabled=" + LatinIME.sKeyboardSettings.compactModeEnabled);
    if (!LatinIME.sKeyboardSettings.compactModeEnabled) {
        CharSequence[] oldEntries = mKeyboardModePortraitPreference.getEntries();
        CharSequence[] oldValues = mKeyboardModePortraitPreference.getEntryValues();
        
        if (oldEntries.length > 2) {
            CharSequence[] newEntries = new CharSequence[] { oldEntries[0], oldEntries[2] };
            CharSequence[] newValues = new CharSequence[] { oldValues[0], oldValues[2] };
            mKeyboardModePortraitPreference.setEntries(newEntries);
            mKeyboardModePortraitPreference.setEntryValues(newValues);
            mKeyboardModeLandscapePreference.setEntries(newEntries);
            mKeyboardModeLandscapePreference.setEntryValues(newValues);
        }
    }
    
    updateSummaries();

    String version = "";
    try {
        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
        version = info.versionName;
        boolean isOfficial = false;
        for (Signature sig : info.signatures) {
            byte[] b = sig.toByteArray();
            int out = 0;
            for (int i = 0; i < b.length; ++i) {
                int pos = i % 4;
                out ^= b[i] << (pos * 4);
            }
            if (out == -466825) {
                isOfficial = true;
            }
            //version += " [" + Integer.toHexString(out) + "]";
        }
        version += isOfficial ? " official" : " custom";
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Could not find version info.");
    }

    mLabelVersion.setSummary(version);
}