mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-09-06 07:53:11 +02:00
41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using System.Collections.Immutable;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Security.Cryptography;
|
|
using MemoryPack;
|
|
|
|
namespace Phantom.Common.Data;
|
|
|
|
[MemoryPackable(GenerateType.VersionTolerant)]
|
|
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
|
|
public sealed partial class AuthToken {
|
|
public const int Length = 12;
|
|
|
|
[MemoryPackOrder(0)]
|
|
[MemoryPackInclude]
|
|
public readonly ImmutableArray<byte> Bytes;
|
|
|
|
public AuthToken(ImmutableArray<byte> bytes) {
|
|
if (bytes.Length != Length) {
|
|
throw new ArgumentOutOfRangeException(nameof(bytes), "Invalid token length: " + bytes.Length + ". Token length must be exactly " + Length + " bytes.");
|
|
}
|
|
|
|
this.Bytes = bytes;
|
|
}
|
|
|
|
public bool FixedTimeEquals(AuthToken providedAuthToken) {
|
|
return FixedTimeEquals(providedAuthToken.Bytes.AsSpan());
|
|
}
|
|
|
|
public bool FixedTimeEquals(ReadOnlySpan<byte> other) {
|
|
return CryptographicOperations.FixedTimeEquals(Bytes.AsSpan(), other);
|
|
}
|
|
|
|
internal void WriteTo(Span<byte> span) {
|
|
Bytes.CopyTo(span);
|
|
}
|
|
|
|
public static AuthToken Generate() {
|
|
return new AuthToken([..RandomNumberGenerator.GetBytes(Length)]);
|
|
}
|
|
}
|