gpt4 book ai didi

objective-c - 将 Torrent info_hash 从 bencoded 数据转换为 URLEncoded 数据

转载 作者:可可西里 更新时间:2023-11-01 03:56:17 24 4
gpt4 key购买 nike

我正在用 objective-c 创建 torrent scraper,我正在使用 AFNetworking对于 HTTP 请求。我需要为跟踪器请求发送元信息部分的 sha1 散列。我已成功创建哈希并验证它是正确的。我不能将散列放在 NSString 中,因为它不对二进制数据进行编码,所以我将它放在 NSData 对象中,然后放在要发送的参数中。这就是我现在所拥有的,但我总是得到一个错误,我会假设它是我用来发送哈希的方法。我也尝试过对哈希进行 url 编码,然后将其放入 NSString 中,但无济于事

 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);
}];

有谁知道是否有可能使用 AFNetworking 执行此操作,或者我可能希望遗漏一些简单的东西。

最佳答案

好吧,我不是种子专家,但我认为您面临的挑战是您从中提取的编码数据表示将 SHA1 哈希编码为 20 个原始字节数据。

您不能像 HTTP 请求的 GET 部分那样发送原始数据,因此您需要对其进行编码。根据规范,urlencoding 原始数据似乎是这样做的官方方式。 (例如,其他选项是将其转换为人类可读的十六进制字符串)。

从这个 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

从原始的 20 字符 C 数组开始,您需要得到一个 URLEncoded NSString

不幸的是,特别是对于这种类型的数据,简单的方法 - 在循环中使用 stringByAddingPercentEscapesUsingEncoding - 将不起作用,因为它不会以安全编码作为 GET 参数发送。

有一点 S.O.帮助 ( How do I URL encode a string ),这里有一些代码,您可以将其塑造成最终产品,但符合正确输出的规范文档:

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);

输出是:

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

祝你好运!

关于objective-c - 将 Torrent info_hash 从 bencoded 数据转换为 URLEncoded 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com