Forum rules
Under no circumstances is spamming or advertising of any kind allowed. Do not post any abusive, obscene, vulgar, slanderous, hateful, threatening, sexually-orientated or any other material that may violate others security. Profanity or any kind of insolent behavior to other members (regardless of rank) will not be tolerated. Remember, what you don’t find offensive can be offensive to other members. Please treat each other with the kind of reverence you’d expect from other members.
Failure to comply with any of the above will result in users being banned without notice. If any further details are needed, contact: “The team” using the link at the bottom of the forum page. Thank you.
jessicamec
Posts: 2
Joined: Wed Oct 11, 2017 2:08 pm

C# What's wrong with my OSDB hash algorithm?

Wed Oct 11, 2017 2:13 pm

I'm trying to write a c# algorithm to get a hash from an online video file to search for subtitles on (https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes)

My idea is that the algorithm is fed a url to a video file, and returns the hash. Simple. Problem is, I'm not getting the right value back. According to the page I've linked to, this file should return 8e245d9679d31e12, but I'm getting 00c4fcb4aa6f763e. Here is my C#:

Code: Select all

public static async Task<byte[]> ComputeMovieHash(string filename)
{
    long filesize = 0;

    //Get File Size
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(filename);
    req.Method = "HEAD";
    var resp = await req.GetResponseAsync();
    filesize = resp.ContentLength;
    long lhash = filesize;

    //Get first 64K bytes
    byte[] firstbytes = new byte[0];
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Range", "bytes=0-65536");
        using (HttpResponseMessage response = await client.GetAsync(filename))
        {
            Debug.WriteLine("getting first bytes (bytes=0-65536)");
            firstbytes = await response.Content.ReadAsByteArrayAsync();
        }
    }
    lhash += BitConverter.ToInt64(firstbytes, 0);

    //Get last 64K bytes
    byte[] lastbytes = new byte[0];
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Range", "bytes=" + (filesize - 65536) + "-" + filesize);
        using (HttpResponseMessage response = await client.GetAsync(filename))
        {
            Debug.WriteLine("getting last bytes (" + "bytes=" + (filesize - 65536) + "-" + filesize + ")");
            lastbytes = await response.Content.ReadAsByteArrayAsync();
        }
    }
    lhash += BitConverter.ToInt64(lastbytes, 0);

    //Return result
    byte[] result = BitConverter.GetBytes(lhash);
    Array.Reverse(result);
    Debug.WriteLine("RESULT=" + ToHexadecimal(result));
    return result;
}

What am I doing wrong?? I've compared it to the code given by opensubtitles.org, and it seems like it should have the same outcome

Return to “Developing”

Who is online

Users browsing this forum: No registered users and 1 guest