{"id":25702820,"url":"https://github.com/Krusen/BencodeNET","last_synced_at":"2025-02-25T05:04:48.559Z","repository":{"id":25185666,"uuid":"28609008","full_name":"Krusen/BencodeNET","owner":"Krusen","description":".NET library for encoding/decoding bencode and reading/writing torrent files","archived":false,"fork":false,"pushed_at":"2023-10-31T19:06:36.000Z","size":1113,"stargazers_count":153,"open_issues_count":6,"forks_count":30,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-02-10T19:51:59.077Z","etag":null,"topics":["bencode","bencode-parser","encoding","parsing","torrent","torrent-files"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Krusen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"ko_fi":"krusen","custom":["https://www.buymeacoffee.com/UCkS2tw"]}},"created_at":"2014-12-29T22:39:48.000Z","updated_at":"2025-01-01T16:08:23.000Z","dependencies_parsed_at":"2022-07-24T04:32:27.730Z","dependency_job_id":"3b9a11e7-3add-4922-bbed-f999c26cb753","html_url":"https://github.com/Krusen/BencodeNET","commit_stats":{"total_commits":382,"total_committers":8,"mean_commits":47.75,"dds":"0.023560209424083767","last_synced_commit":"33a31cb31e7e865e1753a0a98bf39cfb6f262728"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krusen%2FBencodeNET","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krusen%2FBencodeNET/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krusen%2FBencodeNET/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Krusen%2FBencodeNET/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Krusen","download_url":"https://codeload.github.com/Krusen/BencodeNET/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240605831,"owners_count":19827983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bencode","bencode-parser","encoding","parsing","torrent","torrent-files"],"created_at":"2025-02-25T05:02:14.914Z","updated_at":"2025-02-25T05:04:48.523Z","avatar_url":"https://github.com/Krusen.png","language":"C#","readme":"![Icon](Assets/icon_64.png)\n\nBencodeNET \n==========\n[![license](https://img.shields.io/badge/license-Unlicense-blue.svg)](https://github.com/Krusen/BencodeNET/blob/master/LICENSE.md)\n[![Azure Pipelines](https://krusen.visualstudio.com/BencodeNET/_apis/build/status/BencodeNET?branchName=feature%2Fazure-pipelines)](https://krusen.visualstudio.com/BencodeNET/_build?definitionId=5)\n[![Coverage](https://coveralls.io/repos/github/Krusen/BencodeNET/badge.svg?branch=master)](https://coveralls.io/github/Krusen/BencodeNET?branch=master)\n[![CodeFactor](https://www.codefactor.io/repository/github/krusen/bencodenet/badge)](https://www.codefactor.io/repository/github/krusen/bencodenet)\n[![NuGet](https://buildstats.info/nuget/bencodenet?includePreReleases=false)](https://www.nuget.org/packages/BencodeNET/)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_shield)\n\nA lightweight and fast .NET library for encoding and decoding bencode (e.g. torrent files and BitTorrent client/tracker communication).\n\n- http://en.wikipedia.org/wiki/Bencode\n- https://wiki.theory.org/BitTorrentSpecification#Bencoding\n\nThe main focus of this library is on supporting the bencode format; torrent file reading/manipulation is secondary.\n\nContents\n--------\n\n- [Project status](#project-status)\n- [Installation](#installation)\n- [Getting started](#getting-started)\n  - [Parsing](#parsing)\n  - [Encoding](#encoding)\n  - [String character encoding](#string-character-encoding)\n  - [Torrents](#torrents)\n    - [File modes](#file-modes)\n    - [Non-standard fields](#non-standard-fields)\n- [Changelog](#changelog)\n- [Roadmap](#roadmap)\n- [Building the project](#building-the-project)\n- [Contributing](#contributing)\n- [Support](#support)\n\nProject status\n--------------\nThe project is in maintenance mode and only getting updated if issues are being reported or if new features are requested.\n\nSo, while I can't promise anything, go ahead and report any issues or feature requests by creating a new issue.\n\n\nInstallation\n------------\nInstall the package **BencodeNET** from [NuGet](https://www.nuget.org/packages/BencodeNET/), using `\u003cPackageReference\u003e` or from the command line:\n\n```\n// .csproj using PackageReference\n\u003cPackageReference Include=\"BencodeNET\" Version=\"2.3.0\" /\u003e\n\n// .NET CLI\n\u003e dotnet add package BencodeNET\n```\n\n\nGetting started\n---------------\n### Parsing\nHere are some simple examples for parsing bencode strings directly.\n\n```C#\nusing BencodeNET.Parsing;\nusing BencodeNET.Objects;\n\nvar parser = new BencodeParser();\n\n// Parse unknown type\nIBObject bstring = parser.ParseString(\"12:Hellow World!\");\n// \"Hello World!\" (BString)\n\n// If you know the type of the bencode you are parsing, you can use the generic version of `ParseString()` instead.\nBString bstring = parser.ParseString\u003cBString\u003e(\"12:Hello World!\");\n// \"Hello World!\" (BString)\n\nBNumber bnumber = parser.ParseString\u003cBNumber\u003e(\"i42e\");\n// 42 (BNumber)\n\nBList blist = parser.ParseString\u003cBList\u003e(\"l3:foo3:bari42ee\");\n// { \"foo\", \"bar\", 42 } (BList)\n\nBDictionary bdictionary = parser.ParseString\u003cBDictionary\u003e(\"d3:fooi42e5:Hello6:World!e\");\n// { { \"foo\", 42 }, { \"Hello\", \"World\" } } (BDictionary)\n```\n\nUsually you would probably either parse a `Stream` of some kind or a `PipeReader` if using .NET Core.\n\n```C#\nBDictionary bdictionary = parser.Parse\u003cBDictionary\u003e(stream);\nBDictionary bdictionary = await parser.ParseAsync\u003cBDictionary\u003e(stream);\nBDictionary bdictionary = await parser.ParseAsync\u003cBDictionary\u003e(pipeReader);\n```\n\n### Encoding\nEncoding an object is simple and can be done in the following ways:\n\n```C#\nvar bstring = new BString(\"Hello World!\");\n\nbstring.EncodeAsString();    // \"12:Hello World!\"\nbstring.EncodeAsBytes();     // [ 49, 50, 58, 72, ... ]\n\nbstring.EncodeTo(\"C:\\\\data.bencode\"); // Writes \"12:Hello World!\" to the specified file\nbstring.EncodeTo(stream);\nawait bstring.EncodeToAsync(stream);\nbstring.EncodeTo(pipeWriter);\nawait bstring.EncodeToAsync(pipeWriter);\n```\n\n### String character encoding\n\nBy default `Encoding.UTF8` is used when rendering strings. \n\nWhen parsing a string directly the encoding is used to convert the string to an array of bytes.\n\nIf no encoding is passed to `ToString` it will use the encoding the `BString` was created/decoded with.\n\n```C#\n// Using the default encoding from Bencode.DefaultEncoding (UTF8)\nvar parser = new BencodeParser();\nvar bstring = parser.ParseString(\"21:æøå äö èéê ñ\");\nbstring.ToString()              // \"æøå äö èéê ñ\"\nbstring.ToString(Encoding.UTF8) // \"æøå äö èéê ñ\"\n\n// Using ISO-8859-1\nvar parser = new BencodeParser(Encoding.GetEncoding(\"ISO-8859-1\"));\nbstring = parser.ParseString(\"12:æøå äö èéê ñ\");\nbstring.ToString();              // \"æøå äö èéê ñ\"\nbstring.ToString(Encoding.UTF8); // \"??? ?? ??? ?\"\n```\n\nIf you parse bencoded data that is not encoded using UTF8 and you don't specify the encoding, then `EncodeAsString`, \n`EncodeAsBytes`, `EncodeTo` and `ToString` without parameters will use `Encoding.UTF8` to try to render the `BString` \nand you will not get the expected result.\n\n```C#\nvar bytes = Encoding.GetEncoding(\"ISO-8859-1\").GetBytes(\"12:æøå äö èéê ñ\");\n\n// When not specifying an encoding, ToString will use Encoding.UTF8\nvar parser = new BencodeParser();\n\nvar bstring = parser.Parse\u003cBString\u003e(bytes);\nbstring.ToString();\n// \"??? ?? ??? ?\"\n\n// Pass your desired encoding to ToString to override the encoding used to render the string\nbstring.ToString(Encoding.GetEncoding(\"ISO-8859-1\"));\n// \"æøå äö èéê ñ\"\n\n// You have to specify the used encoding when creating the parser\n// BStrings will then use that as the default when encoding the string\nvar parser = new BencodeParser(Encoding.GetEncoding(\"ISO-8859-1\"));\nbstring = parser.Parse\u003cBString\u003e(bytes);\nbstring.ToString();\n// \"æøå äö èéê ñ\"\n```\n\nThe default encoding, UTF8, should be fine in almost all cases.\n\nWhen you encode an object directly to a stream (`IBObject.EncodeTo`) the encoding is irrelevant as \nthe `BString`s are converted to bytes when created, using the specified encoding at the time.\n\nHowever, when encoding to a string (`IBObject.EncodeAsString`) you can specify the encoding used to render the string.\n`BString.EncodeAsString` without specifying an encoding will use the encoding the `BString` was created with.\nFor all the other types `Encoding.UTF8` will be used.\n\n\u003e **Note:** Using `EncodeAsString` of `BList` and `BDictionary` will encode all contained `BString` using the specified encoding or `Encoding.UTF8` if no encoding is specified.\n\n```C#\nvar blist = new BList();\nblist.Add(new BString(\"æøå äö èéê ñ\", Encoding.GetEncoding(\"ISO-8859-1\")));\nblist.EncodeAsString();                                   // \"l12:??? ?? ??? ?e\"\nblist.EncodeAsString(Encoding.UTF8);                      // \"l12:??? ?? ??? ?e\nblist.EncodeAsString(Encoding.GetEncoding(\"ISO-8859-1\")); // \"l12:æøå äö èéê ñe\"\"\n```\n\n### Torrents\n\nWorking with torrent files:\n\n```C#\nusing BencodeNET.Objects;\nusing BencodeNET.Parsing;\nusing BencodeNET.Torrents;\n\n// Parse torrent by specifying the file path\nvar parser = new BencodeParser(); // Default encoding is Encoding.UTF8, but you can specify another if you need to\nTorrent torrent = parser.Parse\u003cTorrent\u003e(\"C:\\\\ubuntu.torrent\");\n\n// Or parse a stream\nTorrent torrent = parser.Parse\u003cTorrent\u003e(stream);\n\n// Calculate the info hash\nstring infoHash = torrent.GetInfoHash();\n// \"B415C913643E5FF49FE37D304BBB5E6E11AD5101\"\n\n// or as bytes instead of a string\nbyte[] infoHashBytes = torrent.GetInfoHashBytes();\n\n// Get Magnet link\nstring magnetLink = torrent.GetMagnetLink();\n// magnet:?xt=urn:btih:1CA512A4822EDC7C1B1CE354D7B8D2F84EE11C32\u0026dn=ubuntu-14.10-desktop-amd64.iso\u0026tr=http://torrent.ubuntu.com:6969/announce\u0026tr=http://ipv6.torrent.ubuntu.com:6969/announce\n\n// Convert Torrent to its BDictionary representation\nBDictionary bdictinoary = torrent.ToBDictionary();\n```\n\n#### File modes\nThe property `FileMode` indicates if the torrent is single-file or multi-file. \n\nFor single-file torrents the `File` property contains the relevant file info. \nThe `Files` property is null.\n\nFor multi-file torrents the `Files` property contains a list of file info and the directory name.\nThe `File` property is null.\n\n####  Non-standard fields\nThe `ExtraFields` property is for any non-standard fields which are not accessible through any other property.\nData set on this property will overwrite any data from the `Torrent` itself when encoding it. This way you are able to add to or owerwrite fields.\n\n\nChangelog\n---------\nSee [CHANGELOG.md](CHANGELOG.md) for any yet unreleased changes and changes made between different version.\n\nhttps://keepachangelog.com\n\n\nRoadmap\n-------\nThe project is in maintenance mode and no new features are currently planned.\nFeel free to request new features by creating a new issue.\n\n\nBuilding the project\n---------------------------------\nRequirements:\n- .NET Core 3.0 SDK\n- Visual Studio 2019 (or other IDE support .NET Core 3.0)\n\nSimply checkout the project, restore nuget packages and build the project.\n\n\nContributing\n------------\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update or add tests as appropriate.\n\n\nSupport\n-------\nIf you use this or one of my other libraries and want to say thank you or support the development feel free to buy me a Red Bull through [buymeacoffee.com](https://www.buymeacoffee.com/UCkS2tw) or through [ko-fi.com](https://ko-fi.com/krusen).\n\n\u003ca href=\"https://www.buymeacoffee.com/UCkS2tw\" target=\"_blank\"\u003e\u003cimg src=\"https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: auto !important;width: auto !important;\" \u003e\u003c/a\u003e\n\n\u003ca href=\"https://ko-fi.com/krusen\" target=\"_blank\"\u003e\u003cimg height=\"37\" src=\"https://az743702.vo.msecnd.net/cdn/kofi1.png?v=2\"/\u003e\u003c/a\u003e\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FKrusen%2FBencodeNET?ref=badge_large)\n","funding_links":["https://ko-fi.com/krusen","https://www.buymeacoffee.com/UCkS2tw"],"categories":["C# #"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKrusen%2FBencodeNET","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKrusen%2FBencodeNET","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKrusen%2FBencodeNET/lists"}