From 32f84b9d89d34ab74f8843f939f8d1d811d403ad Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 29 Mar 2026 10:14:38 +0000 Subject: object/id: Split files --- object/id/algorithm_tables.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 object/id/algorithm_tables.go (limited to 'object/id/algorithm_tables.go') diff --git a/object/id/algorithm_tables.go b/object/id/algorithm_tables.go new file mode 100644 index 00000000..86e1341e --- /dev/null +++ b/object/id/algorithm_tables.go @@ -0,0 +1,63 @@ +package objectid + +import ( + "crypto/sha1" + "crypto/sha256" +) + +//nolint:gochecknoglobals +var algorithmTable = [...]algorithmDetails{ + AlgorithmUnknown: {}, + AlgorithmSHA1: { + name: "sha1", + size: sha1.Size, + packHashID: 1, + sum: func(data []byte) ObjectID { + sum := sha1.Sum(data) //#nosec G401 + + var id ObjectID + copy(id.data[:], sum[:]) + id.algo = AlgorithmSHA1 + + return id + }, + new: sha1.New, + }, + AlgorithmSHA256: { + name: "sha256", + size: sha256.Size, + packHashID: 2, + sum: func(data []byte) ObjectID { + sum := sha256.Sum256(data) + + var id ObjectID + copy(id.data[:], sum[:]) + id.algo = AlgorithmSHA256 + + return id + }, + new: sha256.New, + }, +} + +var ( + //nolint:gochecknoglobals + algorithmByName = map[string]Algorithm{} + //nolint:gochecknoglobals + supportedAlgorithms []Algorithm +) + +func init() { //nolint:gochecknoinits + emptyTreeInput := []byte("tree 0\x00") + + for algo := Algorithm(0); int(algo) < len(algorithmTable); algo++ { + info := &algorithmTable[algo] + if info.name == "" { + continue + } + + info.emptyTree = info.sum(emptyTreeInput) + algorithmByName[info.name] = algo + supportedAlgorithms = append(supportedAlgorithms, algo) + } +} -- cgit v1.3.1-10-gc9f91