Skip to content

Commit

Permalink
remove usage of experimental rxjava emitter (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauin authored Oct 14, 2016
1 parent 022e8cc commit de70481
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,49 @@
import com.mtramin.rxfingerprint.data.FingerprintAuthenticationResult;
import com.mtramin.rxfingerprint.data.FingerprintResult;

import rx.Emitter;
import rx.Observable;

import static rx.Emitter.BackpressureMode.LATEST;
import rx.Subscriber;

/**
* Authenticates the user with his fingerprint.
*/
class FingerprintAuthenticationObservable extends FingerprintObservable<FingerprintAuthenticationResult> {

private FingerprintAuthenticationObservable(Context context) {
super(context);
}
/**
* Creates an Observable that will enable the fingerprint scanner of the device and listen for
* the users fingerprint for authentication
*
* @param context context to use
* @return Observable {@link FingerprintAuthenticationResult}
*/
static Observable<FingerprintAuthenticationResult> create(Context context) {
return Observable.create(new FingerprintAuthenticationObservable(context));
}

/**
* Creates an Observable that will enable the fingerprint scanner of the device and listen for
* the users fingerprint for authentication
*
* @param context context to use
* @return Observable {@link FingerprintAuthenticationResult}
*/
static Observable<FingerprintAuthenticationResult> create(Context context) {
return Observable.fromEmitter(new FingerprintAuthenticationObservable(context), LATEST);
}
private FingerprintAuthenticationObservable(Context context) {
super(context);
}

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<FingerprintAuthenticationResult> subscriber) {
// Simple authentication does not need CryptoObject
return null;
}
@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Subscriber<FingerprintAuthenticationResult> subscriber) {
// Simple authentication does not need CryptoObject
return null;
}

@Override
protected void onAuthenticationSucceeded(Emitter<FingerprintAuthenticationResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.AUTHENTICATED, null));
emitter.onCompleted();
}
@Override
protected void onAuthenticationSucceeded(Subscriber<FingerprintAuthenticationResult> subscriber, FingerprintManagerCompat.AuthenticationResult result) {
subscriber.onNext(new FingerprintAuthenticationResult(FingerprintResult.AUTHENTICATED, null));
subscriber.onCompleted();
}

@Override
protected void onAuthenticationHelp(Emitter<FingerprintAuthenticationResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.HELP, helpString));
}
@Override
protected void onAuthenticationHelp(Subscriber<FingerprintAuthenticationResult> subscriber, int helpMessageId, String helpString) {
subscriber.onNext(new FingerprintAuthenticationResult(FingerprintResult.HELP, helpString));
}

@Override
protected void onAuthenticationFailed(Emitter<FingerprintAuthenticationResult> emitter) {
emitter.onNext(new FingerprintAuthenticationResult(FingerprintResult.FAILED, null));
}
@Override
protected void onAuthenticationFailed(Subscriber<FingerprintAuthenticationResult> subscriber) {
subscriber.onNext(new FingerprintAuthenticationResult(FingerprintResult.FAILED, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import rx.Emitter;
import rx.Observable;

import static rx.Emitter.BackpressureMode.LATEST;
import rx.Subscriber;

/**
* Decrypts data with fingerprint authentication. Initializes a {@link Cipher} for decryption which
Expand All @@ -51,74 +49,74 @@
*/
class FingerprintDecryptionObservable extends FingerprintObservable<FingerprintDecryptionResult> {

private final String keyName;
private final CryptoData encryptedData;

private FingerprintDecryptionObservable(Context context, String keyName, String encrypted) {
super(context);
this.keyName = keyName;
this.encryptedData = CryptoData.fromString(encrypted);
}

/**
* Creates a new FingerprintEncryptionObservable 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) {
return Observable.fromEmitter(new FingerprintDecryptionObservable(context, keyName, encrypted), LATEST);
}

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

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Emitter<FingerprintDecryptionResult> subscriber) {
CryptoProvider cryptoProvider = new CryptoProvider(this.context, this.keyName);
try {
Cipher cipher = cryptoProvider.initDecryptionCipher(encryptedData.getIv());
return new FingerprintManagerCompat.CryptoObject(cipher);
} catch (NoSuchAlgorithmException | CertificateException | InvalidKeyException | KeyStoreException | InvalidAlgorithmParameterException | NoSuchPaddingException | IOException | UnrecoverableKeyException e) {
subscriber.onError(e);
return null;
}
}

@Override
protected void onAuthenticationSucceeded(Emitter<FingerprintDecryptionResult> emitter, FingerprintManagerCompat.AuthenticationResult result) {
try {
Cipher cipher = result.getCryptoObject().getCipher();
String decrypted = new String(cipher.doFinal(encryptedData.getMessage()));

emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.AUTHENTICATED, null, decrypted));
emitter.onCompleted();
} catch (BadPaddingException | IllegalBlockSizeException e) {
emitter.onError(e);
}

}

@Override
protected void onAuthenticationHelp(Emitter<FingerprintDecryptionResult> emitter, int helpMessageId, String helpString) {
emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(Emitter<FingerprintDecryptionResult> emitter) {
emitter.onNext(new FingerprintDecryptionResult(FingerprintResult.FAILED, null, null));
}
private final String keyName;
private final CryptoData encryptedData;

/**
* Creates a new FingerprintEncryptionObservable 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) {
return Observable.create(new FingerprintDecryptionObservable(context, keyName, encrypted));
}

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

private FingerprintDecryptionObservable(Context context, String keyName, String encrypted) {
super(context);
this.keyName = keyName;
encryptedData = CryptoData.fromString(encrypted);
}

@Nullable
@Override
protected FingerprintManagerCompat.CryptoObject initCryptoObject(Subscriber<FingerprintDecryptionResult> subscriber) {
CryptoProvider cryptoProvider = new CryptoProvider(context, keyName);
try {
Cipher cipher = cryptoProvider.initDecryptionCipher(encryptedData.getIv());
return new FingerprintManagerCompat.CryptoObject(cipher);
} catch (NoSuchAlgorithmException | CertificateException | InvalidKeyException | KeyStoreException | InvalidAlgorithmParameterException | NoSuchPaddingException | IOException | UnrecoverableKeyException e) {
subscriber.onError(e);
return null;
}
}

@Override
protected void onAuthenticationSucceeded(Subscriber<FingerprintDecryptionResult> subscriber, FingerprintManagerCompat.AuthenticationResult result) {
try {
Cipher cipher = result.getCryptoObject().getCipher();
String decrypted = new String(cipher.doFinal(encryptedData.getMessage()));

subscriber.onNext(new FingerprintDecryptionResult(FingerprintResult.AUTHENTICATED, null, decrypted));
subscriber.onCompleted();
} catch (BadPaddingException | IllegalBlockSizeException e) {
subscriber.onError(e);
}

}

@Override
protected void onAuthenticationHelp(Subscriber<FingerprintDecryptionResult> subscriber, int helpMessageId, String helpString) {
subscriber.onNext(new FingerprintDecryptionResult(FingerprintResult.HELP, helpString, null));
}

@Override
protected void onAuthenticationFailed(Subscriber<FingerprintDecryptionResult> subscriber) {
subscriber.onNext(new FingerprintDecryptionResult(FingerprintResult.FAILED, null, null));
}
}
Loading

0 comments on commit de70481

Please sign in to comment.