putString method
Method to set String in local storage
key
-> Key for key-value pair
isEncrypted
-> Flag whether to encrypt or not
val
-> Value for key-value pair
Implementation
Future<void> putString(String key, String val, {bool isEncrypted = false}) async {
_assetFunction(isEncrypted);
if (isEncrypted) {
if (Platform.isAndroid) {
_secureStorage?.write(key: key, value: val);
} else if (Platform.isIOS) {
if (await _isMasterKeyAvailable() && _areSubKeysAvailable()) {
final keys = await _getEncrypterKeys();
final algorithm = _getAlgorithm();
final encryptedKey = await algorithm.encrypt(key.codeUnits,
secretKey:
SecretKey(keys.first[SuperSecureSharedPref.keyForKey]!),
nonce: keys.first[SuperSecureSharedPref.ivForKey]);
final encryptedValue = await algorithm.encrypt(val.codeUnits,
secretKey: SecretKey(keys.last[SuperSecureSharedPref.keyForKey]!),
nonce: keys.last[SuperSecureSharedPref.ivForKey]);
_sharedPreferences.setString(encryptedKey.cipherText.toString(),
encryptedValue.cipherText.toString());
} else {
if (await _isMasterKeyAvailable() &&
_areSubKeysAvailable() == false) {
await _deleteMasterKey();
}
var masterKey = await _createMasterKey();
await _saveMasterKeyToKeychain(masterKey);
await _generateAndSaveKeyAndValueSubKeys(
await _getMasterKeyFromKeyChain());
putString(key, val, isEncrypted:isEncrypted);
//throw Exception("Failed to save data something is not correct, please report issue on github");
}
} else {
throw PlatformException(
message: "Not Supported for ${Platform.operatingSystem} platform",
code: "501");
}
} else {
_sharedPreferences.setString(key, val);
}
}