Skip to content

Commit 143c481

Browse files
committed
Save original info hash when parsing torrent
1 parent 1b76d83 commit 143c481

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

BencodeNET.Tests/Parsing/TorrentParserTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Text;
54
using BencodeNET.Exceptions;
65
using BencodeNET.IO;
@@ -555,5 +554,35 @@ public void MultiFile_Files_MissingPathField_ThrowsInvalidTorrentException()
555554
action.ShouldThrow<InvalidTorrentException>()
556555
.Where(ex => ex.InvalidField == TorrentFilesFields.Path);
557556
}
557+
558+
[Fact]
559+
public void OriginalInfoHash_IsSet()
560+
{
561+
// Arrange
562+
ParsedData = ValidSingleFileTorrentData;
563+
var expectedInfoHash = TorrentUtil.CalculateInfoHash(ParsedData.Get<BDictionary>("info"));
564+
565+
// Act
566+
var parser = new TorrentParser(BencodeParser);
567+
var torrent = parser.Parse((BencodeStream) null);
568+
569+
// Assert
570+
torrent.OriginalInfoHash.Should().Be(expectedInfoHash);
571+
}
572+
573+
[Fact]
574+
public void OriginalInfoHashBytes_IsSet()
575+
{
576+
// Arrange
577+
ParsedData = ValidSingleFileTorrentData;
578+
var expectedInfoHashBytes = TorrentUtil.CalculateInfoHashBytes(ParsedData.Get<BDictionary>("info"));
579+
580+
// Act
581+
var parser = new TorrentParser(BencodeParser);
582+
var torrent = parser.Parse((BencodeStream) null);
583+
584+
// Assert
585+
torrent.OriginalInfoHashBytes.Should().Equal(expectedInfoHashBytes);
586+
}
558587
}
559588
}

BencodeNET/BencodeNET.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
66
<GenerateDocumentationFile>True</GenerateDocumentationFile>
77
<PackageId>BencodeNET</PackageId>
8-
<Version>2.2.2</Version>
8+
<Version>2.2.3</Version>
99
<Authors>Søren Kruse</Authors>
1010
<Company />
1111
<Product>BencodeNET</Product>
@@ -15,7 +15,7 @@
1515
<PackageIconUrl>https://raw.githubusercontent.com/Krusen/BencodeNET/master/Assets/icon.png</PackageIconUrl>
1616
<RepositoryUrl>https://github.com/Krusen/BencodeNET</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
18-
<PackageReleaseNotes>Now uses the encoding information of a .torrent when parsing the torrent data.</PackageReleaseNotes>
18+
<PackageReleaseNotes>Save original info hash of parsed torrents</PackageReleaseNotes>
1919
<PackageTags>bencode;torrent;torrents</PackageTags>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<IncludeSource>True</IncludeSource>

BencodeNET/Parsing/TorrentParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected Torrent CreateTorrent(BDictionary data)
6060

6161
var encoding = ParseEncoding(data.Get<BString>(TorrentFields.Encoding)) ?? BencodeParser.Encoding;
6262

63-
var torrent = new Torrent
63+
var torrent = new Torrent(info)
6464
{
6565
IsPrivate = info.Get<BNumber>(TorrentInfoFields.Private) == 1,
6666
PieceSize = info.Get<BNumber>(TorrentInfoFields.PieceLength),

BencodeNET/Torrents/Torrent.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@ namespace BencodeNET.Torrents
1515
/// </summary>
1616
public class Torrent : BObject
1717
{
18+
/// <summary>
19+
///
20+
/// </summary>
21+
public Torrent()
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Creates a torrent and populates the <see cref="OriginalInfoHash"/> and <see cref="OriginalInfoHashBytes"/>
27+
/// properties from the provided <see cref="BDictionary"/>.
28+
/// </summary>
29+
/// <param name="originalInfoDictionary"></param>
30+
internal Torrent(BDictionary originalInfoDictionary)
31+
{
32+
OriginalInfoHashBytes = TorrentUtil.CalculateInfoHashBytes(originalInfoDictionary);
33+
OriginalInfoHash = TorrentUtil.BytesToHexString(OriginalInfoHashBytes);
34+
}
35+
36+
/// <summary>
37+
/// The original info hash value from when the torrent was parsed.
38+
/// This will be null if the instance was created manually and not by the parser.
39+
/// </summary>
40+
public string OriginalInfoHash { get; protected set; }
41+
42+
/// <summary>
43+
/// The original info hash bytes from when the torrent was parsed.
44+
/// This will be null if the instance was created manually and not by the parser.
45+
/// </summary>
46+
public byte[] OriginalInfoHashBytes { get; protected set; }
47+
1848
/// <summary>
1949
/// Add any custom fields to this <see cref="BDictionary"/> and they will
2050
/// be merged with the torrent data when encoded.

BencodeNET/Torrents/TorrentUtil.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public static byte[] CalculateInfoHashBytes(Torrent torrent)
5454
public static string CalculateInfoHash(BDictionary info)
5555
{
5656
var hashBytes = CalculateInfoHashBytes(info);
57-
58-
return BitConverter.ToString(hashBytes).Replace("-", "");
57+
return BytesToHexString(hashBytes);
5958
}
6059

6160
/// <summary>
@@ -79,6 +78,15 @@ public static byte[] CalculateInfoHashBytes(BDictionary info)
7978
}
8079
}
8180

81+
/// <summary>
82+
/// Converts the byte array to a hexadecimal string representation without hyphens.
83+
/// </summary>
84+
/// <param name="bytes"></param>
85+
public static string BytesToHexString(byte[] bytes)
86+
{
87+
return BitConverter.ToString(bytes).Replace("-", "");
88+
}
89+
8290
/// <summary>
8391
/// Creates a Magnet link in the BTIH (BitTorrent Info Hash) format: xt=urn:btih:{info hash}
8492
/// </summary>

0 commit comments

Comments
 (0)