aboutsummaryrefslogtreecommitdiff
path: root/packfile/ingest/hash.go
blob: bb8fd2b4fc7acc64cf0486a350fe993fa5f9978c (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package ingest

import (
	"fmt"

	objectheader "codeberg.org/lindenii/furgit/object/header"
	"codeberg.org/lindenii/furgit/objectid"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

// hashCanonicalObject hashes canonical object bytes (header+content).
func hashCanonicalObject(algo objectid.Algorithm, ty objecttype.Type, content []byte) (objectid.ObjectID, error) {
	header, ok := objectheader.Encode(ty, int64(len(content)))
	if !ok {
		return objectid.ObjectID{}, fmt.Errorf("packfile/ingest: encode object header for type %d", ty)
	}

	hashImpl, err := algo.New()
	if err != nil {
		return objectid.ObjectID{}, err
	}

	_, _ = hashImpl.Write(header)
	_, _ = hashImpl.Write(content)

	return objectid.FromBytes(algo, hashImpl.Sum(nil))
}