using System.Security.Cryptography;

namespace MiGu.Server.Ota;

public static class OtaHash
{
    /// <summary>与参考 OTA 工具一致：MD5 → Base64，并去掉 '-'。</summary>
    public static string OfFile(string path)
    {
        using var fs = File.OpenRead(path);
        var hash = MD5.HashData(fs);
        return Convert.ToBase64String(hash).Replace("-", "", StringComparison.Ordinal);
    }

    public static string OfBytes(ReadOnlySpan<byte> bytes)
    {
        var hash = MD5.HashData(bytes);
        return Convert.ToBase64String(hash).Replace("-", "", StringComparison.Ordinal);
    }

    public static string Short(string? hash, int len = 8)
    {
        if (string.IsNullOrEmpty(hash)) return "—";
        return hash.Length <= len ? hash : hash[..len];
    }
}
