aboutsummaryrefslogtreecommitdiff
path: root/objectstore/packed/pack_idx_checksum.go
blob: 28d4c3dbf7d8b14522e39b85a7e84e810af3574e (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
28
29
30
31
32
33
34
package packed

import (
	"bytes"
	"fmt"

	objectid "codeberg.org/lindenii/furgit/object/id"
)

// verifyMappedPackMatchesMappedIdx compares one mapped pack trailer hash with
// the pack hash recorded in one mapped idx trailer.
func verifyMappedPackMatchesMappedIdx(packData, idxData []byte, algo objectid.Algorithm) error {
	hashSize := algo.Size()
	if hashSize <= 0 {
		return objectid.ErrInvalidAlgorithm
	}

	if len(packData) < hashSize {
		return fmt.Errorf("objectstore/packed: pack too short for trailer hash")
	}

	if len(idxData) < hashSize*2 {
		return fmt.Errorf("objectstore/packed: idx too short for trailer hashes")
	}

	packTrailerHash := packData[len(packData)-hashSize:]

	idxPackHash := idxData[len(idxData)-hashSize*2 : len(idxData)-hashSize]
	if !bytes.Equal(packTrailerHash, idxPackHash) {
		return fmt.Errorf("objectstore/packed: pack hash does not match idx")
	}

	return nil
}