2013-04-16 101 views
1

由于μTorrent的响应系统在通过磁力链接或者torrent文件发送时的不足之处,也就是说,完全没有重复的torrent添加的消息,我试图在发送它之前从Torrent文件中获取散列并将其与当前作业列表进行比较。我现在的代码正在返回一个不正确的散列,我不知道为什么。 这是我正在使用的代码。SHA-1 Hash of Torrent File in Objective-C

我试图发送一个散列为“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; 
} 

回答

1

什么让你觉得一个BT 信息哈希是在刚刚哈希一个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. 
+0

天真居多。我已经将我的代码编辑为一个函数,我在另一个晚上写了一个函数,当我自己读这个函数时,奇怪的是它不再返回不正确的散列值,它返回许多不正确的散列值之一。我认为图书馆应该受到指责。 –

+1

这很可能是24,而不是30左右。 Bencode需要对字典键进行排序。 – pyroscope

+0

该库使用已排序的字典类型,但是当我将它打印到屏幕上时,它会使用随机字符代替某些数字。 –