Skip to content

The ed2k hash calculation result differ from other commonly used tools #25

@nanoia

Description

@nanoia

The calculation results do not match those obtained using other tools such as Amule , emule LinkCreator. ChatGPT says Write() will never handle “that last incomplete chunk.”

func (h *Hash) Write(p []byte) (int, error) {
total := len(p)
for len(p) > 0 {
p2 := h.limitNextChunk(p)
n, _ := h.subhash.Write(p2)
h.written += int64(n)
p = p[n:]
if h.written%ChunkSize == 0 {
h.hashlist = h.subhash.Sum(h.hashlist)
h.subhash.Reset()
}
}
return total, nil
}

func (h *Hash) Sum(b []byte) []byte {
if h.written < ChunkSize {
return h.subhash.Sum(b)
}
if h.written == ChunkSize {
return append(b, h.hashlist...)
}
h2 := md4.New()
h2.Write(h.hashlist)
return h2.Sum(b)
}

// Provided by ChatGPT
func (h *Hash) Sum(b []byte) []byte {
	if h.written == 0 {
		return h.subhash.Sum(b)
	}

	// uncommited chunk
	if h.written%ChunkSize != 0 {
		h.hashlist = h.subhash.Sum(h.hashlist)
	}

	if h.written <= ChunkSize {
		return append(b, h.hashlist...)
	}

	h2 := md4.New()
	h2.Write(h.hashlist)
	return h2.Sum(b)
}

Reference:
https://wiki.anidb.net/Ed2k-hash#How_is_an_ed2k_hash_calculated_exactly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions