Search in sources :

Example 11 with KeyChain

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

the class SimpleDecryptTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    SoLoader.init(this.getInstrumentation().getContext(), false);
    KeyChain keyChain = new FakeKeyChain();
    mCrypto = AndroidConceal.get().createCrypto128Bits(keyChain);
    mIV = keyChain.getNewIV();
    mKey = keyChain.getCipherKey();
    // Encrypt some data before each test.
    mData = new byte[CryptoTestUtils.NUM_DATA_BYTES];
    ByteArrayOutputStream cipherOutputStream = new ByteArrayOutputStream();
    OutputStream outputStream = mCrypto.getCipherOutputStream(cipherOutputStream, new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(mData);
    outputStream.close();
    mCipheredData = cipherOutputStream.toByteArray();
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyChain(com.facebook.crypto.keychain.KeyChain) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with KeyChain

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

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