ios - Objective-C iOS 从 Torrent 文件生成 Info_Hash

标签 ios objective-c torrent

我目前正在努力尝试从我下载的 torrent 文件中生成 info_hash。我使用 uTorrent 并使用 PeerTracker 作为我的跟踪软件创建了 Torrent 文件。

我使用的是此处代码的更新版本:
https://code.google.com/p/rouge-server/source/browse/trunk/driver/objective-c/RougeDriver/BEncoding.m?r=88

{
    announce = "http://sub.url.com:8080/announce.php";
    "created by" = "uTorrent/1870";
    "creation date" = 1425134140;
    encoding = "UTF-8";
    info =     {
        length = 4;
        name = "test.txt";
        "piece length" = 16384;
        pieces = "\U00a9J\U00e8\U00c2\U00c3\U00b1\U00f5\U00b6L\bs\U201d\U00eb\U00c8\U00e1\U00f2/\U00aa\U201d";
    };
}

下面是我使用的确切编码方法:

+ (void)encode:(id)object toString:(NSMutableString *)string
{
    if([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dictionary = (NSDictionary *)object;
        [string appendString:@"d"];

        for(id key in [dictionary allKeys])
        {
            [self encode:key toString:string];
            [self encode:[dictionary objectForKey:key] toString:string];
        }

        [string appendString:@"e"];
    }
    else if([object isKindOfClass:[NSString class]])
    {
        NSString *stringObject = (NSString *)object;
        NSString *format = @"%lu:%@";

        [string appendFormat:format, [string length], stringObject];
    }
    else if([object isKindOfClass:[NSArray class]])
    {
        NSArray *array = (NSArray *)object;
        [string appendString:@"l"];

        for(id item in array)
        {
            [self encode:item toString:string];
        }

        [string appendString:@"e"];
    }
    else if([object isKindOfClass:[NSNumber class]])
    {
        NSNumber *number = (NSNumber *)object;
        NSString *format = @"i%de";

        [string appendFormat:format, [number intValue]];
    }
}

这是我第一次使用 Torrents 和编码。我知道我只应该对主词典中包含的“信息”词典进行编码。

有谁知道我这里可能哪里出错了?

最佳答案

实际的片段内容是数据:

0xa9, 0x4a, 0x8f, 0xe5, 0xcc, 0xb1, 0x9b, 0xa6, 0x1c, 0x4c, 0x08, 0x73, 0xd3, 0x91, 0xe9, 0x87, 0x98, 0x2f, 0xbb, 0xd3

正如@Encombe 所述,这是 SHA1 哈希。

来自维基百科:

The specification does not deal with encoding of characters outside the ASCII set; to mitigate this, some BitTorrent applications explicitly communicate the encoding (most commonly UTF-8) in various non-standard ways.

pieces 是十六进制数据,不是 UTF-8。

关于ios - Objective-C iOS 从 Torrent 文件生成 Info_Hash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28783068/

相关文章:

objective-c - 如何 NSLog 一个 CGRect

libtorrent - python libtorrent 如何从 info_hash 获取 torrent 文件列表?

ios - 使用核心数据完全不相关的错误

ios - UILabel:背景相关颜色

ios - 影片开始播放后,从UIWebView播放youtube影片时当机

objective-c - OSX/ cocoa : setting up service in menu for right click and services list

tor - Tor 上的配置泛滥

c++ - 是否可以禁用p2p并仅在libtorrent中使用HTTPSeed

ios - 控制台输出中的 lldb 错误,无法获得正确的结果

iphone - 如何在IOS中将xml文档转换为NSString