// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintDecryptionResult.java
// public class FingerprintDecryptionResult extends FingerprintAuthenticationResult {
// 
//     private final char[] decrypted;
// 
//     /**
//      * Default constructor
//      *
//      * @param result    result of the fingerprint authentication
//      * @param message   message to be displayed to the user
//      * @param decrypted decrypted data
//      */
//     public FingerprintDecryptionResult(FingerprintResult result, String message, char[] decrypted) {
//         super(result, message);
//         this.decrypted = decrypted;
//     }
// 
//     /**
//      * @return decrypted data as a String. Can only be accessed if the result of the fingerprint
//      * authentication was of type {@link FingerprintResult#AUTHENTICATED}.
//      */
//     public String getDecrypted() {
//         return new String(getDecryptedChars());
//     }
// 
//     /**
//      * @return decrypted data as a char[]. Can only be accessed if the result of the fingerprint
//      * authentication was of type {@link FingerprintResult#AUTHENTICATED}.
//      */
//     public char[] getDecryptedChars() {
//         if (!isSuccess()) {
//             throw new IllegalAccessError("Fingerprint authentication was not successful, cannot access decryption result");
//         }
//         return decrypted;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintEncryptionResult.java
// public class FingerprintEncryptionResult extends FingerprintAuthenticationResult {
// 
//     private final String encrypted;
// 
//     /**
//      * Default constructor
//      *
//      * @param result    result of the operation
//      * @param message   message to be displayed to the user
//      * @param encrypted encrypted data
//      */
//     public FingerprintEncryptionResult(FingerprintResult result, String message, String encrypted) {
//         super(result, message);
//         this.encrypted = encrypted;
//     }
// 
//     /**
//      * @return encrypted data, can only be accessed if the result was of
//      * type {@link FingerprintResult#AUTHENTICATED}
//      */
//     public String getEncrypted() {
//         if (!isSuccess()) {
//             throw new IllegalAccessError("Fingerprint authentication was not successful, cannot access encryption result");
//         }
//         return encrypted;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintResult.java
// public enum FingerprintResult {
//     FAILED, HELP, AUTHENTICATED
// }


import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.support.annotation.Nullable;
import com.mtramin.rxfingerprint.data.FingerprintDecryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintEncryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintResult;
import javax.crypto.Cipher;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
/*
 * Copyright 2015 Marvin Ramin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mtramin.rxfingerprint;





/**
 * Decrypts data with fingerprint authentication. Initializes a {@link Cipher} for decryption which
 * can only be used with fingerprint authentication and uses it once authentication was successful
 * to encrypt the given data.
 * <p/>
 * The date handed in must be previously encrypted by a {@link AesEncryptionObservable}.
 */
@SuppressLint("NewApi") // SDK check happens in {@link FingerprintObservable#subscribe}
class AesDecryptionObservable extends FingerprintObservable<FingerprintDecryptionResult> {

	private final AesCipherProvider cipherProvider;
	private final String encryptedString;
	private final EncodingProvider encodingProvider;

	/**
	 * Creates a new AesEncryptionObservable that will listen to fingerprint authentication
	 * to encrypt the given data.
	 *
	 * @param context   context to use
	 * @param keyName   keyName to use for the decryption
	 * @param encrypted data to encrypt  @return Observable {@link FingerprintEncryptionResult}
	 * @return Observable result of the decryption
	 */
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintDecryptionResult.java
// public class FingerprintDecryptionResult extends FingerprintAuthenticationResult {
// 
//     private final char[] decrypted;
// 
//     /**
//      * Default constructor
//      *
//      * @param result    result of the fingerprint authentication
//      * @param message   message to be displayed to the user
//      * @param decrypted decrypted data
//      */
//     public FingerprintDecryptionResult(FingerprintResult result, String message, char[] decrypted) {
//         super(result, message);
//         this.decrypted = decrypted;
//     }
// 
//     /**
//      * @return decrypted data as a String. Can only be accessed if the result of the fingerprint
//      * authentication was of type {@link FingerprintResult#AUTHENTICATED}.
//      */
//     public String getDecrypted() {
//         return new String(getDecryptedChars());
//     }
// 
//     /**
//      * @return decrypted data as a char[]. Can only be accessed if the result of the fingerprint
//      * authentication was of type {@link FingerprintResult#AUTHENTICATED}.
//      */
//     public char[] getDecryptedChars() {
//         if (!isSuccess()) {
//             throw new IllegalAccessError("Fingerprint authentication was not successful, cannot access decryption result");
//         }
//         return decrypted;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintEncryptionResult.java
// public class FingerprintEncryptionResult extends FingerprintAuthenticationResult {
// 
//     private final String encrypted;
// 
//     /**
//      * Default constructor
//      *
//      * @param result    result of the operation
//      * @param message   message to be displayed to the user
//      * @param encrypted encrypted data
//      */
//     public FingerprintEncryptionResult(FingerprintResult result, String message, String encrypted) {
//         super(result, message);
//         this.encrypted = encrypted;
//     }
// 
//     /**
//      * @return encrypted data, can only be accessed if the result was of
//      * type {@link FingerprintResult#AUTHENTICATED}
//      */
//     public String getEncrypted() {
//         if (!isSuccess()) {
//             throw new IllegalAccessError("Fingerprint authentication was not successful, cannot access encryption result");
//         }
//         return encrypted;
//     }
// }
// 
// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/data/FingerprintResult.java
// public enum FingerprintResult {
//     FAILED, HELP, AUTHENTICATED
// }


// Path: rxfingerprint/src/main/java/com/mtramin/rxfingerprint/AesDecryptionObservable.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
import android.hardware.fingerprint.FingerprintManager.CryptoObject;
import android.support.annotation.Nullable;
import com.mtramin.rxfingerprint.data.FingerprintDecryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintEncryptionResult;
import com.mtramin.rxfingerprint.data.FingerprintResult;
import javax.crypto.Cipher;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;

/*
 * Copyright 2015 Marvin Ramin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mtramin.rxfingerprint;





/**
 * Decrypts data with fingerprint authentication. Initializes a {@link Cipher} for decryption which
 * can only be used with fingerprint authentication and uses it once authentication was successful
 * to encrypt the given data.
 * <p/>
 * The date handed in must be previously encrypted by a {@link AesEncryptionObservable}.
 */
@SuppressLint("NewApi") // SDK check happens in {@link FingerprintObservable#subscribe}
class AesDecryptionObservable extends FingerprintObservable<FingerprintDecryptionResult> {

	private final AesCipherProvider cipherProvider;
	private final String encryptedString;
	private final EncodingProvider encodingProvider;

	/**
	 * Creates a new AesEncryptionObservable that will listen to fingerprint authentication
	 * to encrypt the given data.
	 *
	 * @param context   context to use
	 * @param keyName   keyName to use for the decryption
	 * @param encrypted data to encrypt  @return Observable {@link FingerprintEncryptionResult}
	 * @return Observable result of the decryption
	 */
	static Observable<FingerprintDecryptionResult> create(Context context, String keyName, String encrypted) {