gitech

Форк
0
/
object_format.go 
146 строк · 3.9 Кб
1
// Copyright 2023 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
3

4
package git
5

6
import (
7
	"crypto/sha1"
8
	"crypto/sha256"
9
	"regexp"
10
	"strconv"
11
)
12

13
// sha1Pattern can be used to determine if a string is an valid sha
14
var sha1Pattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
15

16
// sha256Pattern can be used to determine if a string is an valid sha
17
var sha256Pattern = regexp.MustCompile(`^[0-9a-f]{4,64}$`)
18

19
type ObjectFormat interface {
20
	// Name returns the name of the object format
21
	Name() string
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
25
	EmptyTree() ObjectID
26
	// FullLength is the length of the hash's hex string
27
	FullLength() int
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
34
}
35

36
/* SHA1 Type */
37
type Sha1ObjectFormatImpl struct{}
38

39
var (
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,
44
	}
45
)
46

47
func (Sha1ObjectFormatImpl) Name() string { return "sha1" }
48
func (Sha1ObjectFormatImpl) EmptyObjectID() ObjectID {
49
	return emptySha1ObjectID
50
}
51

52
func (Sha1ObjectFormatImpl) EmptyTree() ObjectID {
53
	return emptySha1Tree
54
}
55
func (Sha1ObjectFormatImpl) FullLength() int { return 40 }
56
func (Sha1ObjectFormatImpl) IsValid(input string) bool {
57
	return sha1Pattern.MatchString(input)
58
}
59

60
func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID {
61
	var id Sha1Hash
62
	copy(id[0:20], b)
63
	return &id
64
}
65

66
// ComputeHash compute the hash for a given ObjectType and content
67
func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
68
	hasher := sha1.New()
69
	_, _ = hasher.Write(t.Bytes())
70
	_, _ = hasher.Write([]byte(" "))
71
	_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
72
	_, _ = hasher.Write([]byte{0})
73

74
	// HashSum generates a SHA1 for the provided hash
75
	var sha1 Sha1Hash
76
	copy(sha1[:], hasher.Sum(nil))
77
	return &sha1
78
}
79

80
/* SHA256 Type */
81
type Sha256ObjectFormatImpl struct{}
82

83
var (
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,
89
		0x53, 0x21,
90
	}
91
)
92

93
func (Sha256ObjectFormatImpl) Name() string { return "sha256" }
94
func (Sha256ObjectFormatImpl) EmptyObjectID() ObjectID {
95
	return emptySha256ObjectID
96
}
97

98
func (Sha256ObjectFormatImpl) EmptyTree() ObjectID {
99
	return emptySha256Tree
100
}
101
func (Sha256ObjectFormatImpl) FullLength() int { return 64 }
102
func (Sha256ObjectFormatImpl) IsValid(input string) bool {
103
	return sha256Pattern.MatchString(input)
104
}
105

106
func (Sha256ObjectFormatImpl) MustID(b []byte) ObjectID {
107
	var id Sha256Hash
108
	copy(id[0:32], b)
109
	return &id
110
}
111

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})
119

120
	// HashSum generates a SHA256 for the provided hash
121
	var sha256 Sha1Hash
122
	copy(sha256[:], hasher.Sum(nil))
123
	return &sha256
124
}
125

126
var (
127
	Sha1ObjectFormat   ObjectFormat = Sha1ObjectFormatImpl{}
128
	Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{}
129
)
130

131
var SupportedObjectFormats = []ObjectFormat{
132
	Sha1ObjectFormat,
133
}
134

135
func ObjectFormatFromName(name string) ObjectFormat {
136
	for _, objectFormat := range SupportedObjectFormats {
137
		if name == objectFormat.Name() {
138
			return objectFormat
139
		}
140
	}
141
	return nil
142
}
143

144
func IsValidObjectFormat(name string) bool {
145
	return ObjectFormatFromName(name) != nil
146
}
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.