aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/internal/ingest/hash.go
diff options
context:
space:
mode:
Diffstat (limited to 'object/store/packed/internal/ingest/hash.go')
-rw-r--r--object/store/packed/internal/ingest/hash.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/object/store/packed/internal/ingest/hash.go b/object/store/packed/internal/ingest/hash.go
new file mode 100644
index 00000000..4b739c20
--- /dev/null
+++ b/object/store/packed/internal/ingest/hash.go
@@ -0,0 +1,27 @@
+package ingest
+
+import (
+ "fmt"
+
+ objectheader "codeberg.org/lindenii/furgit/object/header"
+ objectid "codeberg.org/lindenii/furgit/object/id"
+ 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))
+}