aboutsummaryrefslogtreecommitdiff
path: root/object/id/object_format_details.go
blob: d0278602b042c32192acbc91324e04e8e6507dd9 (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
package id

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

type objectFormatDetails struct {
	name string
	size int
	sum  func([]byte) ObjectID
	new  func() hash.Hash
}

func (objectFormat ObjectFormat) details() objectFormatDetails {
	return objectFormatTable[objectFormat]
}

//nolint:gochecknoglobals
var objectFormatTable = [...]objectFormatDetails{
	ObjectFormatUnknown: {}, //nolint:exhaustruct
	ObjectFormatSHA1: {
		name: "sha1",
		size: sha1.Size,
		sum: func(data []byte) ObjectID {
			sum := sha1.Sum(data) //#nosec G401

			var id ObjectID
			copy(id.data[:], sum[:])
			id.objectFormat = ObjectFormatSHA1

			return id
		},
		new: sha1.New,
	},
	ObjectFormatSHA256: {
		name: "sha256",
		size: sha256.Size,
		sum: func(data []byte) ObjectID {
			sum := sha256.Sum256(data)

			var id ObjectID
			copy(id.data[:], sum[:])
			id.objectFormat = ObjectFormatSHA256

			return id
		},
		new: sha256.New,
	},
}

// maxObjectIDSize MUST be >= the largest supported object format size.
const maxObjectIDSize = sha256.Size

var (
	//nolint:gochecknoglobals
	objectFormatByName = map[string]ObjectFormat{}
	//nolint:gochecknoglobals
	supportedObjectFormats []ObjectFormat
)

func init() { //nolint:gochecknoinits
	for objectFormat := ObjectFormatUnknown + 1; int(objectFormat) < len(objectFormatTable); objectFormat++ {
		info := &objectFormatTable[objectFormat]
		objectFormatByName[info.name] = objectFormat
		supportedObjectFormats = append(supportedObjectFormats, objectFormat)
	}
}