1
1
package git
2
2
3
3
import (
4
+ "bytes"
5
+ "crypto/sha1" //nolint:gosec
6
+ "crypto/sha256"
4
7
"encoding/hex"
5
8
"errors"
6
9
)
7
10
11
+ const (
12
+ HashSizeSHA256 = sha256 .Size
13
+ HashSizeSHA1 = sha1 .Size
14
+ HashSizeMax = HashSizeSHA256
15
+ )
16
+
17
+ type HashAlgo int
18
+
19
+ const (
20
+ HashUnknown HashAlgo = iota
21
+ HashSHA1
22
+ HashSHA256
23
+ )
24
+
8
25
// OID represents the SHA-1 object ID of a Git object, in binary
9
26
// format.
10
27
type OID struct {
11
- v [20 ]byte
28
+ v [HashSizeMax ]byte
29
+ hashSize int
12
30
}
13
31
14
- // NullOID is the null object ID; i.e., all zeros.
15
- var NullOID OID
32
+ func (h HashAlgo ) NullOID () OID {
33
+ switch h {
34
+ case HashSHA1 :
35
+ return OID {hashSize : HashSizeSHA1 }
36
+ case HashSHA256 :
37
+ return OID {hashSize : HashSizeSHA256 }
38
+ }
39
+ return OID {}
40
+ }
41
+
42
+ func (h HashAlgo ) HashSize () int {
43
+ switch h {
44
+ case HashSHA1 :
45
+ return HashSizeSHA1
46
+ case HashSHA256 :
47
+ return HashSizeSHA256
48
+ }
49
+ return 0
50
+ }
51
+
52
+ // defaultNullOID is the null object ID; i.e., all zeros.
53
+ var defaultNullOID OID
54
+
55
+ func IsNullOID (o OID ) bool {
56
+ return bytes .Equal (o .v [:], defaultNullOID .v [:])
57
+ }
16
58
17
59
// OIDFromBytes converts a byte slice containing an object ID in
18
60
// binary format into an `OID`.
19
61
func OIDFromBytes (oidBytes []byte ) (OID , error ) {
20
62
var oid OID
21
- if len (oidBytes ) != len (oid .v ) {
63
+ oidSize := len (oidBytes )
64
+ if oidSize != HashSizeSHA1 && oidSize != HashSizeSHA256 {
22
65
return OID {}, errors .New ("bytes oid has the wrong length" )
23
66
}
24
- copy (oid .v [0 :20 ], oidBytes )
67
+ oid .hashSize = oidSize
68
+ copy (oid .v [0 :oidSize ], oidBytes )
25
69
return oid , nil
26
70
}
27
71
28
- // NewOID converts an object ID in hex format (i.e., `[0-9a-f]{40}`)
29
- // into an `OID`.
72
+ // NewOID converts an object ID in hex format (i.e., `[0-9a-f]{40,64}`) into an `OID`.
30
73
func NewOID (s string ) (OID , error ) {
31
74
oidBytes , err := hex .DecodeString (s )
32
75
if err != nil {
@@ -37,18 +80,18 @@ func NewOID(s string) (OID, error) {
37
80
38
81
// String formats `oid` as a string in hex format.
39
82
func (oid OID ) String () string {
40
- return hex .EncodeToString (oid .v [:])
83
+ return hex .EncodeToString (oid .v [:oid . hashSize ])
41
84
}
42
85
43
86
// Bytes returns a byte slice view of `oid`, in binary format.
44
87
func (oid OID ) Bytes () []byte {
45
- return oid .v [:]
88
+ return oid .v [:oid . hashSize ]
46
89
}
47
90
48
91
// MarshalJSON expresses `oid` as a JSON string with its enclosing
49
92
// quotation marks.
50
93
func (oid OID ) MarshalJSON () ([]byte , error ) {
51
- src := oid .v [:]
94
+ src := oid .v [:oid . hashSize ]
52
95
dst := make ([]byte , hex .EncodedLen (len (src ))+ 2 )
53
96
dst [0 ] = '"'
54
97
dst [len (dst )- 1 ] = '"'
0 commit comments