Files
mtusi2026lab2/scr/Messenger.Api/Services/SessionTokens.cs
T
2026-10-23 21:13:27 +03:00

17 lines
917 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Cryptography;
using System.Text;
namespace Messenger.Api.Services;
public static class SessionTokens
{
// 32 случайных байта = 256 бит энтропии (контракт: не менее 256). В base64url это 43 символа.
public static string Generate() =>
Convert.ToBase64String(RandomNumberGenerator.GetBytes(32))
.TrimEnd('=').Replace('+', '-').Replace('/', '_');
// В БД хранится только SHA-256 токена. Быстрый хеш здесь уместен: токен случайный и длинный, подбирать нечего
// (в отличие от пароля, который выбирает человек и который поэтому хешируется медленным Argon2id).
public static byte[] Hash(string token) => SHA256.HashData(Encoding.UTF8.GetBytes(token));
}