Files
Govor/Govor.Application/Authentication/JWT/JwtTokenHasher.cs
T
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
2026-07-16 19:27:45 +07:00

40 lines
1.1 KiB
C#

using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Configuration;
namespace Govor.Application.Authentication.JWT;
public class JwtTokenHasher : IJwtTokenHasher
{
private readonly byte[] _pepperBytes;
public JwtTokenHasher(IConfiguration config)
{
var pepper = config["EncryptionOption:Secret"]
?? throw new InvalidOperationException("Pepper is missing");
_pepperBytes = Encoding.UTF8.GetBytes(pepper);
}
public string HashToken(string token)
{
using var hmac = new HMACSHA256(_pepperBytes);
var tokenBytes = Encoding.UTF8.GetBytes(token);
var hash = hmac.ComputeHash(tokenBytes);
return Convert.ToBase64String(hash);
}
public bool VerifyToken(string token, string storedHash)
{
using var hmac = new HMACSHA256(_pepperBytes);
var tokenBytes = Encoding.UTF8.GetBytes(token);
var computedHash = hmac.ComputeHash(tokenBytes);
var storedHashBytes = Convert.FromBase64String(storedHash);
return CryptographicOperations.FixedTimeEquals(computedHash, storedHashBytes);
}
}