aboutsummaryrefslogtreecommitdiff
path: root/object/id/algorithm_tables.go
blob: e4ec3257dfcd3e7d568e59eb8a108ab64fbc8afb (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package objectid

import (
	"crypto/sha1" //#nosec:G505
	"crypto/sha256"
)

//nolint:gochecknoglobals
var algorithmTable = [...]algorithmDetails{
	AlgorithmUnknown: {},
	AlgorithmSHA1: {
		name:                "sha1",
		size:                sha1.Size,
		packHashID:          1,
		signatureHeaderName: "gpgsig",
		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,
		signatureHeaderName: "gpgsig-sha256",
		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
	algorithmBySignatureHeaderName = 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
		if info.signatureHeaderName != "" {
			algorithmBySignatureHeaderName[info.signatureHeaderName] = algo
		}

		supportedAlgorithms = append(supportedAlgorithms, algo)
	}
}