TorrentUtils is a C# rewrite of airium/TorrentUtils. It provides BitTorrent metadata APIs for parsing, writing, hashing, top-level metadata rewriting, and payload verification.
The library is focused on BEP 3 / v1 torrent metadata. It also exposes a lightweight meta version probe so callers can identify v2 metadata before choosing a parsing path.
dotnet add package TorrentUtilsRead metadata and compute the v1 info hash:
using TorrentUtils;
using var torrent = new Torrent();
torrent.ReadTorrentFile("sample.torrent");
Console.WriteLine(torrent.Name);
Console.WriteLine(torrent.ComputeInfoHashHex());
foreach (var file in torrent.FileList)
{
Console.WriteLine($"{file.RelativePath} {file.Size}");
}Create a v1 torrent from a file or directory:
using TorrentUtils;
using var torrent = new Torrent(pieceLength: 4 * 1024 * 1024);
torrent.SetTrackerList(["https://tracker.example/announce"]);
torrent.SetCreatedBy("TorrentUtils");
await torrent.LoadFromPathAsync(new DirectoryInfo("payload"));
torrent.WriteTorrentFile("payload.torrent");Verify payload bytes against an existing torrent:
using TorrentUtils;
using var torrent = new Torrent();
torrent.ReadTorrentFile("payload.torrent");
IReadOnlyList<int> failedPieces = torrent.Verify("payload");Probe the torrent metadata version:
using TorrentUtils;
int version = TorrentVersion.ReadMetaVersionFromFile("sample.torrent");
bool isV2 = TorrentVersion.IsV2FromFile("sample.torrent");Rewrite top-level metadata without changing the info dictionary:
using TorrentUtils;
var update = new TorrentTopLevelUpdate();
update.SetComment("Updated comment");
update.SetCreatedBy("TorrentUtils");
TorrentTopLevelRewriter.RewriteFile("input.torrent", "output.torrent", update);