Question

Due to the inadequacies of µTorrent's response system when sending through either magnet links or torrent files, that is, the complete lack of a message for duplicate torrent adding, I am attempting to get the hash from a torrent file before it's sent over and comparing that with the list of current jobs. The code I currently have is returning an incorrect hash and I haven't a clue why. Here is the code I'm using.

I'm attempting to send through a file with a hash of "dc9202f98aea7420a2872655c8f7184401e2a9c8", this code is returning one of thirty or so hashes every time it's run.

+ (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;
}
Was it helpful?

Solution

What makes you think a BT info hash is a SHA1 over just the piece hashes?

Quoting 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. 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top