public App() { InitializeComponent(); if (!string.IsNullOrEmpty(Preferences.Get("accesstoken", ""))) { MainPage = new MasterPage(); } else if (string.IsNullOrEmpty(Preferences.Get("useremail", "")) && string.IsNullOrEmpty(Preferences.Get("password", ""))) { MainPage = new NavigationPage(new LoginPage()); } }
static VersionTracking() { IsFirstLaunchEver = !Preferences.ContainsKey(versionsKey, sharedName) || !Preferences.ContainsKey(buildsKey, sharedName); if (IsFirstLaunchEver) { versionTrail = new Dictionary <string, List <string> > { { versionsKey, new List <string>() }, { buildsKey, new List <string>() } }; } else { versionTrail = new Dictionary <string, List <string> > { { versionsKey, ReadHistory(versionsKey).ToList() }, { buildsKey, ReadHistory(buildsKey).ToList() } }; } IsFirstLaunchForCurrentVersion = !versionTrail[versionsKey].Contains(CurrentVersion); if (IsFirstLaunchForCurrentVersion) { versionTrail[versionsKey].Add(CurrentVersion); } IsFirstLaunchForCurrentBuild = !versionTrail[buildsKey].Contains(CurrentBuild); if (IsFirstLaunchForCurrentBuild) { versionTrail[buildsKey].Add(CurrentBuild); } if (IsFirstLaunchForCurrentVersion || IsFirstLaunchForCurrentBuild) { WriteHistory(versionsKey, versionTrail[versionsKey]); WriteHistory(buildsKey, versionTrail[buildsKey]); } }
static void WriteHistory(string key, IEnumerable <string> history) => Preferences.Set(key, string.Join("|", history), sharedName);
static string[] ReadHistory(string key) => Preferences.Get(key, null, sharedName)?.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
static void PlatformRemoveAll() => Preferences.Clear(Alias);
ISecretKey GetKey() { // check to see if we need to get our key from past-versions or newer versions. // we want to use symmetric if we are >= 23 or we didn't set it previously. useSymmetric = Preferences.Get(useSymmetricPreferenceKey, Platform.HasApiLevel(BuildVersionCodes.M), SecureStorage.Alias); // If >= API 23 we can use the KeyStore's symmetric key if (useSymmetric && !alwaysUseAsymmetricKey) { return(GetSymmetricKey()); } // NOTE: KeyStore in < API 23 can only store asymmetric keys // specifically, only RSA/ECB/PKCS1Padding // So we will wrap our symmetric AES key we just generated // with this and save the encrypted/wrapped key out to // preferences for future use. // ECB should be fine in this case as the AES key should be // contained in one block. // Get the asymmetric key pair var keyPair = GetAsymmetricKeyPair(); var existingKeyStr = Preferences.Get(prefsMasterKey, null, alias); if (!string.IsNullOrEmpty(existingKeyStr)) { try { var wrappedKey = Convert.FromBase64String(existingKeyStr); var unwrappedKey = UnwrapKey(wrappedKey, keyPair.Private); var kp = unwrappedKey.JavaCast <ISecretKey>(); return(kp); } catch (InvalidKeyException ikEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Invalid Key. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ikEx.Message}"); } catch (IllegalBlockSizeException ibsEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Illegal Block Size. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ibsEx.Message}"); } catch (BadPaddingException paddingEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Bad Padding. This may be caused by system backup or upgrades. All secure storage items will now be removed. {paddingEx.Message}"); } SecureStorage.RemoveAll(); } var keyGenerator = KeyGenerator.GetInstance(aesAlgorithm); var defSymmetricKey = keyGenerator.GenerateKey(); var newWrappedKey = WrapKey(defSymmetricKey, keyPair.Public); Preferences.Set(prefsMasterKey, Convert.ToBase64String(newWrappedKey), alias); return(defSymmetricKey); }