20 lines
772 B
C#
20 lines
772 B
C#
using System.Security.Cryptography;
|
|
|
|
namespace WordVersioning.Api.Services;
|
|
|
|
public interface IHashService
|
|
{
|
|
/// <summary>คำนวณ SHA256 ระหว่าง stream ผ่าน — คืน hex ตัวเล็ก
|
|
/// (อ่านจนจบ stream — caller ต้อง Seek(0) เองถ้าจะใช้ stream ต่อ)</summary>
|
|
Task<string> ComputeSha256HexAsync(Stream stream, CancellationToken ct = default);
|
|
}
|
|
|
|
public class HashService : IHashService
|
|
{
|
|
public async Task<string> ComputeSha256HexAsync(Stream stream, CancellationToken ct = default)
|
|
{
|
|
using var sha = SHA256.Create();
|
|
var hash = await sha.ComputeHashAsync(stream, ct);
|
|
return Convert.ToHexString(hash).ToLowerInvariant();
|
|
}
|
|
}
|