Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / PHP
Tip/Trick

Magnet Link

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
5 Jan 2010CPOL 21.2K   1   2
This PHP function return the urn (Uniform Resource Name) formed from the content hash of a particular torrent file. The urn refering to the Base32 encoded hash of the file.function base32_encode ($hash){$outString = '';$compBits = '';$BASE32_TABLE = array( ...
This PHP function return the urn (Uniform Resource Name) formed from the content hash of a particular torrent file. The urn refering to the Base32 encoded hash of the file.

function base32_encode ($hash)
{
$outString = '';
$compBits = '';
$BASE32_TABLE = array(
                         '00000' => 0x61,
                         '00001' => 0x62,
                         '00010' => 0x63,
                         '00011' => 0x64,
                         '00100' => 0x65,
                         '00101' => 0x66,
                         '00110' => 0x67,
                         '00111' => 0x68,
                         '01000' => 0x69,
                         '01001' => 0x6a,
                         '01010' => 0x6b,
                         '01011' => 0x6c,
                         '01100' => 0x6d,
                         '01101' => 0x6e,
                         '01110' => 0x6f,
                         '01111' => 0x70,
                         '10000' => 0x71,
                         '10001' => 0x72,
                         '10010' => 0x73,
                         '10011' => 0x74,
                         '10100' => 0x75,
                         '10101' => 0x76,
                         '10110' => 0x77,
                         '10111' => 0x78,
                         '11000' => 0x79,
                         '11001' => 0x7a,
                         '11010' => 0x32,
                         '11011' => 0x33,
                         '11100' => 0x34,
                         '11101' => 0x35,
                         '11110' => 0x36,
                         '11111' => 0x37,
                         );

   /* Turn the compressed string into a string that represents the bits as 0 and 1. */
   for ($i = 0; $i < strlen($hash); $i++) {
       $compBits .= str_pad(decbin(ord(substr($hash,$i,1))), 8, '0', STR_PAD_LEFT);
   }

   /* Pad the value with enough 0's to make it a multiple of 5 */
   if((strlen($compBits) % 5) != 0) {
       $compBits = str_pad($compBits, strlen($compBits)+(5-(strlen($compBits)%5)), '0', STR_PAD_RIGHT);
   }

   /* Create an array by chunking it every 5 chars */
   $fiveBitsArray = split("\n",rtrim(chunk_split($compBits, 5, "\n")));

   /* Look-up each chunk and add it to $outstring */
   foreach($fiveBitsArray as $fiveBitsString) {
       $outString .= chr($BASE32_TABLE[$fiveBitsString]);
   }

   return $outString;

}


Parametres:

dn - Filename

xl - Size in bytes

xt - urn containing file hash

as - Web link to the file online

xs - P2P link

kt - Key words for search

mt - link to the metafile that contains a list of magneto

xt is the most important part of magnet links.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Serbia Serbia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralArray Pin
zafar j15-May-10 21:55
zafar j15-May-10 21:55 
GeneralMy vote of 2 Pin
Tusar ranjan Patra21-Apr-10 0:37
Tusar ranjan Patra21-Apr-10 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.