mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	Fix incorrect object id hash function (#30708)
Great thanks to @oliverpool for figuring out the problem and proposing a fix. Regression of #28138 Incorrect hash causes the user's LFS files get all deleted when running `doctor fix all` (by the way, remove unused/non-standard comments) Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
		| @@ -33,7 +33,6 @@ type ObjectFormat interface { | |||||||
| 	ComputeHash(t ObjectType, content []byte) ObjectID | 	ComputeHash(t ObjectType, content []byte) ObjectID | ||||||
| } | } | ||||||
|  |  | ||||||
| /* SHA1 Type */ |  | ||||||
| type Sha1ObjectFormatImpl struct{} | type Sha1ObjectFormatImpl struct{} | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -70,14 +69,10 @@ func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID | |||||||
| 	_, _ = hasher.Write([]byte(" ")) | 	_, _ = hasher.Write([]byte(" ")) | ||||||
| 	_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10))) | 	_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10))) | ||||||
| 	_, _ = hasher.Write([]byte{0}) | 	_, _ = hasher.Write([]byte{0}) | ||||||
|  | 	_, _ = hasher.Write(content) | ||||||
| 	// HashSum generates a SHA1 for the provided hash | 	return h.MustID(hasher.Sum(nil)) | ||||||
| 	var sha1 Sha1Hash |  | ||||||
| 	copy(sha1[:], hasher.Sum(nil)) |  | ||||||
| 	return &sha1 |  | ||||||
| } | } | ||||||
|  |  | ||||||
| /* SHA256 Type */ |  | ||||||
| type Sha256ObjectFormatImpl struct{} | type Sha256ObjectFormatImpl struct{} | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @@ -116,11 +111,8 @@ func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) Object | |||||||
| 	_, _ = hasher.Write([]byte(" ")) | 	_, _ = hasher.Write([]byte(" ")) | ||||||
| 	_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10))) | 	_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10))) | ||||||
| 	_, _ = hasher.Write([]byte{0}) | 	_, _ = hasher.Write([]byte{0}) | ||||||
|  | 	_, _ = hasher.Write(content) | ||||||
| 	// HashSum generates a SHA256 for the provided hash | 	return h.MustID(hasher.Sum(nil)) | ||||||
| 	var sha256 Sha1Hash |  | ||||||
| 	copy(sha256[:], hasher.Sum(nil)) |  | ||||||
| 	return &sha256 |  | ||||||
| } | } | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
|   | |||||||
| @@ -16,7 +16,6 @@ type ObjectID interface { | |||||||
| 	Type() ObjectFormat | 	Type() ObjectFormat | ||||||
| } | } | ||||||
|  |  | ||||||
| /* SHA1 */ |  | ||||||
| type Sha1Hash [20]byte | type Sha1Hash [20]byte | ||||||
|  |  | ||||||
| func (h *Sha1Hash) String() string { | func (h *Sha1Hash) String() string { | ||||||
| @@ -40,7 +39,6 @@ func MustIDFromString(hexHash string) ObjectID { | |||||||
| 	return id | 	return id | ||||||
| } | } | ||||||
|  |  | ||||||
| /* SHA256 */ |  | ||||||
| type Sha256Hash [32]byte | type Sha256Hash [32]byte | ||||||
|  |  | ||||||
| func (h *Sha256Hash) String() string { | func (h *Sha256Hash) String() string { | ||||||
| @@ -54,7 +52,6 @@ func (h *Sha256Hash) IsZero() bool { | |||||||
| func (h *Sha256Hash) RawValue() []byte { return h[:] } | func (h *Sha256Hash) RawValue() []byte { return h[:] } | ||||||
| func (*Sha256Hash) Type() ObjectFormat { return Sha256ObjectFormat } | func (*Sha256Hash) Type() ObjectFormat { return Sha256ObjectFormat } | ||||||
|  |  | ||||||
| /* utility */ |  | ||||||
| func NewIDFromString(hexHash string) (ObjectID, error) { | func NewIDFromString(hexHash string) (ObjectID, error) { | ||||||
| 	var theObjectFormat ObjectFormat | 	var theObjectFormat ObjectFormat | ||||||
| 	for _, objectFormat := range SupportedObjectFormats { | 	for _, objectFormat := range SupportedObjectFormats { | ||||||
|   | |||||||
| @@ -18,4 +18,8 @@ func TestIsValidSHAPattern(t *testing.T) { | |||||||
| 	assert.False(t, h.IsValid("abc")) | 	assert.False(t, h.IsValid("abc")) | ||||||
| 	assert.False(t, h.IsValid("123g")) | 	assert.False(t, h.IsValid("123g")) | ||||||
| 	assert.False(t, h.IsValid("some random text")) | 	assert.False(t, h.IsValid("some random text")) | ||||||
|  | 	assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", ComputeBlobHash(Sha1ObjectFormat, nil).String()) | ||||||
|  | 	assert.Equal(t, "2e65efe2a145dda7ee51d1741299f848e5bf752e", ComputeBlobHash(Sha1ObjectFormat, []byte("a")).String()) | ||||||
|  | 	assert.Equal(t, "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813", ComputeBlobHash(Sha256ObjectFormat, nil).String()) | ||||||
|  | 	assert.Equal(t, "eb337bcee2061c5313c9a1392116b6c76039e9e30d71467ae359b36277e17dc7", ComputeBlobHash(Sha256ObjectFormat, []byte("a")).String()) | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user