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