Skip to content

Commit

Permalink
Finalized encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasReitberger committed Mar 19, 2024
1 parent 76fc31f commit 2ed139d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
25 changes: 23 additions & 2 deletions src/MauiSettings.Example/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,38 @@
<Label
Text="License"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
SemanticProperties.HeadingLevel="Level1"
/>

<Editor
Text="{Binding LicenseInfo}"
/>
<Label
Text="New hash"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level1"
/>
<Grid
ColumnDefinitions="*,Auto"
>
<Editor
Text="{Binding HashKey}"
/>
<Button
Margin="4,2"
Grid.Column="1"
x:Name="ExchangeBtn"
Text="New key"
Command="{Binding ExchangeHashKeyCommand}"
/>
</Grid>

<Button
x:Name="SaveBtn"
Text="Save Settings"
Command="{Binding SaveSettingsCommand}"
HorizontalOptions="Fill" />
HorizontalOptions="Fill"
/>

<Button
x:Name="LoadBtn"
Expand Down
15 changes: 14 additions & 1 deletion src/MauiSettings.Example/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using AndreasReitberger.Shared.Core.Utilities;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiSettings.Example.Models.Settings;

Expand All @@ -10,6 +11,9 @@ public partial class MainPageViewModel : ObservableObject
[ObservableProperty]
bool isLoading = false;

[ObservableProperty]
string hashKey = App.Hash;

[ObservableProperty]
string licenseInfo = string.Empty;
partial void OnLicenseInfoChanged(string value)
Expand Down Expand Up @@ -52,6 +56,15 @@ async Task LoadSettingsFromDevice()
LoadSettings();
}

[RelayCommand]
async Task ExchangeHashKey()
{
string newKey = EncryptionManager.GenerateBase64Key();
await SettingsApp.ExhangeKeyAsync(oldKey: App.Hash, newKey: newKey);
App.Hash = HashKey = newKey;
LoadSettings();
}

[RelayCommand]
async Task ToDictionary()
{
Expand Down
20 changes: 14 additions & 6 deletions src/MauiSettings/MauiSettingsGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace AndreasReitberger.Maui
{
#region Settings Object

static object _settingsObject;
static object? _settingsObject;
public static SO SettingsObject
{
get
Expand Down Expand Up @@ -68,9 +68,7 @@ public MauiSettingsGeneric(SO settingsObject, string key)

#region LoadSettings
public static void LoadSettings() => LoadSettings(settings: SettingsObject);
//public static void LoadSettings(string? key = null) => LoadSettings(settings: SettingsObject, key: key);

//public static void LoadSetting<T>(Expression<Func<SO, T>> value, string? key = null) => LoadObjectSetting(SettingsObject, value, key: key);

public static void LoadSetting<T>(Expression<Func<SO, T>> value) => LoadObjectSetting(SettingsObject, value);

public static async Task LoadSettingAsync<T>(Expression<Func<SO, T>> value, string? key = null)
Expand All @@ -80,10 +78,9 @@ await Task.Run(async delegate
await LoadObjectSettingAsync(SettingsObject, value, key: key);
});
}
//public void LoadObjectSettings(string? key = null) => LoadSettings(this, key: key);

public void LoadObjectSettings() => LoadSettings(this);

//public static void LoadObjectSetting<T>(object settingsObject, Expression<Func<SO, T>> value, string? key = null)
public static void LoadObjectSetting<T>(object settingsObject, Expression<Func<SO, T>> value)
=> GetExpressionMeta(settings: settingsObject, value, MauiSettingsActions.Load);

Expand Down Expand Up @@ -298,6 +295,17 @@ public static async Task<Tuple<string, Tuple<object, Type>>> ToSettingsTupleAsyn
}
#endregion

#region MyRegion

public static Task ExhangeKeyAsync(string? newKey = null, string? oldKey = null)
=> Task.Run(async delegate
{
await LoadSecureSettingsAsync(key: oldKey);
await SaveSettingsAsync(key: newKey);
});

#endregion

#region Private
static List<MemberInfo> GetClassMetaAsList(object settings)
{
Expand Down

0 comments on commit 2ed139d

Please sign in to comment.