【发布时间】: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