Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 3413a6c

Browse files
ethantkoeniglafriks
authored andcommitted
Simplify sha1 functions (#111)
1 parent 827f97a commit 3413a6c

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

sha1.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package git
66

77
import (
8+
"bytes"
89
"encoding/hex"
910
"fmt"
1011
"strings"
@@ -26,43 +27,23 @@ func (id SHA1) Equal(s2 interface{}) bool {
2627
}
2728
return v == id.String()
2829
case []byte:
29-
if len(v) != 20 {
30-
return false
31-
}
32-
for i, v := range v {
33-
if id[i] != v {
34-
return false
35-
}
36-
}
30+
return bytes.Equal(v, id[:])
3731
case SHA1:
38-
for i, v := range v {
39-
if id[i] != v {
40-
return false
41-
}
42-
}
32+
return v == id
4333
default:
4434
return false
4535
}
46-
return true
4736
}
4837

4938
// String returns string (hex) representation of the Oid.
5039
func (id SHA1) String() string {
51-
result := make([]byte, 0, 40)
52-
hexvalues := []byte("0123456789abcdef")
53-
for i := 0; i < 20; i++ {
54-
result = append(result, hexvalues[id[i]>>4])
55-
result = append(result, hexvalues[id[i]&0xf])
56-
}
57-
return string(result)
40+
return hex.EncodeToString(id[:])
5841
}
5942

6043
// MustID always creates a new SHA1 from a [20]byte array with no validation of input.
6144
func MustID(b []byte) SHA1 {
6245
var id SHA1
63-
for i := 0; i < 20; i++ {
64-
id[i] = b[i]
65-
}
46+
copy(id[:], b)
6647
return id
6748
}
6849

0 commit comments

Comments
 (0)