com.facebook.crypto.Entity Java Examples

The following examples show how to use com.facebook.crypto.Entity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CipherStorageFacebookConceal.java    From react-native-keychain with MIT License 6 votes vote down vote up
@Override
@NonNull
public EncryptionResult encrypt(@NonNull final String alias,
                                @NonNull final String username,
                                @NonNull final String password,
                                @NonNull final SecurityLevel level)
  throws CryptoFailedException {

  throwIfInsufficientLevel(level);
  throwIfNoCryptoAvailable();

  final Entity usernameEntity = createUsernameEntity(alias);
  final Entity passwordEntity = createPasswordEntity(alias);

  try {
    final byte[] encryptedUsername = crypto.encrypt(username.getBytes(UTF8), usernameEntity);
    final byte[] encryptedPassword = crypto.encrypt(password.getBytes(UTF8), passwordEntity);

    return new EncryptionResult(
      encryptedUsername,
      encryptedPassword,
      this);
  } catch (Throwable fail) {
    throw new CryptoFailedException("Encryption failed for alias: " + alias, fail);
  }
}
 
Example #2
Source File: CipherStorageFacebookConceal.java    From react-native-keychain with MIT License 6 votes vote down vote up
@NonNull
@Override
public DecryptionResult decrypt(@NonNull final String alias,
                                @NonNull final byte[] username,
                                @NonNull final byte[] password,
                                @NonNull final SecurityLevel level)
  throws CryptoFailedException {

  throwIfInsufficientLevel(level);
  throwIfNoCryptoAvailable();

  final Entity usernameEntity = createUsernameEntity(alias);
  final Entity passwordEntity = createPasswordEntity(alias);

  try {
    final byte[] decryptedUsername = crypto.decrypt(username, usernameEntity);
    final byte[] decryptedPassword = crypto.decrypt(password, passwordEntity);

    return new DecryptionResult(
      new String(decryptedUsername, UTF8),
      new String(decryptedPassword, UTF8),
      SecurityLevel.ANY);
  } catch (Throwable fail) {
    throw new CryptoFailedException("Decryption failed for alias: " + alias, fail);
  }
}
 
Example #3
Source File: CipherStorageFacebookConceal.java    From react-native-secure-storage with MIT License 5 votes vote down vote up
@Override
public DecryptionResult decrypt(@NonNull String service, @NonNull String key, @NonNull byte[] value) throws CryptoFailedException {
    if (!crypto.isAvailable()) {
        throw new CryptoFailedException("Crypto is missing");
    }
    Entity valueEntity = createEntity(service, key);

    try {
        byte[] decryptedValue = crypto.decrypt(value, valueEntity);

        return new DecryptionResult(key, new String(decryptedValue, charsetName));
    } catch (Exception e) {
        throw new CryptoFailedException("Decryption failed for service " + service, e);
    }
}
 
Example #4
Source File: ConcealCrypto.java    From droid-stealth with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decrypts the encrypted file. Can also throw a lot of errors
 *
 * @param encrypted
 * @param unencrypted
 * @param entityName
 * @throws IOException
 * @throws CryptoInitializationException
 * @throws KeyChainException
 */
@Override
public void decrypt(File encrypted, File unencrypted,
                    String entityName) throws IOException, CryptoInitializationException, KeyChainException {
	doFileChecks(encrypted, unencrypted);

	InputStream from = crypto.getCipherInputStream(new FileInputStream(encrypted),
			new Entity(entityName));

	FileOutputStream to = new FileOutputStream(unencrypted);

	copyStreams(from, to);
}
 
Example #5
Source File: ConcealCrypto.java    From droid-stealth with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Encrypts the unencrypted file. Can throw a lot of errors
 *
 * @param encrypted
 * @param unencrypted
 * @param entityName
 * @throws IOException
 * @throws CryptoInitializationException
 * @throws KeyChainException
 */
@Override
public void encrypt(File encrypted, File unencrypted,
                    String entityName) throws IOException, CryptoInitializationException, KeyChainException {
	doFileChecks(unencrypted, encrypted);

	FileInputStream from = new FileInputStream(unencrypted); // Stream to read from source
	OutputStream to = crypto.getCipherOutputStream(new FileOutputStream(encrypted),
			new Entity(entityName)); // Stream to write to destination

	copyStreams(from, to);
}
 
Example #6
Source File: CryptoUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
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();
    }
 
Example #7
Source File: CryptoUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
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());


// Check for whether the crypto functionality is available
// 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();
    }
 
Example #8
Source File: CryptoUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
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();
    }
 
Example #9
Source File: CryptoUtils.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
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());


// Check for whether the crypto functionality is available
// 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();
    }
 
Example #10
Source File: SecurePreferences.java    From secure-preferences with Apache License 2.0 5 votes vote down vote up
private SecurePreferences(Context context,
                          final KeyChain keyChain,
                          final Entity entity,
                          final String sharedPrefFilename) {
    this.entity = entity;
    this.sharedPreferences = getSharedPreferenceFile(context, sharedPrefFilename);
    this.crypto = AndroidConceal.get().createCrypto256Bits(keyChain);
}
 
Example #11
Source File: SecurePreferences.java    From secure-preferences with Apache License 2.0 5 votes vote down vote up
public SharedPreferences build() {
    if(!isInit) {
        Log.w(TAG, "You need call 'SecurePreferences.init()' in onCreate() from your application class.");
    }
    KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
    Entity entity = Entity.create(
            TextUtils.isEmpty(password) ? getClass().getPackage().getName() : password
    );
    return new SecurePreferences(
            context,
            keyChain,
            entity,
            sharedPrefFilename
    );
}
 
Example #12
Source File: CipherStorageFacebookConceal.java    From react-native-secure-storage with MIT License 5 votes vote down vote up
@Override
public EncryptionResult encrypt(@NonNull String service, @NonNull String key, @NonNull String value) throws CryptoFailedException {
    if (!crypto.isAvailable()) {
        throw new CryptoFailedException("Crypto is missing");
    }
    Entity valueEntity = createEntity(service, key);

    try {
        byte[] encryptedValue = crypto.encrypt(value.getBytes(charsetName), valueEntity);

        return new EncryptionResult(key, encryptedValue, this);
    } catch (Exception e) {
        throw new CryptoFailedException("Encryption failed for service " + service, e);
    }
}
 
Example #13
Source File: ConcealEncryption.java    From hawk with Apache License 2.0 4 votes vote down vote up
@Override public String encrypt(String key, String plainText) throws Exception {
  Entity entity = Entity.create(key);
  byte[] bytes = crypto.encrypt(plainText.getBytes(), entity);
  return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
 
Example #14
Source File: ConcealEncryption.java    From hawk with Apache License 2.0 4 votes vote down vote up
@Override public String decrypt(String key, String cipherText) throws Exception {
  Entity entity = Entity.create(key);
  byte[] decodedBytes = Base64.decode(cipherText, Base64.NO_WRAP);
  byte[] bytes = crypto.decrypt(decodedBytes, entity);
  return new String(bytes);
}
 
Example #15
Source File: CipherStorageFacebookConceal.java    From react-native-keychain with MIT License 4 votes vote down vote up
@NonNull
private static Entity createPasswordEntity(@NonNull final String alias) {
  final String prefix = getEntityPrefix(alias);

  return Entity.create(prefix + "pass");
}
 
Example #16
Source File: CipherStorageFacebookConceal.java    From react-native-keychain with MIT License 4 votes vote down vote up
@NonNull
private static Entity createUsernameEntity(@NonNull final String alias) {
  final String prefix = getEntityPrefix(alias);

  return Entity.create(prefix + "user");
}
 
Example #17
Source File: CipherStorageFacebookConceal.java    From react-native-secure-storage with MIT License 4 votes vote down vote up
private Entity createEntity(String service, String key) {
    String prefix = getEntityPrefix(service);
    return Entity.create(prefix + ":" + key);
}