Showing the changes to modules/git/object_format.go (1 of 1 file). Show all
Unified · Split
| @@ -6,6 +6,7 @@ package git · 5 unmodified lines | |||
| 6 | import ( | 6 | import ( |
| 7 | "crypto/sha1" | 7 | "crypto/sha1" |
| 8 | "crypto/sha256" | 8 | "crypto/sha256" |
| 9 | "hash" | ||
| 9 | "regexp" | 10 | "regexp" |
| 10 | "strconv" | 11 | "strconv" |
| 11 | ) | 12 | ) |
| @@ -33,6 +34,15 @@ type ObjectFormat interface { · 21 unmodified lines | |||
| 33 | ComputeHash(t ObjectType, content []byte) ObjectID | 34 | ComputeHash(t ObjectType, content []byte) ObjectID |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 37 | func computeHash(dst []byte, hasher hash.Hash, t ObjectType, content []byte) []byte { | ||
| 38 | _, _ = hasher.Write(t.Bytes()) | ||
| 39 | _, _ = hasher.Write([]byte(" ")) | ||
| 40 | _, _ = hasher.Write([]byte(strconv.Itoa(len(content)))) | ||
| 41 | _, _ = hasher.Write([]byte{0}) | ||
| 42 | _, _ = hasher.Write(content) | ||
| 43 | return hasher.Sum(dst) | ||
| 44 | } | ||
| 45 | |||
| 36 | /* SHA1 Type */ | 46 | /* SHA1 Type */ |
| 37 | type Sha1ObjectFormatImpl struct{} | 47 | type Sha1ObjectFormatImpl struct{} |
| 38 | 48 | ||
| @@ -65,16 +75,9 @@ func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID { · 26 unmodified lines | |||
| 65 | 75 | ||
| 66 | // ComputeHash compute the hash for a given ObjectType and content | 76 | // ComputeHash compute the hash for a given ObjectType and content |
| 67 | func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { | 77 | func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { |
| 68 | hasher := sha1.New() | 78 | var obj Sha1Hash |
| 69 | _, _ = hasher.Write(t.Bytes()) | 79 | computeHash(obj[:0], sha1.New(), t, content) |
| 70 | _, _ = hasher.Write([]byte(" ")) | 80 | return &obj |
| 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 | } | 81 | } |
| 79 | 82 | ||
| 80 | /* SHA256 Type */ | 83 | /* SHA256 Type */ |
| @@ -111,16 +114,9 @@ func (Sha256ObjectFormatImpl) MustID(b []byte) ObjectID { · 30 unmodified lines | |||
| 111 | 114 | ||
| 112 | // ComputeHash compute the hash for a given ObjectType and content | 115 | // ComputeHash compute the hash for a given ObjectType and content |
| 113 | func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { | 116 | func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { |
| 114 | hasher := sha256.New() | 117 | var obj Sha256Hash |
| 115 | _, _ = hasher.Write(t.Bytes()) | 118 | computeHash(obj[:0], sha256.New(), t, content) |
| 116 | _, _ = hasher.Write([]byte(" ")) | 119 | return &obj |
| 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 | } | 120 | } |
| 125 | 121 | ||
| 126 | var ( | 122 | var ( |