Android Open Source - PicSync Package Security






From Project

Back to project page PicSync.

License

The source code is released under:

Apache License

If you think the Android project PicSync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.darrenmowat.gdcu.security;
//w  ww. j ava 2s  .c o  m
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

public class PackageSecurity {

  /**
   * This method will check that the package name & singature of this apk are valid
   * 
   * If they are not a PackageSecurityException is thrown. The provided userIdentifier will be included as part
   * of the exception message. 
   * 
   * You can catch the exception and so as you please or allow it to be caught and logged by whatever exception tracker you are using.
   * 
   * @param context - A reference to Android Context
   * @param packageName - The name you expect the package to be
   * @param packageSignature - One of the signature you expect the package to be signed with
   * @param userIdentifier - A way to identify this user. This will be included in the PackageSecurityException thrown
   * @throws PackageSecurityException
   */
  public static void checkPackageCertificate(Context context, String packageName, String packageSignature, String userIdentifier) throws PackageSecurityException {
    if (!context.getPackageName().equals(packageName)) {
      throw new PackageSecurityException("Package has been renamed to " + context.getPackageName() + " " + userIdentifier, userIdentifier);
    }
    PackageInfo packageInfo;
    try {
      packageInfo = context.getPackageManager().getPackageInfo(packageName,
          PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
      throw new PackageSecurityException("Package name not found - " + packageName + " " + userIdentifier, userIdentifier);
    }
    boolean signed = false;
    for (Signature signature : packageInfo.signatures) {
      if (signature.toCharsString().equals(packageSignature)) {
        signed = true;
      }
    }
    if (!signed) {
      throw new PackageSecurityException("Package is not signed with correct key" + " " + userIdentifier, userIdentifier);
    }
  }
}




Java Source Code List

com.darrenmowat.gdcu.GDCU.java
com.darrenmowat.gdcu.Keys.java
com.darrenmowat.gdcu.activity.AuthActivity.java
com.darrenmowat.gdcu.activity.RenameFolderActivity.java
com.darrenmowat.gdcu.activity.SettingsActivity.java
com.darrenmowat.gdcu.activity.WebviewActivity.java
com.darrenmowat.gdcu.data.Database.java
com.darrenmowat.gdcu.data.Preferences.java
com.darrenmowat.gdcu.drive.DriveApi.java
com.darrenmowat.gdcu.otto.DataBus.java
com.darrenmowat.gdcu.otto.UploadStatusEventProducer.java
com.darrenmowat.gdcu.otto.UploadStatusEvent.java
com.darrenmowat.gdcu.security.PackageSecurityException.java
com.darrenmowat.gdcu.security.PackageSecurity.java
com.darrenmowat.gdcu.service.GalleryAlarmListener.java
com.darrenmowat.gdcu.service.GalleryObserver.java
com.darrenmowat.gdcu.service.MediaService.java
com.darrenmowat.gdcu.service.MediaThread.java
com.darrenmowat.gdcu.service.Notifier.java
com.darrenmowat.gdcu.service.ServiceUtils.java
com.darrenmowat.gdcu.service.UploadService.java
com.darrenmowat.gdcu.service.UploadThread.java
com.darrenmowat.gdcu.service.helpers.CloudSyncer.java
com.darrenmowat.gdcu.service.helpers.Media.java
com.darrenmowat.gdcu.service.helpers.ServiceIntents.java
com.darrenmowat.gdcu.service.helpers.ThreadCallbacks.java
com.darrenmowat.gdcu.tasks.GetTokenTask.java
com.darrenmowat.gdcu.tasks.RenameFolderTask.java
com.darrenmowat.gdcu.ui.ProgressDialogFragment.java
com.darrenmowat.gdcu.utils.HexConversions.java
com.darrenmowat.gdcu.utils.MD5Utils.java
com.darrenmowat.gdcu.utils.Preconditions.java