Search in sources :

Example 1 with AndroidCryptoLibrary

use of com.facebook.android.crypto.keychain.AndroidCryptoLibrary in project conceal by facebook.

the class Cipher128BitsTest method testCase.

public void testCase(String key, String iv, String plain, String cipher) throws Exception {
    // Test Case 16 - Page 40
    KeyChain keyChain = fixedKeyChain(key, iv);
    CryptoConfig config = CryptoConfig.KEY_128;
    Crypto crypto = new Crypto(keyChain, new AndroidCryptoLibrary(), config);
    byte[] plainBytes = toBytes(plain);
    byte[] encrypted = crypto.encrypt(plainBytes, new Entity("whatever"));
    byte[] expected = toBytes(cipher);
    // remove initial 2 bytes + IV
    // remove final tag 16 bytes
    int metadataLength = config.getHeaderLength() + config.getTailLength();
    int prefix = metadataLength - config.tagLength;
    byte[] bareEncrypted = Arrays.copyOfRange(encrypted, prefix, encrypted.length - config.tagLength);
    Assert.assertTrue(Arrays.equals(expected, bareEncrypted));
}
Also used : CryptoTestUtils.fixedKeyChain(com.facebook.crypto.CryptoTestUtils.fixedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) AndroidCryptoLibrary(com.facebook.android.crypto.keychain.AndroidCryptoLibrary)

Example 2 with AndroidCryptoLibrary

use of com.facebook.android.crypto.keychain.AndroidCryptoLibrary in project conceal by facebook.

the class EntityFactoryTest method testCase.

public void testCase(String key, String iv, String plain, String entityId) throws Exception {
    // Test Case 16 - Page 40
    KeyChain keyChain = fixedKeyChain(key, iv);
    CryptoConfig config = CryptoConfig.KEY_256;
    Crypto crypto = new Crypto(keyChain, new AndroidCryptoLibrary(), config);
    byte[] plainBytes = toBytes(plain);
    byte[] encrypted = crypto.encrypt(plainBytes, Entity.create(entityId));
    byte[] decrypted = crypto.decrypt(encrypted, Entity.create(entityId));
    Assert.assertFalse(Arrays.equals(encrypted, plainBytes));
    Assert.assertTrue(Arrays.equals(decrypted, plainBytes));
    try {
        crypto.decrypt(encrypted, Entity.utf16(entityId));
        Assert.fail("Decryption with old entity should have failed!");
    } catch (IOException ioe) {
    // ok, it shouldn't match!
    }
}
Also used : CryptoTestUtils.fixedKeyChain(com.facebook.crypto.CryptoTestUtils.fixedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) IOException(java.io.IOException) AndroidCryptoLibrary(com.facebook.android.crypto.keychain.AndroidCryptoLibrary)

Example 3 with AndroidCryptoLibrary

use of com.facebook.android.crypto.keychain.AndroidCryptoLibrary in project conceal by facebook.

the class Cipher256BitsTest method testCase.

public void testCase(String key, String iv, String plain, String cipher) throws Exception {
    // Test Case 16 - Page 40
    KeyChain keyChain = fixedKeyChain(key, iv);
    CryptoConfig config = CryptoConfig.KEY_256;
    Crypto crypto = new Crypto(keyChain, new AndroidCryptoLibrary(), config);
    byte[] plainBytes = toBytes(plain);
    byte[] encrypted = crypto.encrypt(plainBytes, new Entity("whatever"));
    byte[] expected = toBytes(cipher);
    // remove initial 2 bytes + IV
    // remove final tag 16 bytes
    int prefix = config.getHeaderLength();
    int metadataLength = config.getHeaderLength() + config.getTailLength();
    byte[] bareEncrypted = Arrays.copyOfRange(encrypted, prefix, encrypted.length - config.tagLength);
    Assert.assertTrue(Arrays.equals(expected, bareEncrypted));
}
Also used : CryptoTestUtils.fixedKeyChain(com.facebook.crypto.CryptoTestUtils.fixedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) AndroidCryptoLibrary(com.facebook.android.crypto.keychain.AndroidCryptoLibrary)

Aggregations

AndroidCryptoLibrary (com.facebook.android.crypto.keychain.AndroidCryptoLibrary)3 CryptoTestUtils.fixedKeyChain (com.facebook.crypto.CryptoTestUtils.fixedKeyChain)3 KeyChain (com.facebook.crypto.keychain.KeyChain)3 IOException (java.io.IOException)1