diff options
| author | 2026-03-29 10:14:38 +0000 | |
|---|---|---|
| committer | 2026-03-29 10:17:15 +0000 | |
| commit | 32f84b9d89d34ab74f8843f939f8d1d811d403ad (patch) | |
| tree | 90786209ddd050ad2676a7d0404b78d8d278e1f2 /object/id/algorithm_tables.go | |
| parent | object/{id,header,signature,type}: Update docs (diff) | |
| signature | No signature | |
object/id: Split files
Diffstat (limited to 'object/id/algorithm_tables.go')
| -rw-r--r-- | object/id/algorithm_tables.go | 63 |
1 files changed, 63 insertions, 0 deletions
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) + } +} |
