1
// Copyright 2023 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
13
// sha1Pattern can be used to determine if a string is an valid sha
14
var sha1Pattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
16
// sha256Pattern can be used to determine if a string is an valid sha
17
var sha256Pattern = regexp.MustCompile(`^[0-9a-f]{4,64}$`)
19
type ObjectFormat interface {
20
// Name returns the name of the object format
22
// EmptyObjectID creates a new empty ObjectID from an object format hash name
23
EmptyObjectID() ObjectID
24
// EmptyTree is the hash of an empty tree
26
// FullLength is the length of the hash's hex string
28
// IsValid returns true if the input is a valid hash
29
IsValid(input string) bool
30
// MustID creates a new ObjectID from a byte slice
31
MustID(b []byte) ObjectID
32
// ComputeHash compute the hash for a given ObjectType and content
33
ComputeHash(t ObjectType, content []byte) ObjectID
37
type Sha1ObjectFormatImpl struct{}
40
emptySha1ObjectID = &Sha1Hash{}
41
emptySha1Tree = &Sha1Hash{
42
0x4b, 0x82, 0x5d, 0xc6, 0x42, 0xcb, 0x6e, 0xb9, 0xa0, 0x60,
43
0xe5, 0x4b, 0xf8, 0xd6, 0x92, 0x88, 0xfb, 0xee, 0x49, 0x04,
47
func (Sha1ObjectFormatImpl) Name() string { return "sha1" }
48
func (Sha1ObjectFormatImpl) EmptyObjectID() ObjectID {
49
return emptySha1ObjectID
52
func (Sha1ObjectFormatImpl) EmptyTree() ObjectID {
55
func (Sha1ObjectFormatImpl) FullLength() int { return 40 }
56
func (Sha1ObjectFormatImpl) IsValid(input string) bool {
57
return sha1Pattern.MatchString(input)
60
func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID {
66
// ComputeHash compute the hash for a given ObjectType and content
67
func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
69
_, _ = hasher.Write(t.Bytes())
70
_, _ = hasher.Write([]byte(" "))
71
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
72
_, _ = hasher.Write([]byte{0})
74
// HashSum generates a SHA1 for the provided hash
76
copy(sha1[:], hasher.Sum(nil))
81
type Sha256ObjectFormatImpl struct{}
84
emptySha256ObjectID = &Sha256Hash{}
85
emptySha256Tree = &Sha256Hash{
86
0x6e, 0xf1, 0x9b, 0x41, 0x22, 0x5c, 0x53, 0x69, 0xf1, 0xc1,
87
0x04, 0xd4, 0x5d, 0x8d, 0x85, 0xef, 0xa9, 0xb0, 0x57, 0xb5,
88
0x3b, 0x14, 0xb4, 0xb9, 0xb9, 0x39, 0xdd, 0x74, 0xde, 0xcc,
93
func (Sha256ObjectFormatImpl) Name() string { return "sha256" }
94
func (Sha256ObjectFormatImpl) EmptyObjectID() ObjectID {
95
return emptySha256ObjectID
98
func (Sha256ObjectFormatImpl) EmptyTree() ObjectID {
99
return emptySha256Tree
101
func (Sha256ObjectFormatImpl) FullLength() int { return 64 }
102
func (Sha256ObjectFormatImpl) IsValid(input string) bool {
103
return sha256Pattern.MatchString(input)
106
func (Sha256ObjectFormatImpl) MustID(b []byte) ObjectID {
112
// ComputeHash compute the hash for a given ObjectType and content
113
func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
114
hasher := sha256.New()
115
_, _ = hasher.Write(t.Bytes())
116
_, _ = hasher.Write([]byte(" "))
117
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
118
_, _ = hasher.Write([]byte{0})
120
// HashSum generates a SHA256 for the provided hash
122
copy(sha256[:], hasher.Sum(nil))
127
Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{}
128
Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{}
131
var SupportedObjectFormats = []ObjectFormat{
135
func ObjectFormatFromName(name string) ObjectFormat {
136
for _, objectFormat := range SupportedObjectFormats {
137
if name == objectFormat.Name() {
144
func IsValidObjectFormat(name string) bool {
145
return ObjectFormatFromName(name) != nil