Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:07:57+00:00 2026-06-09T13:07:57+00:00

I am creating torrent scraper in objective-c and I am using AFNetworking for the

  • 0

I am creating torrent scraper in objective-c and I am using AFNetworking for the HTTP requests. I need to send a sha1 hash of the part of the meta info for a tracker request. I have successfully created the hash and verified that it is correct. I can not put the hash in an NSString since it does not encode binary data so I have put it in an NSData object and then in the parameters to send. This is what I have right now, but I always get an error back, and I would assume it is with the methods I am using to send the hash. I have also tried url encoding the hash and then putting it in an NSString with no avail

 NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

 unsigned char infoHash[20];
 [self.tracker generateTorrentInfoHash:infoHash];

 const char peer_id[20] = "randomstringfornow";

[parameters setObject:[NSData dataWithBytes:&infoHash length:20] forKey:@"info_hash"];    
[parameters setObject:[NSData dataWithBytes:&peer_id length:20] forKey:@"peer_id"];
[parameters setObject:@(8080) forKey:@"port"];
[parameters setObject:@(0) forKey:@"uploaded"];
[parameters setObject:@(self.tracker.metaInfo.totalFileSize) forKey:@"left"];
[parameters setObject:@(0) forKey:@"downloaded"];
[parameters setObject:@(0) forKey:@"compact"];
[parameters setObject:@"stopped" forKey:@"event"];


[self getPath:@"" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@",operation.responseString);


   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",operation.responseString);
}];

Does anyone know if there is a possible way of doing this with AFNetworking or maybe I am hopefully missing something simple.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:07:58+00:00Added an answer on June 9, 2026 at 1:07 pm

    Alright, I’m not an expert at torrents, but I think the challenge you’re having is that the bencoded data representation you’re pulling from encodes the SHA1 hash as 20 raw bytes of data.

    You can’t send raw data like that in the GET portion of an HTTP request, so you’re going to need to encode it. Based on the specs, it appears that urlencoding the raw data is the official way to do so. (Other options would be to convert it to a human-readable hex string, for example).

    From this Alternative Bittorrent Specification:

    The tracker is an HTTP/HTTPS service which responds to HTTP GET requests. The requests include metrics from clients that help the tracker keep overall statistics about the torrent. The response includes a peer list that helps the client participate in the torrent. The base URL consists of the “announce URL” as defined in the metainfo (.torrent) file. The parameters are then added to this URL, using standard CGI methods (i.e. a ‘?’ after the announce URL, followed by ‘param=value’ sequences separated by ‘&’).

    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 \x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a

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

    Starting with your raw 20 char C array, you need to get to a URLEncoded NSString.

    Unfortunately, especially with this type of data, the simple method – using stringByAddingPercentEscapesUsingEncoding in a loop – won’t work because it does not end up with a safe encoding to send as a GET parameter.

    With a bit of S.O. help ( How do I URL encode a string ), here is a bit of code that you can mold into the final product, but meets the spec doc for correct output:

    const char source[20] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x9a};
    
    NSMutableString *output = [NSMutableString string];
    
    NSLog(@"Raw Data: %@", [NSData dataWithBytes:source length:20]);
    
    int sourceLen = sizeof(source);
    
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = source[i];
        if (thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    
    NSLog(@"Encoded:  %@", output);
    

    Output is:

    Raw Data: <12345678 9abcdef1 23456789 abcdef12 3456789a>
    Encoded:  %124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating an torrent application using libtorrent in VS 2008. I tried the
Creating a zip file using Send To -> Compressed folder excludes .hg folder on
Creating a simple RPG game, first time using XNA. Trying to get my character
I creating a web application using JSF,Hibernate,Spring. I have added a filter for checking
I'm playing with an idea of creating client that would use the torrent protocol
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating sprite sheets aka texture atlases rather than using many hundres of individual images
Creating a site in with FrogCMS and using alot of jquery to create an
Creating the closure is easy but using it is confusing for me. Here is
Creating a widget for my site. Check it out at http://bit.ly/w42SC4 . Looks good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.