ED2K Hash Algorithms

XP105

Member
Joined
Jan 19, 2010
Messages
7
Location
England, UK
Programming Experience
1-3
Hi,
I'm working on a program that will require me to get the ED2K Hash of a file. I know this involves splitting the file into 9500 KiB chunks then running an MD4 Hash on each chunk (including the remainder chunk) then MD4 Hashing the result. The problem is, I have little/no experience with either MD4, breaking files, or even encryption (although I have done successful CRC32 and MD5 Checksums). Can anyone help?
 
Last edited by a moderator:
I found this MD4 class: VB.net MD4 Message Digest class :: Snippet :: lucamauri.com
It it extremely slow, so it is likely something could be improved in the algorithm. Since MD4 "Process Message in 16-Word Blocks" (RFC1320) in source up until padding, there could also be memory enhancements in providing stream based reading.

Anyway, the only reference I've found for ed2k is ed2k URI scheme - Wikipedia, the free encyclopedia. For files smaller than the chunk size the hash is same as md4:
VB.NET:
Dim md4 As New MD4Hash.MD4
Dim hash As String = md4.GetHexHashFromBytes(IO.File.ReadAllBytes("D:\path\file.ext")).ToLower
For larger files you can use FileStream Class (System.IO) and read the chunks:
VB.NET:
Dim md4 As New MD4Hash.MD4
Dim file = "d:\path\file.ext"
Dim s As IO.FileStream = IO.File.OpenRead(file)
Dim hashes As New List(Of Byte)
Dim read As Integer = -1
Do Until read = 0
    Dim tmp(9728000 - 1) As Byte
    read = s.Read(tmp, 0, tmp.Length)
    If read = tmp.Length Then
        hashes.AddRange(md4.GetByteHashFromBytes(tmp))
    ElseIf read > 0 Then
        Array.Resize(tmp, read)
        hashes.AddRange(md4.GetByteHashFromBytes(tmp))
    End If
Loop
s.Close()
hash = md4.GetHexHashFromBytes(hashes.ToArray).ToLower
One thing that is not clear is the "If the file length is an exact multiple of 9500 KiB, the remainder zero size chunk is still used at the end of the hash list" part. I have no idea what "zero size chunk" means, adding md4 of a zero size array does not add up.

A faster and simpler solution is to download the ed2k tools linked in Wiki article and automate the console tool:
VB.NET:
Dim info As New ProcessStartInfo
info.FileName = "C:\Program Files\Ed2k_hash\ed2k_hash.exe"
info.Arguments = """d:\path\file.ext"""
info.UseShellExecute = False
info.CreateNoWindow = True
info.RedirectStandardOutput = True
Dim p As Process = Process.Start(info)
Dim link As String = p.StandardOutput.ReadLine
p.Dispose()
Dim parts() As String = link.Split("|"c)
Dim hash As String = parts(parts.Length - 2)
 
The first method really did take a long time (I waited 10 mins, and it was still working on it) whereas the result with the ed2k hash was almost instantaneous (and definitely accurate).
 
Back
Top