Skip to content

Refactor: Use SHA256 hash for registry keys related to file paths #17381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Files.App/Helpers/Layout/LayoutPreferencesDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -139,7 +140,7 @@ private void IterateKeys(List<LayoutPreferencesDatabaseItem> 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();
Expand Down
15 changes: 8 additions & 7 deletions src/Files.App/Utils/FileTags/FileTagsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [])
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -182,7 +183,7 @@ public IEnumerable<TaggedFile> GetAllUnderPath(string folderPath)
{
try
{
IterateKeys(list, CombineKeys(FileTagsKey, folderPath), 0);
IterateKeys(list, CombineKeys(FileTagsKey, CreateSHA256(folderPath)), 0);
}
catch (SecurityException)
{
Expand All @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Files.Shared/Helpers/ChecksumHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> hash = stackalloc byte[SHA256.HashSizeInBytes];
SHA256.HashData(buffer, hash);
return Convert.ToHexString(hash).ToLower();
}

public async static Task<string> CreateCRC32(Stream stream, CancellationToken cancellationToken)
{
var crc32 = new Crc32();
Expand Down
Loading