jk has asked for the wisdom of the Perl Monks concerning the following question:

Hey, Here is my problem. I want to send GET request to server. This is example:
GET http://some.torrent.tracker/announce.php?info_hash=c%97%91%C5jG%951%BE%C7M%F9%BFa%03%F2%2C%ED%EE%0F& peer_id=S588-----gqQ8TqDeqaY&port=6882&uploaded=0&downloaded=0&left=753690875&event=started
But info_hash part is always different. This is what I want to do:
Convert:
db8babd3f3662166ee9019e29fd7345af8b2b59a
to:
%db%8b%ab%d3%f3f%21f%ee%90%19%e2%9f%d74Z%f8%b2%b5%9a

I found in url below, so I have to encode it using RFC1738 standard.
http://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol
I found one module, which should do what I am trying to do, but I am not sure how to use it (I have compiled it already, just don't know how to use). Here is url:
http://search.cpan.org/~davidnico/Tie-UrlEncoder-0.01/UrlEncoder.pm I would really appreciate for every solution how to encode one hash to other. Regards,
Jerzy.

Replies are listed 'Best First'.
Re: BitTorrent info_hash and Tie::UrlEncoder
by blokhead (Monsignor) on Sep 20, 2007 at 13:16 UTC
    Are you doing anything more than inserting a "%" character before every two characters? It certainly appears that way (plus or minus some typing errors, perhaps). Then there is no need for encoding, just simple string manipulation:
    my $info_hash = "db8babd3f3662166ee9019e29fd7345af8b2b59a"; $info_hash =~ s/(..)/%$1/g;
    This should be recognized just fine by the tracker.

    However, if you really want things like %5A to come out as "Z" instead (since they are characters that are safe to use in a URI), then use URI::Escape to do the escaping. It is part of LWP, so is quite common to have installed. First decode the hash from its printable form back into raw bytes and then use URI::Escape to encode those bytes into a URI-safe string.

    use URI::Escape; my $info_hash = "db8babd3f3662166ee9019e29fd7345af8b2b59a"; $info_hash =~ s/(..)/chr hex $1/ge; # or a suitable call to pack, but I'm lazy right now my $uri_hash = uri_escape($info_hash);
    BTW, if you are computing the info_hash of a bittorrent file, you have the option of getting the result in an printable hex-encoded form "db8b.." or raw bytes "\0xdb\0x8b..." (when you compute the SHA1 hash). You might as well choose to get it in the raw form if you pass it directly to uri_escape.

    blokhead