HRM-10/Ease.Core/Utility/JwtSigningKey.cs

24 lines
893 B
C#
Raw Normal View History

2026-11-03 13:06:00 +06:00
using System.Security.Cryptography;
using System.Text;
namespace Ease.Core.Utility
{
/// <summary>
/// The key every JWT of the application is signed and validated with (HmacSha512Signature).
/// Since Microsoft.IdentityModel 7 (.NET 8+ JwtBearer), HS512 refuses a key shorter than 512 bits
/// (IDX10720), so a shorter AppSettings.Secret is stretched to 64 bytes with SHA-512. A secret of
/// 64 bytes or more is used as it is. Token issuing and token validation must both use this, or
/// every login fails with an invalid signature.
/// </summary>
public static class JwtSigningKey
{
private const int MinimumKeyBytes = 64;
public static byte[] FromSecret(string secret)
{
byte[] key = Encoding.ASCII.GetBytes(secret);
return key.Length >= MinimumKeyBytes ? key : SHA512.HashData(key);
}
}
}