Jump to content

Torrent Info Hash Encoding


Recommended Posts

I would like to programmatically check to see if a given tracker has information on the torrent I specify. This requires that the SHA-1 Info Hash of a torrent be encoded to make valid requests. I read from: http://nakkaya.com/2009/12/03/bittorrent-tracker-protocol/ If you don't pay attention to the spec and send this directly to tracker you will get an error this should be in URL Encoded form. Padding every two chars with % sign also doesn't work, been there done that don't waste your time. Any hex in the hash that corresponds to a unreserved character should be replaced,

a-z A-Z 0-9 -_.~

Partition the hex in to chunks of two and check if the hex corresponds to any of these values, if they do replace them with the unreserved char.

So that a hash such as,

123456789abcdef123456789abcdef123456789a

becomes,

%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a

notice that hex 34 became 4 which is what it is in ASCII. You can test the correctness of your hashes using the tracker url but don't request from announce request from file,

http://some.tracker.com/file?info_hash=hash

If you get a torrent back that means you have the correct hash.

I have tried the above and failed. Here's my code:

$testStr = "123456789abcdef123456789abcdef123456789a"
ConsoleWrite(_EncodeHash($testStr) & @CRLF)
Func _EncodeHash($sString)
    If (Not IsString($sString)) Or $sString = "" Then Return SetError(1, 0, 0)
    Local $aArray = StringRegExp($sString, "(?s).{1," & 2 & "}", 3), $sEncodedHash
For $i = 0 To UBound($aArray) -1
    If StringInStr($sString, Chr(Dec($aArray[$i]))) Then
     $sEncodedHash &= Chr(Dec($aArray[$i]))
    Else
     $sEncodedHash &= "%" & $aArray[$i]
    EndIf
    Next
Return $sEncodedHash
EndFunc

If you follow the first link in my post there is an example but I'm unfamiliar with the language being used. Thanks for any help.

Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

Hey JohnOne, you're absolutely correct. I consider myself to be an important individual. I would usually have something negative to say but I'm being humble. I'll wait until after I've made six thousand posts and never decide to suggest not bumping threads unless the OP feels important. Thanks for your first suggestion. I can't wait to endure number two but I'd rather stay on topic. ;)

Spoiler

censored.jpg

 

Link to comment
Share on other sites

Decipher,

This is forum etiquette. Plus the support you get is from people who want to help not have to help, so learn how to be patient.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I just took this from the spec:

Note that all binary data in the URL (particularly info_hash and peer_id) must be properly escaped. This means any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', must be encoded using the "%nn" format, where nn is the hexadecimal value of the byte. (See RFC1738 for details.)

For a 20-byte hash of x12x34x56x78x9axbcxdexf1x23x45x67x89xabxcdxefx12x34x56x78x9a,

The right encoded form is %124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A

I think I realize what I'm doing wrong....... I'm still waiting for help guies and no, I'm not trying to bump this thread it is relevant information.

Spoiler

censored.jpg

 

Link to comment
Share on other sites

  • Moderators

Decipher,

I do not like the tone you have taken so far in this thread - please alter it in future. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yes ma'am, I have solved my problem by myself. Heres the code:

#include <array.au3>
$INFOHASH = "123456789abcdef123456789abcdef123456789a"
ConsoleWrite(_EncodeHash($INFOHASH) & @CRLF)
Func _EncodeHash($sString)
    If (Not IsString($sString)) Or $sString = "" Then Return SetError(1, 0, 0)
    Local $aArray = StringRegExp($sString, "(?s).{1," & 2 & "}", 3), $sEncodedHash
For $i = 0 To UBound($aArray) -1
    If StringInStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~", Chr(Dec($aArray[$i]))) Then
     $sEncodedHash &= Chr(Dec($aArray[$i]))
    Else
     $sEncodedHash &= "%" & $aArray[$i]
    EndIf
    Next
Return $sEncodedHash
EndFunc
;%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a
;%124Vx%9a%bc%de%f1%23Eg%89%ab%cd%ef%124Vx%9a

Would you mind telling exactly how long I shall wait in the future before bumping, if it is even allowed.

Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

  • Moderators

Decipher,

I am a "Sir", thank you! ;)

Like most public forums we prefer you not to bump your own thread within 24 hours. Remember this is not a 24/7 support forum - those who answer are only here because they like helping others and have some time to spare. You just have to wait until someone who knows something about your particular problem, and is willing to help, comes online. Be patient and someone will answer eventually. Or if not - tough! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I think you should wait atleast a day before bumping.

Some people have a thing called "A life", you've probably heard of it, and therefore can't be online all the time.

Post, wait, and if nothing, bump after 24 hours.

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

I think you should wait atleast a day before bumping.

Some people have a thing called "A life", you've probably heard of it, and therefore can't be online all the time.

Post, wait, and if nothing, bump after 24 hours.

@Maffe811,

Seriously, You have to bored out of your mind taking the time out of your precious life to say what has been said once than again by a mod.

Sorry Melba32 I had to tell this poor guy.

Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

Well, not really, i was reading the topic and was quite entertained by your behaviour and i took my time even finding some snacks.

So when i finally got arround to post a reply, some had beaten me to it.

But from the post you made answearing JohnOne i was just waiting for you to step over the line and get thrown out.

You should read the forum rules, they mention:

Do not be obnoxious, rude or in general a nuisance to the smooth operation of the forum.

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...