Files
tiku-backend.net/Tiku.Infrastructure/Auth/SmsCodeHashing.cs
xiong c497a3ca8d
Some checks failed
ci / release-gate (push) Has been cancelled
清理代码
2026-08-03 12:31:39 +08:00

39 lines
1.1 KiB
C#

using System.Security.Cryptography;
using System.Text;
using Tiku.Domain.Tenancy;
namespace Tiku.Infrastructure.Auth;
public static class SmsCodeHashing
{
public static string Hash(
Guid tenantId,
string phone,
SmsPurpose purpose,
string code,
string pepper)
{
ArgumentException.ThrowIfNullOrWhiteSpace(pepper);
var normalized = $"{tenantId:N}:{NormalizePhone(phone)}:{purpose}:{code.Trim()}";
var hash = HMACSHA256.HashData(
Encoding.UTF8.GetBytes(pepper),
Encoding.UTF8.GetBytes(normalized));
return Convert.ToHexString(hash).ToLowerInvariant();
}
public static string HashScope(string value, string pepper)
{
ArgumentException.ThrowIfNullOrWhiteSpace(pepper);
var hash = HMACSHA256.HashData(
Encoding.UTF8.GetBytes(pepper),
Encoding.UTF8.GetBytes(value.Trim().ToLowerInvariant()));
return Convert.ToHexString(hash).ToLowerInvariant();
}
public static string NormalizePhone(string phone)
{
return phone.Trim();
}
}