// Path: lib/src/main/java/com/google/capillary/AuthModeUnavailableException.java
// public final class AuthModeUnavailableException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link AuthModeUnavailableException} instance.
//    *
//    * @param msg the exception message.
//    */
//   public AuthModeUnavailableException(String msg) {
//     super(msg);
//   }
// }
// 
// Path: lib/src/main/java/com/google/capillary/NoSuchKeyException.java
// public final class NoSuchKeyException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link NoSuchKeyException} object.
//    *
//    * @param msg the exception message.
//    */
//   public NoSuchKeyException(String msg) {
//     super(msg);
//   }
// }


import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.VisibleForTesting;
import com.google.capillary.AuthModeUnavailableException;
import com.google.capillary.NoSuchKeyException;
import com.google.capillary.internal.CapillaryPublicKey;
import com.google.crypto.tink.HybridDecrypt;
import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException;
import java.util.UUID;
   * @throws GeneralSecurityException if the key generation fails.
   */
  public synchronized void generateKeyPair(boolean isAuth)
      throws AuthModeUnavailableException, GeneralSecurityException {
    int currentSerialNumber = sharedPreferences.getInt(toSerialNumberPrefKey(isAuth), -1);
    generateKeyPair(currentSerialNumber + 1, isAuth);
  }

  /**
   * Generates a new Capillary key pair with the given key serial number.
   */
  synchronized boolean generateKeyPair(int newSerialNumber, boolean isAuth)
      throws AuthModeUnavailableException, GeneralSecurityException {
    if (isAuth) {
      utils.checkAuthModeIsAvailable(context);
    }
    int currentSerialNumber = sharedPreferences.getInt(toSerialNumberPrefKey(isAuth), -1);
    // If the requested serial number is older, do not generate the keys.
    if (newSerialNumber <= currentSerialNumber) {
      return false;
    }
    rawGenerateKeyPair(isAuth);
    sharedPreferences.edit().putInt(toSerialNumberPrefKey(isAuth), newSerialNumber).apply();
    return true;
  }

  static String toKeyTypeString(boolean isAuth) {
    return isAuth ? "Auth" : "NoAuth";
  }

// Path: lib/src/main/java/com/google/capillary/AuthModeUnavailableException.java
// public final class AuthModeUnavailableException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link AuthModeUnavailableException} instance.
//    *
//    * @param msg the exception message.
//    */
//   public AuthModeUnavailableException(String msg) {
//     super(msg);
//   }
// }
// 
// Path: lib/src/main/java/com/google/capillary/NoSuchKeyException.java
// public final class NoSuchKeyException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link NoSuchKeyException} object.
//    *
//    * @param msg the exception message.
//    */
//   public NoSuchKeyException(String msg) {
//     super(msg);
//   }
// }


// Path: lib-android/src/main/java/com/google/capillary/android/KeyManager.java
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.VisibleForTesting;
import com.google.capillary.AuthModeUnavailableException;
import com.google.capillary.NoSuchKeyException;
import com.google.capillary.internal.CapillaryPublicKey;
import com.google.crypto.tink.HybridDecrypt;
import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException;
import java.util.UUID;

   * @throws GeneralSecurityException if the key generation fails.
   */
  public synchronized void generateKeyPair(boolean isAuth)
      throws AuthModeUnavailableException, GeneralSecurityException {
    int currentSerialNumber = sharedPreferences.getInt(toSerialNumberPrefKey(isAuth), -1);
    generateKeyPair(currentSerialNumber + 1, isAuth);
  }

  /**
   * Generates a new Capillary key pair with the given key serial number.
   */
  synchronized boolean generateKeyPair(int newSerialNumber, boolean isAuth)
      throws AuthModeUnavailableException, GeneralSecurityException {
    if (isAuth) {
      utils.checkAuthModeIsAvailable(context);
    }
    int currentSerialNumber = sharedPreferences.getInt(toSerialNumberPrefKey(isAuth), -1);
    // If the requested serial number is older, do not generate the keys.
    if (newSerialNumber <= currentSerialNumber) {
      return false;
    }
    rawGenerateKeyPair(isAuth);
    sharedPreferences.edit().putInt(toSerialNumberPrefKey(isAuth), newSerialNumber).apply();
    return true;
  }

  static String toKeyTypeString(boolean isAuth) {
    return isAuth ? "Auth" : "NoAuth";
  }

  private int checkAndGetSerialNumber(boolean isAuth) throws NoSuchKeyException {