【问题标题】:Issue With SHA1 Hash of Torrent File in Objective-CObjective-C 中 Torrent 文件的 SHA1 哈希问题
【发布时间】:2013-04-16 14:14:16
【问题描述】:

由于 µTorrent 的响应系统在通过磁力链接或 torrent 文件发送时的不足,即完全没有重复添加 torrent 的消息,我试图在发送之前从 torrent 文件中获取哈希并将其与当前工作列表进行比较。我目前拥有的代码返回了一个不正确的哈希值,我不知道为什么。 这是我正在使用的代码。

我正在尝试通过散列为“dc9202f98aea7420a2872655c8f7184401e2a9c8”的文件发送,此代码每次运行时都会返回大约 30 个散列中的一个。

+ (NSString *) torrentHashFromFile:(NSData *)file
{
    NSString * retVal = @"";

    NSData * data = [BEncoding encodedDataFromObject:
                     [[BEncoding objectFromEncodedData:file] 
                      objectForKey:@"info"]];

    unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH];

    if (CC_SHA1([data bytes], (unsigned)[data length], hashBytes))
    {
        NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

        for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        {
            [output appendFormat:@"%02x", hashBytes[i]];
        }

        retVal = output;
    }

    return retVal;
}

【问题讨论】:

    标签: objective-c hash sha1 bittorrent utorrent


    【解决方案1】:

    是什么让您认为 BT info 散列只是 piece 散列的 SHA1?

    引用BEP-0003

    info_hash
        The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file. 
        Note that this is a substring of the metainfo file. 
    

    【讨论】:

    • 天真,主要是。我已经将我的代码编辑为我在前几天晚上自己阅读时编写的函数,奇怪的是它不再返回不正确的哈希值,而是返回许多不正确的哈希值之一。我认为图书馆是罪魁祸首。
    • 很可能正好是 24 岁,而不是 30 岁左右。 Bencode 需要对 dict 键进行排序。
    • 该库使用排序字典类型,但是当我将其打印到屏幕上时,它会用随机字符代替一些数字。
    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多