# Invalid info Hash

**URL:** <https://forum.codecrafters.io/t/invalid-info-hash/3062>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [November 22, 2024, 3:47pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062 "2024-11-22T15:47:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlesmatte](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/charlesmatte/32/1012_2.png) [@charlesmatte](https://forum.codecrafters.io/u/charlesmatte)\
**Post date:** [November 22, 2024, 3:47pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062/1 "2024-11-22T15:47:29Z")

</div>

I’m stuck on the Tracker GET request on Stage #FI9.

I keep getting this reponse from the tracker URL:

```auto
d14:failure reason25:provided invalid infohashe

```

Here’s the info hash hexadecimal value I am extracting from the sample torrent file:  
d69f91e6b2ae4c542468d1073a71d4ea13879a7f

The problem is unlikely to come from the info hash itself because I have double-checked it against the expected info hash hex value at step #RB2 and it matches perfectly. I also succesfully passed that step which would have been impossible without the right info hash value.

This is is how I extract the hex value of the info hash:

```csharp
        const string infoMarker = "4:infod";
        var hashStart =
            encodedFile.IndexOf(infoMarker, StringComparison.Ordinal) + infoMarker.Length - 1;
        var chunk = bytes[hashStart..^1];
        InfoHash = SHA1.HashData(chunk);
        InfoHashHex = Convert.ToHexString(InfoHash).ToLower(); 

```

After that, all I do is URL encode the info hash bytes and add to the query params:

```csharp
        var queryParams = new Dictionary<string, string>
        {
            { "info_hash", HttpUtility.UrlEncode(torrentFile.InfoHash) },
            { "peer_id", "22974793449159003000" },
            { "port", "6881" },
            { "uploaded", "0" },
            { "downloaded", "0" },
            { "left", torrentFile.Length },
            { "compact", "1" },
        };

```

The final endpoint to which I send a GET request looks like this:  
[http://bittorrent-test-tracker.codecrafters.io/announce?info\_hash=%25d6%259f%2591%25e6%25b2%25aeLT%2524h%25d1%2507%253aq%25d4%25ea%2513%2587%259a%257f&peer\_id=22974793449159003000&port=6881&uploaded=0&downloaded=0&left=92063&compact=1](http://bittorrent-test-tracker.codecrafters.io/announce?info_hash=%25d6%259f%2591%25e6%25b2%25aeLT%2524h%25d1%2507%253aq%25d4%25ea%2513%2587%259a%257f&peer_id=22974793449159003000&port=6881&uploaded=0&downloaded=0&left=92063&compact=1)

I have tried encoding the InfoHash in a thousand different ways but to no avail. This ultimately seems like the right way to do it. Yet the CodeCrafter tells me its invalid.

Any tip would be appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![sarp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sarp/32/1082_2.png) [@sarp](https://forum.codecrafters.io/u/sarp)\
**Post date:** [November 22, 2024, 4:03pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062/2 "2024-11-22T16:03:17Z")

</div>

Hi charlesmatte,  
Can you try without HttpUtility.UrlEncode

---

<div class="post-metadata">

**Author:** ![charlesmatte](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/charlesmatte/32/1012_2.png) [@charlesmatte](https://forum.codecrafters.io/u/charlesmatte)\
**Post date:** [November 22, 2024, 6:08pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062/3 "2024-11-22T18:08:37Z")

</div>

Hi sarp,

Thanks for pointing that out.

I was encoding the value twice, once with HttpUtility.UrlEncode(torrentFile.InfoHash) and a second time later through HttpUtility.ParseQueryString which automatically encodes the values.

I got rid of HttpUtility.ParseQueryString and it did the trick.

---

<div class="post-metadata">

**Author:** ![sarp](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/sarp/32/1082_2.png) [@sarp](https://forum.codecrafters.io/u/sarp)\
**Post date:** [November 22, 2024, 6:23pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062/4 "2024-11-22T18:23:09Z")

</div>

Nice, glad you figured it out!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 27, 2024, 6:23pm UTC](https://forum.codecrafters.io/t/invalid-info-hash/3062/5 "2024-11-27T18:23:23Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
