Invalid info Hash

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

I keep getting this reponse from the tracker URL:

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:

        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:

        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

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.

Hi charlesmatte,
Can you try without HttpUtility.UrlEncode

1 Like

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.

1 Like

Nice, glad you figured it out!

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