Files
SocialPub/PrivaPub/Infrastructure/Ids/PrivacyIds.cs
T

20 lines
666 B
C#
Raw Normal View History

using System.Buffers.Binary;
using System.Security.Cryptography;
namespace PrivaPub.Infrastructure.Ids
{
public static class PrivacyIds
{
public static string ForDay(DateTime when) => At(DateTime.SpecifyKind(when, DateTimeKind.Utc).Date);
public static string At(DateTime when)
{
var seconds = Math.Clamp(new DateTimeOffset(DateTime.SpecifyKind(when, DateTimeKind.Utc)).ToUnixTimeSeconds(), 0,
DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeSeconds());
Span<byte> bytes = stackalloc byte[12];
BinaryPrimitives.WriteInt32BigEndian(bytes, (int)seconds);
RandomNumberGenerator.Fill(bytes[4..]);
return Convert.ToHexStringLower(bytes);
}
}
}