Search in sources :

Example 1 with Crypto

use of com.facebook.crypto.Crypto in project TeamCityApp by vase4kin.

the class AppModule method providesCryptoManager.

@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
@Provides
@Singleton
protected CryptoManager providesCryptoManager() {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(mApplication.getApplicationContext(), CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    return new CryptoManagerImpl(crypto);
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) CryptoManagerImpl(com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) VisibleForTesting(android.support.annotation.VisibleForTesting) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with Crypto

use of com.facebook.crypto.Crypto in project LabDayApp by jakdor.

the class ProjectRepository method loadAccessToken.

/**
 * Loads access token and decrypts it with Conceal
 * @param context required for SharedPreferences/Conceal
 * @return boolean success/fail
 */
public boolean loadAccessToken(Context context) {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    try {
        byte[] encryptedToken = readFile(context, "lab");
        if (encryptedToken == null) {
            Timber.e("Access token not found");
            return false;
        } else if (encryptedToken.length == 0) {
            Timber.e("Access token not available");
            return false;
        }
        byte[] plainToken = crypto.decrypt(encryptedToken, Entity.create("token"));
        this.accessToken = new String(plainToken);
        Timber.i("Loaded and decrypted access token");
    } catch (Exception e) {
        Timber.wtf("unable to decipher access token");
        return false;
    }
    return true;
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)

Example 3 with Crypto

use of com.facebook.crypto.Crypto in project UltimateAndroid by cymcsg.

the class FakeKeyChain method encryptingContent.

public static void encryptingContent(Context context, File file, byte[] plainTextBytes) throws Exception {
    // Creates a new Crypto object with default implementations of
    // a key chain as well as native library.
    Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary());
    // This might fail if android does not load libaries correctly.
    if (!crypto.isAvailable()) {
        return;
    }
    OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(file));
    // Creates an output stream which encrypts the data as
    // it is written to it and writes it out to the file.
    OutputStream outputStream = crypto.getCipherOutputStream(fileStream, new Entity("TEST1"));
    // Write plaintext to it.
    outputStream.write(plainTextBytes);
    outputStream.close();
}
Also used : Entity(com.facebook.crypto.Entity) Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.crypto.keychain.SharedPrefsBackedKeyChain) SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary)

Example 4 with Crypto

use of com.facebook.crypto.Crypto in project UltimateAndroid by cymcsg.

the class FakeKeyChain method decryptingContent.

public static void decryptingContent(Context context, File file, String newPath) throws Exception {
    // Get the file to which ciphertext has been written.
    FileInputStream fileStream = new FileInputStream(file);
    Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(context), new SystemNativeCryptoLibrary());
    // Creates an input stream which decrypts the data as
    // it is read from it.
    InputStream inputStream = crypto.getCipherInputStream(fileStream, new Entity("TEST1"));
    // Read into a byte array.
    int read;
    byte[] buffer = new byte[1024];
    // You must read the entire stream to completion.
    // The verification is done at the end of the stream.
    // Thus not reading till the end of the stream will cause
    // a security bug.
    FileOutputStream fs = new FileOutputStream(newPath);
    while ((read = inputStream.read(buffer)) != -1) {
        fs.write(buffer, 0, read);
    }
    inputStream.close();
}
Also used : Entity(com.facebook.crypto.Entity) Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.crypto.keychain.SharedPrefsBackedKeyChain) SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary)

Example 5 with Crypto

use of com.facebook.crypto.Crypto in project LabDayApp by jakdor.

the class ProjectRepository method saveAccessToken.

/**
 * Saves access token encrypted with Conceal
 * @param token access token retrieved after successful login
 * @param context required for SharedPreferences/Conceal
 */
public void saveAccessToken(String token, Context context) {
    if (token.equals("-1")) {
        this.accessToken = token;
        Timber.e("bad access token");
        return;
    }
    KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    try {
        byte[] tokenBytes = token.getBytes(Charset.forName("UTF-8"));
        byte[] encryptedToken = crypto.encrypt(tokenBytes, Entity.create("token"));
        FileOutputStream outputStream = context.openFileOutput("lab", Context.MODE_PRIVATE);
        outputStream.write(encryptedToken);
        outputStream.close();
        Timber.i("Successful login, access token saved");
    } catch (Exception e) {
        Timber.wtf("unable to save access token");
    }
    this.accessToken = token;
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) FileOutputStream(java.io.FileOutputStream) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)

Aggregations

Crypto (com.facebook.crypto.Crypto)5 SharedPrefsBackedKeyChain (com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)3 KeyChain (com.facebook.crypto.keychain.KeyChain)3 Entity (com.facebook.crypto.Entity)2 SharedPrefsBackedKeyChain (com.facebook.crypto.keychain.SharedPrefsBackedKeyChain)2 SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 CryptoManagerImpl (com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl)1 Provides (dagger.Provides)1 FileOutputStream (java.io.FileOutputStream)1 Singleton (javax.inject.Singleton)1