Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions vendor/go.felesatra.moe/hash/ed2k/ed2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ func (h *Hash) limitNextChunk(b []byte) []byte {
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
func (h *Hash) Sum(b []byte) []byte {
if h.written < ChunkSize {
if h.written == 0 {
return h.subhash.Sum(b)
}
if h.written == ChunkSize {

// If there's an uncommitted chunk, hash it and add to the hashlist
if h.written%ChunkSize != 0 {
h.hashlist = h.subhash.Sum(h.hashlist)
}

// If the total written data is less than or equal to one chunk,
// the hashlist already contains the final hash (or the hash of the single chunk).
if h.written <= ChunkSize {
return append(b, h.hashlist...)
}

// For multiple chunks, hash the hashlist itself.
h2 := md4.New()
h2.Write(h.hashlist)
return h2.Sum(b)
Expand Down