diff --git a/src/Files.App/Helpers/Layout/LayoutPreferencesDatabase.cs b/src/Files.App/Helpers/Layout/LayoutPreferencesDatabase.cs index e03b0a7e0db2..d844dd8a94d1 100644 --- a/src/Files.App/Helpers/Layout/LayoutPreferencesDatabase.cs +++ b/src/Files.App/Helpers/Layout/LayoutPreferencesDatabase.cs @@ -6,6 +6,7 @@ using Windows.ApplicationModel; using static Files.App.Helpers.LayoutPreferencesDatabaseItemRegistry; using static Files.App.Helpers.RegistryHelpers; +using static Files.Shared.Helpers.ChecksumHelpers; using JsonSerializer = System.Text.Json.JsonSerializer; namespace Files.App.Helpers @@ -58,7 +59,7 @@ void UpdateValues(LayoutPreferencesDatabaseItem? preferences) { if (filePath is not null) { - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, filePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, CreateSHA256(filePath))); SaveValues(filePathKey, preferences); } @@ -91,7 +92,7 @@ private static void ImportCore(LayoutPreferencesDatabaseItem[]? preferences) } foreach (var preference in preferences) { - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, preference.FilePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, CreateSHA256(preference.FilePath))); SaveValues(filePathKey, preference); if (preference.Frn is not null) { @@ -139,7 +140,7 @@ private void IterateKeys(List list, string path, { if (filePath is not null) { - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, filePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(LayoutSettingsKey, CreateSHA256(filePath))); if (filePathKey.ValueCount > 0) { var preference = new LayoutPreferencesDatabaseItem(); diff --git a/src/Files.App/Utils/FileTags/FileTagsDatabase.cs b/src/Files.App/Utils/FileTags/FileTagsDatabase.cs index e7d120850dd7..25394621823f 100644 --- a/src/Files.App/Utils/FileTags/FileTagsDatabase.cs +++ b/src/Files.App/Utils/FileTags/FileTagsDatabase.cs @@ -7,6 +7,7 @@ using Windows.ApplicationModel; using static Files.App.Helpers.RegistryHelpers; using static Files.App.Utils.FileTags.TaggedFileRegistry; +using static Files.Shared.Helpers.ChecksumHelpers; using JsonSerializer = System.Text.Json.JsonSerializer; namespace Files.App.Utils.FileTags @@ -21,7 +22,7 @@ public void SetTags(string filePath, ulong? frn, string[] tags) if (FileTagsKey is null) return; - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(filePath))); if (tags is []) { @@ -57,7 +58,7 @@ public void SetTags(string filePath, ulong? frn, string[] tags) if (filePath is not null) { - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, filePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(filePath))); if (filePathKey.ValueCount > 0) { var tag = new TaggedFile(); @@ -99,7 +100,7 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath) return; var tag = FindTag(oldFilePath, null); - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, oldFilePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(oldFilePath))); SaveValues(filePathKey, null); if (tag is not null) @@ -115,7 +116,7 @@ public void UpdateTag(string oldFilePath, ulong? frn, string? newFilePath) if (newFilePath is not null) { - using var newFilePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, newFilePath)); + using var newFilePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(newFilePath))); SaveValues(newFilePathKey, tag); } } @@ -143,7 +144,7 @@ public void UpdateTag(ulong oldFrn, ulong? frn, string? newFilePath) if (newFilePath is not null) { - using var newFilePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, newFilePath)); + using var newFilePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(newFilePath))); SaveValues(newFilePathKey, tag); } } @@ -182,7 +183,7 @@ public IEnumerable GetAllUnderPath(string folderPath) { try { - IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0); + IterateKeys(list, CombineKeys(FileTagsKey, CreateSHA256(folderPath)), 0); } catch (SecurityException) { @@ -207,7 +208,7 @@ public void Import(string json) } foreach (var tag in tags) { - using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, tag.FilePath)); + using var filePathKey = Registry.CurrentUser.CreateSubKey(CombineKeys(FileTagsKey, CreateSHA256(tag.FilePath))); SaveValues(filePathKey, tag); if (tag.Frn is not null) { diff --git a/src/Files.Shared/Helpers/ChecksumHelpers.cs b/src/Files.Shared/Helpers/ChecksumHelpers.cs index 4e2b061ca7c0..848900e58b1e 100644 --- a/src/Files.Shared/Helpers/ChecksumHelpers.cs +++ b/src/Files.Shared/Helpers/ChecksumHelpers.cs @@ -22,6 +22,14 @@ public static string CalculateChecksumForPath(string path) return Convert.ToHexString(hash); } + public static string CreateSHA256(string input) + { + var buffer = Encoding.UTF8.GetBytes(input); + Span hash = stackalloc byte[SHA256.HashSizeInBytes]; + SHA256.HashData(buffer, hash); + return Convert.ToHexString(hash).ToLower(); + } + public async static Task CreateCRC32(Stream stream, CancellationToken cancellationToken) { var crc32 = new Crc32();