Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: BitTorrent info_hash and Tie::UrlEncoder

by blokhead (Monsignor)
on Sep 20, 2007 at 13:16 UTC ( [id://640113]=note: print w/replies, xml ) Need Help??


in reply to BitTorrent info_hash and Tie::UrlEncoder

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://640113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found