Search in sources :

Example 1 with SystemNativeCryptoLibrary

use of com.facebook.crypto.util.SystemNativeCryptoLibrary 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 2 with SystemNativeCryptoLibrary

use of com.facebook.crypto.util.SystemNativeCryptoLibrary 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 3 with SystemNativeCryptoLibrary

use of com.facebook.crypto.util.SystemNativeCryptoLibrary 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 SystemNativeCryptoLibrary

use of com.facebook.crypto.util.SystemNativeCryptoLibrary 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)

Aggregations

SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)4 Crypto (com.facebook.crypto.Crypto)2 Entity (com.facebook.crypto.Entity)2 KeyChain (com.facebook.crypto.keychain.KeyChain)2 SharedPrefsBackedKeyChain (com.facebook.crypto.keychain.SharedPrefsBackedKeyChain)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 Random (java.util.Random)1