Search in sources :

Example 6 with KeyChain

use of com.facebook.crypto.keychain.KeyChain 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 7 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class NativeGCMCipherInputStreamTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    mNativeCryptoLibrary = new SystemNativeCryptoLibrary();
    KeyChain keyChain = new FakeKeyChain();
    mCrypto = new Crypto(keyChain, mNativeCryptoLibrary);
    mIV = keyChain.getNewIV();
    mKey = keyChain.getCipherKey();
    // Encrypt some data before each test.
    mData = new byte[CryptoTestUtils.NUM_DATA_BYTES];
    Random random = new Random();
    random.nextBytes(mData);
    ByteArrayOutputStream cipherOutputStream = new ByteArrayOutputStream();
    OutputStream outputStream = mCrypto.getCipherOutputStream(cipherOutputStream, new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(mData);
    outputStream.close();
    mCipheredData = cipherOutputStream.toByteArray();
    mCipherInputStream = new ByteArrayInputStream(mCipheredData);
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary) KeyChain(com.facebook.crypto.keychain.KeyChain) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class NativeGCMCipherOutputStreamTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    mNativeCryptoLibrary = new SystemNativeCryptoLibrary();
    KeyChain keyChain = new FakeKeyChain();
    mKey = keyChain.getCipherKey();
    mIV = keyChain.getNewIV();
    mCrypto = new Crypto(keyChain, mNativeCryptoLibrary);
    mData = new byte[CryptoTestUtils.NUM_DATA_BYTES];
    mCipherOutputStream = new ByteArrayOutputStream();
}
Also used : SystemNativeCryptoLibrary(com.facebook.crypto.util.SystemNativeCryptoLibrary) KeyChain(com.facebook.crypto.keychain.KeyChain) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class SharedPrefsBackedKeyChainTest method test256Bits.

public void test256Bits() throws Exception {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(this.getInstrumentation().getContext(), CryptoConfig.KEY_256);
    // destroy keys if they were present in prefs from previous test
    keyChain.destroyKeys();
    byte[] key = keyChain.getCipherKey();
    assertEquals(32, key.length);
    KeyChain keyChain2 = new SharedPrefsBackedKeyChain(this.getInstrumentation().getContext());
    byte[] key2 = keyChain.getCipherKey();
    assertTrue(Arrays.equals(key, key2));
}
Also used : SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain)

Example 10 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class SimpleEncryptTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    SoLoader.init(this.getInstrumentation().getContext(), false);
    KeyChain keyChain = new FakeKeyChain();
    mKey = keyChain.getCipherKey();
    mIV = keyChain.getNewIV();
    mConfig = CryptoConfig.KEY_128;
    mCrypto = AndroidConceal.get().createCrypto128Bits(keyChain);
    mData = new byte[CryptoTestUtils.NUM_DATA_BYTES];
}
Also used : KeyChain(com.facebook.crypto.keychain.KeyChain)

Aggregations

KeyChain (com.facebook.crypto.keychain.KeyChain)12 SharedPrefsBackedKeyChain (com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)5 AndroidCryptoLibrary (com.facebook.android.crypto.keychain.AndroidCryptoLibrary)3 Crypto (com.facebook.crypto.Crypto)3 CryptoTestUtils.fixedKeyChain (com.facebook.crypto.CryptoTestUtils.fixedKeyChain)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)2 OutputStream (java.io.OutputStream)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 CryptoManagerImpl (com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl)1 Provides (dagger.Provides)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Random (java.util.Random)1 Singleton (javax.inject.Singleton)1