feat: implement machine key provider

- implement logic for hashing machine key
- extracted interfaces
This commit is contained in:
Stevan Freeborn
2026-03-31 07:15:03 -05:00
parent e05e959378
commit 2915c32274
5 changed files with 98 additions and 7 deletions
@@ -0,0 +1,6 @@
namespace StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
internal interface IMachineIdKeyGenerator
{
string GetId();
}
@@ -0,0 +1,6 @@
namespace StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
internal interface IEncryptionKeyProvider
{
byte[] GetKey();
}
@@ -5,11 +5,6 @@ using Microsoft.Extensions.Logging;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
internal interface IMachineIdKeyGenerator
{
string GetId();
}
internal sealed class MachineIdKeyGenerator : IMachineIdKeyGenerator
{
private const string IOPlatformUUID = nameof(IOPlatformUUID);
@@ -111,4 +106,4 @@ internal static partial class LogMessages
Message = "Using fallback strategy for Machine ID generation."
)]
public static partial void LogUsingFallbackStrategy(this ILogger logger);
}
}
@@ -1,5 +1,17 @@
using System.Security.Cryptography;
using System.Text;
namespace StevanFreeborn.Extensions.Configuration.Secure.Cryptography;
internal sealed class MachineIdKeyProvider
internal sealed class MachineIdKeyProvider(IMachineIdKeyGenerator generator) : IEncryptionKeyProvider
{
private readonly IMachineIdKeyGenerator _generator = generator;
public byte[] GetKey()
{
var machineId = _generator.GetId();
var @bytes = Encoding.UTF8.GetBytes(machineId);
using var sha256 = SHA256.Create();
return sha256.ComputeHash(@bytes);
}
}