1.7 ApiKey value type

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-07 14:12:16 +02:00
parent 1b236e03af
commit 79039623e8
3 changed files with 112 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
using System.Security.Cryptography;
using System.Text;
namespace FrameProcessor.Domain;
/// <summary>
/// Wraps the shared API key. <see cref="Matches"/> uses a constant-time comparison
/// over UTF-8 bytes to avoid leaking key length or content via timing side channels.
/// </summary>
public readonly record struct ApiKey
{
private readonly string? _value;
public ApiKey(string value)
{
ArgumentNullException.ThrowIfNull(value);
_value = value;
}
public string Value => _value ?? string.Empty;
public bool Matches(string? candidate)
{
if (candidate is null)
{
return false;
}
var expectedBytes = Encoding.UTF8.GetBytes(Value);
var candidateBytes = Encoding.UTF8.GetBytes(candidate);
return CryptographicOperations.FixedTimeEquals(expectedBytes, candidateBytes);
}
}