use of com.facebook.crypto.Crypto in project TeamCityApp by vase4kin.
the class AppModule method providesCryptoManager.
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
@Provides
@Singleton
protected CryptoManager providesCryptoManager() {
KeyChain keyChain = new SharedPrefsBackedKeyChain(mApplication.getApplicationContext(), CryptoConfig.KEY_256);
Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
return new CryptoManagerImpl(crypto);
}
use of com.facebook.crypto.Crypto 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;
}
use of com.facebook.crypto.Crypto 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();
}
use of com.facebook.crypto.Crypto 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();
}
use of com.facebook.crypto.Crypto 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;
}
Aggregations