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